none
Error_Cannot implicitly convert type 'string' to 'string[]'

    Question

  • msg = Console.ReadLine();           
                for (i = 0; i < msg.Length; i++)
                    p[i] = msg[i];

     

    //Here msg is an array of type string

    Friday, March 25, 2011 5:38 AM

Answers

  • You shoulddo it like:

    string msg = Console.ReadLine();
    string[]array = msg.Split(' ');
    
    

     

    or if you want to put every user`s input into an array:

    string[] array = null;
    int i = 1;
    while(true)
    {
     Console.WriteLine("Enter new insertion:"); 
     string msg = Console.ReadLine();
     Array.Resize(ref array, i++);
     array[i - 1] = msg; 
     Console.WriteLine("Do you want a new insertion? Type yes, or no);
     msg = Console.ReadLine();
     if(msg == "no")
      break;
    }
    

     

    Hope it helps,

    Mitja


    • Edited by Mitja Bonca Friday, March 25, 2011 10:50 AM Ups, sorry it was a typo (I did the code by my heart).
    • Marked as answer by Lie YouModerator Monday, April 04, 2011 4:25 AM
    Friday, March 25, 2011 5:53 AM
  • Hello Techis,

    Welcome to the MSDN Forum.

    I want to know what’s your “p[i]”.

    Here is my code, it works well  on my machine.

                    string msg =Console.ReadLine();

                string[] p= new string [msg.Length+1];

    StringBuilder sb = new StringBuilder();//this is also OK.

     

                for (int i = 0; i < msg.Length; i++)

                {

      sb.Append(msg[i]);//for StringBuilder

     

                    p[i] = msg[i].ToString();//here, if I don’t use ToString, there is always an error “Cannot implicitly convert type 'char' to 'string' 

     

                }

                foreach (string  c in p)

                {

                    Console.Write(c);

                }

                Console.ReadLine();

    I hope this will help resolve your problem

    If you have any difficulty in future programming, we welcome you to post here again. There are many enthusiastic community members here to provide their help and suggestions.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 28, 2011 7:23 AM

All replies

  • You shoulddo it like:

    string msg = Console.ReadLine();
    string[]array = msg.Split(' ');
    
    

     

    or if you want to put every user`s input into an array:

    string[] array = null;
    int i = 1;
    while(true)
    {
     Console.WriteLine("Enter new insertion:"); 
     string msg = Console.ReadLine();
     Array.Resize(ref array, i++);
     array[i - 1] = msg; 
     Console.WriteLine("Do you want a new insertion? Type yes, or no);
     msg = Console.ReadLine();
     if(msg == "no")
      break;
    }
    

     

    Hope it helps,

    Mitja


    • Edited by Mitja Bonca Friday, March 25, 2011 10:50 AM Ups, sorry it was a typo (I did the code by my heart).
    • Marked as answer by Lie YouModerator Monday, April 04, 2011 4:25 AM
    Friday, March 25, 2011 5:53 AM
  • Techis, Use Mitja's solution. Type Split instead of Spint in the 2nd line. That was a typing mistake.

    Thanks


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Friday, March 25, 2011 10:45 AM
  • Hello Techis,

    Welcome to the MSDN Forum.

    I want to know what’s your “p[i]”.

    Here is my code, it works well  on my machine.

                    string msg =Console.ReadLine();

                string[] p= new string [msg.Length+1];

    StringBuilder sb = new StringBuilder();//this is also OK.

     

                for (int i = 0; i < msg.Length; i++)

                {

      sb.Append(msg[i]);//for StringBuilder

     

                    p[i] = msg[i].ToString();//here, if I don’t use ToString, there is always an error “Cannot implicitly convert type 'char' to 'string' 

     

                }

                foreach (string  c in p)

                {

                    Console.Write(c);

                }

                Console.ReadLine();

    I hope this will help resolve your problem

    If you have any difficulty in future programming, we welcome you to post here again. There are many enthusiastic community members here to provide their help and suggestions.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 28, 2011 7:23 AM