i have two numbers in array list
array[0] = 1
array[1] = 2
but
if i use array[2] i know its null not exist but how to check
if( array[2] what can i do for check or exist )
{
show( array not exit);
}
plz help.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jun 13, 2012, 7:14 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 13, 2012, 6:48 AM EDT
if (array.Count > 2)
{
// do something with array[2]
}
Notice that array[2] can still exist but can have a value of null. You need to check for nullness separately.
Saqi60 reputationPosted Jun 13, 2012, 6:52 AM EDT
thanks
VulpesRank #55 · 29.2K reputationPosted Jun 13, 2012, 6:56 AM EDT
if (array.Count > 2 && array[2] == null)
{
// do something
}
Glenn PattonRank #6,159 · 275 reputationPosted Jun 13, 2012, 7:00 AM EDT
Silly kind of question if you only have two (0,1) elements in the array why are you looking at the third (2) why not limit the value to the max it can have in it in this case two,
so you have limit set to n-1, where n is the array size.
something like the following:
int limit_array = 0;
limit_arrray = array.GetLength();
for(int i = 0; i <= limit_array; i++)
{
}
By keeping to the size of the array you won't need to venture into try....catch() land which can be painful!
Glenn PattonRank #6,159 · 275 reputationPosted Jun 13, 2012, 7:02 AM EDT
Saqi60 reputationPosted Jun 13, 2012, 7:05 AM EDT
if (ldays >= 31)
{
Double t = ldays / 30;
char[] s = { '.' };
String[] lastmonth = t.ToString().Split(s);
hipertensos.idade_txb.Text = "";
string months = lastmonth[0].ToString();
string days = null;
if (Convert.ToInt32(lastmonth[1]) > lastmonth.Length ) error is there some time lastmonth[1] not exist sometime have value.......................
{
days = lastmonth[1].ToString();
}
else
{
days = "0";
}
hipertensos.idade_txb.Text = String.Format("{0:0}.{1:0}.{2:0}", years[0], months, days);
}
else
{
hipertensos.idade_txb.Text = String.Format("{0:0}.{1:0}.{2:0}", years[0], "0", idays );
}
Saqi60 reputationPosted Jun 13, 2012, 7:18 AM EDT