2

I have a string array, and I want to check if a string is a number, and then put it in an object array as an int.

(For those of you asking why an object array, because I want to check for characters and other things too)

I have this:

Console.WriteLine("Enter parameters for the function with a space in between each parameter: "); String stringParameters = Console.ReadLine();
String[] parametersStringArray = stringParameters.Split(' ');
Object[] parametersArray = new Object[parametersStringArray.Length];

for (int i = 0; i < parametersStringArray.Length; i++)
{
    int.TryParse(parametersStringArray[i], out int.Parse(parametersArray[i]));
}

It doesn't compile and I am not familiar with the 'out' command, what's wrong and how do I fix it?

Thanks.

shoham
  • 792
  • 2
  • 12
  • 30
  • Why do you need to put it into an `object` array...? As you are just putting `int` values in there...... – Arran Sep 09 '13 at 14:16
  • parametersArray should be rather int array – Kamil Budziewski Sep 09 '13 at 14:17
  • why an object array? (you probably only need `out parametersArray[1]`) – Sayse Sep 09 '13 at 14:17
  • The statement *"It doesn't compile"* without the compiler error is worthless. What is the *exact* compiler error? – abelenky Sep 09 '13 at 14:23
  • You should read this question and my answer to understand first, why you must give a *variable* as the parameter when passing `out` or `ref`, and second, why that variable must *exactly* match the parameter type. http://stackoverflow.com/questions/1207144/c-sharp-why-doesnt-ref-and-out-support-polymorphism/1207302#1207302 – Eric Lippert Sep 09 '13 at 17:01

4 Answers4

5

use this code

     Console.WriteLine("Enter parameters for the function with a space in between each parameter: "); String stringParameters = Console.ReadLine();
     String[] parametersStringArray = stringParameters.Split(' ');
     Object[] parametersArray = new Object[parametersStringArray.Length];

     for (int i = 0; i < parametersStringArray.Length; i++)
     {
        int tmp;
        if (int.TryParse(parametersStringArray[i], out tmp))
           parametersArray[i] = tmp;

     }
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
3

First of all, your target array has to be int[], not Object[]:

int[] parametersArray = new int[parametersStringArray.Length];

And your TryParse call is incorrect. Try following:

int.TryParse(parametersStringArray[i].ToString(), out parametersArray[i]);
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1
int res;
if (int.TryParse(parametersStringArray[i], out res)) {
    parametersArray[i] = res;
}
else {
Console.WriteLine("Not a number at index {0}", i);
}
bohdan_trotsenko
  • 5,167
  • 3
  • 43
  • 70
0

Take a look at this:

public static int? TryParse(string s)
{
    int result;
    if (int.TryParse(s, out result))
    {
        return result;
    }
    return null;
}

var res = new string[] { "1", "2", "a" }
    .Select(x => TryParse(x).GetValueOrDefault());
Servy
  • 202,030
  • 26
  • 332
  • 449
Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32