开发者

Array issue for checking the count

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 t开发者_运维问答his attribute exists in client 


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";


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜