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

    Question Need help with a program. Pls

    Ok, so to state the obvious. I am a programming newbie. I have one semester of C++ under my belt and I'm currently taking a C# class. I have code written, but when I try to compile I am getting 2 errors, both are: Cannot implicitly convert type 'void' to 'string'.


    Below are the two lines that I'm receiving the errors from. I'm not sure what I've done wrong and can post the entire code if need be. Any help would be greatly appreciated.

    message = Console.WriteLine("Original number: \n Digits Reversed:" +
    " \n Number of Digits: {0}",
    num, numReverse, numofDig);

    caption = Console.WriteLine("CIS 365 Midterm");


    Thanks,

    Chris

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Need help with a program. Pls

    Console.WriteLine returns void (meaning, it does not return anything).

    You can not cache the return of WriteLine to anything since it doesn't return anything.

  3. #3
    Join Date
    Mar 2010
    Posts
    4

    Re: Need help with a program. Pls

    Ok then, so I guess what is confusing me is how can I pass my values into the message box?

    I thought by setting my variable message = Console.WriteLine("blah, blah, blah"); then calling the MessageBox.Show(message, caption, icon, button);


    It would give me the desired result, but obviously I'm wrong.

  4. #4
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Need help with a program. Pls

    The best way to do this is to create the string before-hand, and use that string in the Console.WriteLine and the MessageBox.Show().

    ie:

    Code:
    string myString = "foobar";
    Console.WriteLine(myString);
    MessageBox.Show(myString, ...);
    To keep the formatting of your string, you can do:

    Code:
    int myInt = 1;
    string myString = string.Format("Foobar: {0}", myInt);
    Console.WriteLine(myString);
    MessageBox.Show(myString, ...);

  5. #5
    Join Date
    Feb 2010
    Posts
    6

    Re: Need help with a program. Pls

    "message" in this context is just a string, so you could pass it in any string:

    Code:
    string message = string.Format("Original number: \n Digits Reversed:"
                                         + " \n Number of Digits: {0}", num, numReverse, numofDig);
    MessageBox.Show(message);
    I assume that you were confused because you didn't know how to format a string, unless it was in the WriteLine method. The method string.Format returns a formatted string in the way that you were hoping that Console.WriteLine would.

    Console.WriteLine is only used to actually write output to the console. If you wanted to output your message to the console, and put it in a MessageBox, you could just take "message" and write it to the console as well:

    Code:
    Console.WriteLine(message);

  6. #6
    Join Date
    Mar 2010
    Posts
    4

    Re: Need help with a program. Pls

    Thanks for the help guys! I am still smoothing the wrinkles out right now, but hopefully I'll get it fine tuned. If not I know where to come for help.





    Thanks,

    Chris

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