public const STATS_CONST[] astStatsConst =
{
{ 0, 0, 0, "Pot", "counts" },
{ 0, 0, 0, "Position", "" },
{ 0, 0, 0, "Battery Capacity", "mAh" },
{ 0, 0, 0, "Battery Voltage", "V" },
{ 0, 0, 0, "Battery Current", "mA" },
{ 0, 0, 0, "Battery Temperature","K" },
{ 0, 0, 0, "Battery Charge Current","mA" },
{ 0, 0, 0, "Pot Error", "" },
{ 0, 0, 0, "Movement Error", "" }
};
请问这个结构体错在哪里?
{
{ 0, 0, 0, "Pot", "counts" },
{ 0, 0, 0, "Position", "" },
{ 0, 0, 0, "Battery Capacity", "mAh" },
{ 0, 0, 0, "Battery Voltage", "V" },
{ 0, 0, 0, "Battery Current", "mA" },
{ 0, 0, 0, "Battery Temperature","K" },
{ 0, 0, 0, "Battery Charge Current","mA" },
{ 0, 0, 0, "Pot Error", "" },
{ 0, 0, 0, "Movement Error", "" }
};
请问这个结构体错在哪里?
public const STATS_CONST[] astStatsConst =
{
{ 0, 0, 0, "Pot", "counts" },
{ 0, 0, 0, "Position", "" },
{ 0, 0, 0, "Battery Capacity", "mAh" },
{ 0, 0, 0, "Battery Voltage", "V" },
{ 0, 0, 0, "Battery Current", "mA" },
{ 0, 0, 0, "Battery Temperature","K" },
{ 0, 0, 0, "Battery Charge Current","mA" },
{ 0, 0, 0, "Pot Error", "" },
{ 0, 0, 0, "Movement Error", "" }
};
结构的关键字是struct,const 关键字用于修改字段或局部变量的声明。它指定字段或局部变量的值不能被修改。常数声明引入给定类型的一个或多个常数。
数组为引用类型,而且如果你的STATS_CONST是个结构的化,不能用{ 0, 0, 0, "Pot", "counts" }实例化结构吧?
public struct STATS_CONST
{
public int x;
public int y;
public int z;
public string str1;
public string str2;
public STATS_CONST(int x, int y, int z, string a, string b)
{
this.x = x;
this.y = y;
this.z = z;
this.str1 = a;
this.str2 = b;
}
} public STATS_CONST[] astStatsConst = {
new STATS_CONST(0,0,0,"Pot","counts"),
new STATS_CONST(0,0,0,"Position","")
};
2、结构不用{}这样初始化,这样初始化只有数组或者集合。
可以对结构增加一个构造函数。如下面这样改造一下。 public struct STATS_CONST
{
int i1;
int i2;
int i3;
string s1;
string s2; public STATS_CONST( int ii1, int ii2, int ii3, string ss1, string ss2 )
{
i1 = ii1;
i2 = ii2;
i3 = ii3;
s1 = ss1;
s2 = ss2;
}
}重新定义为readonly,模仿一下常量了。 public readonly STATS_CONST[] astStatsConst =
{
new STATS_CONST( 0, 0, 0, "Pot", "counts" ),
new STATS_CONST( 0, 0, 0, "Position", "" ),
new STATS_CONST( 0, 0, 0, "Battery Capacity", "mAh" ),
new STATS_CONST( 0, 0, 0, "Battery Voltage", "V" ),
new STATS_CONST( 0, 0, 0, "Battery Current", "mA" ),
new STATS_CONST( 0, 0, 0, "Battery Temperature","K" ),
new STATS_CONST( 0, 0, 0, "Battery Charge Current","mA" ),
new STATS_CONST( 0, 0, 0, "Pot Error", "" ),
new STATS_CONST( 0, 0, 0, "Movement Error", "" )
};
struct类型是一种值类型,通常用来封装小型相关变量组,例如,矩形的坐标或库存商品的特征。下面的示例显示了一个简单的结构声明。public struct Book
{
public decimal price;
public string title;
public string author;
}
备注
结构还可以包含构造函数、常量、字段、方法、属性、索引器、运算符、事件和嵌套类型,但如果同时需要上述几种成员,则应当考虑改为使用类作为类型。结构可以实现接口,但它们无法继承另一个结构。因此,结构成员无法声明为 protected。有关更多信息,请参见结构(C# 编程指南)。
STATS_CONST 应该是一个自定义类型的,
这是基础知识