tags:

views:

42

answers:

2

I have a code residing on server which contain 5 attribute values assigned from an array which shown as below,but the client machine may or may not contain 5 attribute values if it is installed by old version(5th one is new one "string version") now i have one requirement "The code needs to check to make sure that the fifth attribute exis.If there are not five entries in the array, the version should be set to a value like "0.0.0.0"

  string Name = this_event.Data[0].lower_value;
                   string no = this_event.Data[1].lower_value;
                   string debitvalue = this_event.Data[2].lower_value;
                   string creditvalue = this_event.Data[3].lower_value;
                   string version = this_event.Data[4].lower_value;//we have to check here whether this attribute exists in client 
A: 

In order to get the element count in an array you can use the Length property.

string Name = this_event.Data[0].lower_value;
string no = this_event.Data[1].lower_value;
string debitvalue = this_event.Data[2].lower_value;
string creditvalue = this_event.Data[3].lower_value;
string version = "0.0.0.0";
if (this_event.Data.Length >= 5)
{
   version = this_event.Data[4].lower_value;
}
JCasso
For forward compatibility you might want to make that ">= 5" instead of "== 5"
Jon Skeet
Exactly ,,thanks for your replay
peter
+1  A: 

As you are learning, this is not a very scalable solution. You should really look to using a strongly typed serializable object rather than an arbitrary array for this. That being said, a simple solution to your specific problem would be:

string version = this_event.Data.length > 4 ? this_event.Data[4].lower_value : "0.0.0.0";
Greg Martin
do we need a ternary operator here,though the explanation given above as answer is sufficient
peter
nope, it's not necessary, just a matter of style preference really.
Greg Martin