Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
           BuildList(new object[] {1,tests,3});
        }



public string BuildList(object[] input)
            {
 string result = null;
            for (int i = 0; i < input.Length; i++)
            {
              if (input[i] != null)
                {

                   result = result + input[i].ToString() +  ",";
                }


                else
                {
                    result = "error";
                }
            }




            return result;
}
Posted
Updated 20-Jan-13 1:42am
v5
Comments
Sandeep Mewara 20-Jan-13 6:52am    
And did you try it by yourself? Where are you stuck? Whats confusing you?

Sounds like a homework assignment...
mrDivan 20-Jan-13 6:57am    
Hi THANK YOU FOR YOUR HELP yes i tried it works but doesn whant to give me an error when input[i]= test it should then show error only when input[i]= test and not 1or 2or3
mrDivan 20-Jan-13 7:12am    
Can anyone help me

1 solution

If you want to check for valid numbewrs, then you are probably best off doing a trial conversion to the actual datatype you are expecting.
So if you are expecting integer values in string form then use the int.TryParse method:
C#
public string BuildList(object[] input)
    {
    StringBuilder result = new StringBuilder();
    foreach (object o in input)
        {
        string s = o as string;
        if (s != null)
            {
            int i;
            if (int.TryParse(s, out i))
                {
                result.AppendFormat("{0}, ", s);
                continue;
                }
            }
        return "Error";
        }
    return result.ToString();
    }
Note that I have changed your code quite a bit - some of it to make your code compile (adding the return at the end), some to make it clearer (changing the for loop to a foreach) and some to make it more efficient (using a StringBuilder rather than concatenating strings). I also sorted out the indentation for you, so it is easier to read...


"hallo originalGriff thank you so much for your response the question is I have to handle the nullreference exception an the error input example"test" I do not know the type of the input"

Try this:
C#
public string BuildList(object[] input)
    {
    StringBuilder result = new StringBuilder();
    foreach (object o in input)
        {
        if (o != null)
            {
            string s = o.ToString();
            if (s != null)
                {
                int i;
                if (int.TryParse(s, out i))
                    {
                    result.AppendFormat("{0}, ", s);
                    continue;
                    }
                }
            }
        return "Error";
        }
    return result.ToString();
    }
 
Share this answer
 
v2
Comments
mrDivan 20-Jan-13 7:32am    
hallo originalGriff thank you so much for your response the question is I have to handle the nullreference exception an the error input example"test" I do not know the type of the input
public string BuildList(object[] input)
{
string result = null;
for (int i = 0; i < input.Length; i++)
{
if (input[i] != null)
{

result = result + input[i].ToString() + ", ";
}


else
{
result = "error";
}
}




return result;
}
OriginalGriff 20-Jan-13 7:44am    
Answer updated
mrDivan 20-Jan-13 7:52am    
thank you
OriginalGriff 20-Jan-13 7:53am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900