我有兩個類,字段和FieldValues:字段如下:錯誤CS1729,我做錯了什麼?
enum FieldType
{
Boolean,
Integer,
String
}
class Fields
{
public FieldType type { get; set; }
public string name { get; set; }
public string GetFieldDeclaration()
{
switch (type)
{
case FieldType.Boolean:
return name + " tinyint(1)";
case FieldType.Integer:
return name + " int";
case FieldType.String:
return name + " varchar(2048)";
default:
return string.Empty;
}
}
和FieldValues:域
class FieldValues:Fields
{
public Dictionary<int, string> dictionary { get; set; }
FieldValues(FieldType newType, string newName)
{
type = newType;
name = newName;
dictionary = new Dictionary<int, string>();
}
這些類表示將從一個XLS被提取並插入到一些值數據庫。在主程序中,
List<Fields> tableFields = new List<Fields>()
{
new Fields() { name = "id", type = FieldType.Integer },
new Fields() { name = "ECE", type = FieldType.Boolean },
new Fields() { name = "TC", type = FieldType.Boolean },
new Fields() { name = "Categories", type = FieldType.String },
};
string createQuery = "CREATE TABLE `poi_specs_mgu_ece_1.6` (";
//creaza tabelul in functie de cate field-uri(coloane) sunt in lista
for(var i = 0; i < tableFields.Count; i++)
{
createQuery += "\n\t" + tableFields[i].GetFieldDeclaration() + (i == tableFields.Count - 1 ? string.Empty : ",");
}
createQuery += "\n);";
//[WORK]create insert statement
List<FieldValues> fieldValues = new List<FieldValues>()
{
new FieldValues(tableFields[0]) {
dictionary = new Dictionary<int, string>()
{
{0, "test" },
{1, "test" },
{2, "test" },
{3, "test" },
{4, "test" },
{5, "test" }
}
},
new FieldValues(tableFields[1]) {
dictionary = new Dictionary<int, string>()
{
{0, "x" },
{1, "x" },
{2, " " },
{3, "x" },
{4, " " },
{5, "x" }
}
},
new FieldValues(tableFields[2]) {
dictionary = new Dictionary<int, string>()
{
{0, "x" },
{1, "x" },
{2, "x" },
{3, "x" },
{4, " " },
{5, "x" }
}
},
new FieldValues(tableFields[3]) {
dictionary = new Dictionary<int, string>()
{
{0, "Car" },
{1, "Travel" },
{2, "Stopping Possibilities" },
{3, "Petrol Stations" },
{4, "Rest Areas" },
{5, "Travel" }
}
},
};
string createQuery2 = createQuery + "\n INSERT INTO `poi_specs_mgu_ece_1.6` (";
for (var i = 0; i < tableFields.Count; i++)
{
createQuery2 += "\n\t" + tableFields[i].GetFieldDeclaration() + (i == tableFields.Count - 1 ? string.Empty : ",");
}
createQuery2 += ")\n VALUES (";
for (var i = 0; i < fieldValues.Count; i++)
{
for (var j = 0; j < fieldValues[i].dictionary.Count; j++)
{
createQuery2 += "\n\t" + fieldValues[i].dictionary[j];
}
}
createQuery2 += " \n);";
File.WriteAllText("output.txt", createQuery2);
Console.WriteLine(createQuery2);
Console.ReadKey();
當編譯我收到以下錯誤:
Error CS1729 'FieldValues' does not contain a constructor that takes 1 arguments.
我真的不明白到底爲什麼從FieldValues我的構造並不在這裏工作。
因爲你的構造函數有_two_參數'newType'和'newName',但只提供了一個。就像錯誤信息所述。 –
「我不明白爲什麼我的FieldValues的構造函數在這裏不起作用」 - 它不工作,因爲它沒有被調用:) – Fildor
另一個問題是你是否需要使構造函數(或一個構造函數重載)更容易訪問。它目前是私人的。例如,使用關鍵字「internal」使其可以從整個項目訪問。 –