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.

5
  • Why do you need to put it into an object array...? As you are just putting int values in there...... Commented Sep 9, 2013 at 14:16
  • parametersArray should be rather int array Commented Sep 9, 2013 at 14:17
  • why an object array? (you probably only need out parametersArray[1]) Commented Sep 9, 2013 at 14:17
  • The statement "It doesn't compile" without the compiler error is worthless. What is the exact compiler error? Commented Sep 9, 2013 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. stackoverflow.com/questions/1207144/… Commented Sep 9, 2013 at 17:01

4 Answers 4

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;

     }
Sign up to request clarification or add additional context in comments.

1 Comment

Such a nice Boxing example.
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]);

Comments

1
int res;
if (int.TryParse(parametersStringArray[i], out res)) {
    parametersArray[i] = res;
}
else {
Console.WriteLine("Not a number at index {0}", i);
}

Comments

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.