CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2010
    Posts
    26

    Converting an int list to a string array.

    Hello everyone..
    I've tried the ToString with no success. Can anyone tell me how to convert an int list to a string array. Thanks.. here is my code.

    Code:
    string [] mystring;
    
    //prints each element of your list individually
                foreach (int item in ListOutput)
                {
                    mystring = (ListOutput.ToString()); 
                }

  2. #2
    Join Date
    Jan 2007
    Posts
    491

    Re: Converting an int list to a string array.

    Code:
                foreach (int item in ListOutput)
                {
                    mystring = (ListOutput.ToString()); 
                }
    if you try to do something like that, then you don't know what you're doing.

    here's an example for a code that will work:
    Code:
    int[] ints = new int[] {1,2,3,91,16,0,-90};
    string[] intsStringArray = new string[ints.Length];
    
    for(int i=0;i<ints.Length;i++)
       intsStringArray[i] = ints[i].ToString();

  3. #3
    Join Date
    Aug 2010
    Posts
    26

    Re: Converting an int list to a string array.

    Talikag
    Will this work if im working with a list<int> Thats where
    i'm have the issue at. I was trying to see your code would work,
    but it would not work, since i hat a integer list. in your example you
    have an integer array..

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Converting an int list to a string array.

    It will work. Why don't you try it before assuming something won't work when you are obviously a beginner? List<T> defines operator []. They both implement IEnumerable, so you could also use a foreach loop with an indexer variable. Since you need an indexer, you may as well just use a for loop as Tal did. You could also use Array.ConvertAll.

  5. #5
    Join Date
    Aug 2010
    Posts
    26

    Re: Converting an int list to a string array.

    Big,
    Ed, I didn't try to waste Talikag time, i got an error message when trying to run the code. I was asking whether this could be implemented with integer list. What i'm try to do is
    I have a list of integer with i want to convert to string array to add to my string array. I already know the count in the integer list since i already have populate it but the problem is i'm not able to convert it to an stringarray .. I want to explicit converty it since I keep getting the message that its converting it implicitly

    Here is the error message i'm recieving..

    NULL REFERENCE EXCEPTION --->
    Object reference not set to an instance of an object.
    Last edited by wilnicm; September 18th, 2010 at 09:18 PM. Reason: change wording

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Converting an int list to a string array.

    Quote Originally Posted by wilnicm View Post
    Big,
    Ed, I didn't try to waste Talikag time, i got an error message when trying to run the code. I was asking whether this could be implemented with integer list. What i'm try to do is
    I have a list of integer with i want to convert to string array to add to my string array. I already know the count in the integer list since i already have populate it but the problem is i'm not able to convert it to an stringarray .. I want to explicit converty it since I keep getting the message that its converting it implicitly

    Here is the error message i'm recieving..

    NULL REFERENCE EXCEPTION --->
    Object reference not set to an instance of an object.
    We know what you are trying to do - there's no need to restate it. Also, if you post errors, you need to tell use on what line the error has occurred. The easiest way is to include the code snippet and put a comment in where the error is.

    You code has two errors in it:

    1) You've declared a string array, but haven't initialized it. That's why you're getting a null reference exception.
    2) When you are walking through the list, you are calling ToString( ) on ListOutput.

    To fix this, declare the string array as Talikag showed you.

    and walk through your list like Talikag showed (replacing his example with your code:

    Code:
    for(int i = 0; i < ListOutput.Length; i++)
    {
       myString[i] = ListOutput[i].ToString();
    }
    The other way to do this is use the foreach loop, but you're going to have to use an index variable to be able to set the myString array at the appropriate position in the array.

    Code:
    int i = 0;
    foreach( int item in ListOutput )
    {
       myString[ i++ ] = item.ToString( );
    }
    When BigEd referred to the indexer and just using a 'for' loop, this is what he was talking about.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured