foolish question , help!
-
suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:
-
suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:
You have already answered your question. When you create the array at each element a structure with all its members set to their default values is generated. Its like working with integers.
-
suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:
There's no facility provided by the CLR to do it, you have to do it on your own. The simplest way is to have a bool variable that's set to true on any property/method access. You can then check if the bool variable is value and know that it's just been initialized. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
suppose I have a struct type structA; and an array structA [] structArray = new structA[5]; how to test structA[0] is empty? cause struct is value type, can not compare it with null. Thank ya !:confused:
Create a static 'Empty' variable for you struct. Similar to Rectangle/Point structs. Eg
struct Foo
{
int a;
int b;public readonly static Empty;
static Foo() { Empty = new Foo() ;} //mite not even be neccesary infact,
//should be initialized to default value
}...
Foo[] a = new Foo[2];
for (int i = 0; i < a.Length; i++)
{
if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype
{
Console.WriteLine("a[{0}] = Foo.Empty", i);
}
}xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
Create a static 'Empty' variable for you struct. Similar to Rectangle/Point structs. Eg
struct Foo
{
int a;
int b;public readonly static Empty;
static Foo() { Empty = new Foo() ;} //mite not even be neccesary infact,
//should be initialized to default value
}...
Foo[] a = new Foo[2];
for (int i = 0; i < a.Length; i++)
{
if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype
{
Console.WriteLine("a[{0}] = Foo.Empty", i);
}
}xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
I think the problem stays the same. Here in Foo struct int a, b are initialized with its default values 0. What happens if we want to set "a[0].a = a[0].b = 0"? Then the check a[0].Equals(Foo.Empty) would succeed when its not empty anymore.. I think you need some sentinel value - a special or illegal value. For example you could use -1 as a sentinel. Default int value 0 also would do but you have to be sure that this value won't be treated as legal value.
-
I think the problem stays the same. Here in Foo struct int a, b are initialized with its default values 0. What happens if we want to set "a[0].a = a[0].b = 0"? Then the check a[0].Equals(Foo.Empty) would succeed when its not empty anymore.. I think you need some sentinel value - a special or illegal value. For example you could use -1 as a sentinel. Default int value 0 also would do but you have to be sure that this value won't be treated as legal value.
It really depends on what you want. Initializing structs to 'illegal' values will have some overhead. The point here is to check if the struct has been used or not. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
It really depends on what you want. Initializing structs to 'illegal' values will have some overhead. The point here is to check if the struct has been used or not. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
not 'illegal'.. sentinel value. ...The point here is to check if the struct has been used or not.... Try this: void SomeMethod() { Foo[] a = new Foo[2]; if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 1; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 0; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); } ....................... Output: "a[0] = Foo.Empty" "a[0] != Foo.Empty" "a[0] = Foo.Empty" !!!! which means that this struct haven't been used before! ..but we used a[0] before...
-
not 'illegal'.. sentinel value. ...The point here is to check if the struct has been used or not.... Try this: void SomeMethod() { Foo[] a = new Foo[2]; if (a[0].Equals(Foo.Empty)) // does a field by field comparision in valuetype MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 1; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); a[0].a = a[0].b = 0; if (a[0].Equals(Foo.Empty)) MessageBox.Show("a[0] = Foo.Empty"); else MessageBox.Show("a[0] != Foo.Empty"); } ....................... Output: "a[0] = Foo.Empty" "a[0] != Foo.Empty" "a[0] = Foo.Empty" !!!! which means that this struct haven't been used before! ..but we used a[0] before...
In this case you will have to do some more work. Something like:
struct Foo
{
bool init;
int a;
int b;public int A
{
get {return a;} set { init = true; a= value;}
}
public int B
{
get {return b;} set { init = true; b= value;}
}... the rest of previous example.
}xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots