String::Format problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek =?utf-8?Q?Kuli=C5=84ski?= / takeda

    String::Format problem

    I spent many hours on this, googling around and experimenting, and still
    nothing.

    I wrote an umanaged program (actually various classes), while I was
    developing them I used command line to test it.
    Now, I decided to make GUI for it, so I created new project: "Windows Forms
    Application (.NET)" (I was trying to use something that's not managed (e.g.
    MFC, but I learned quickly that I don't actually know how to use MFC :)
    In .NET I was able to quickly create the GUI that looked as I wanted it too
    look.

    Now my problem is really silly. While I figured out how to convert
    System::String to for example std::string, and looks like the other way
    around is actually easy, I can't for a love of God make String::Format do
    what I want.
    Even the examples from MSDN doesn't work when I copy&paste them verbatim!
    Here is an example:
    Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see Get started with the String.Format method for a quick overview.


    In a simple program that has just textbox, button and label, the following
    code works:
    label1->Text = String::Format( "Hello, {0}", textBox1->Text);

    But this, doesn't want to:
    label1->Text = String::Format( "Hello, {0}", 10);

    My goal is basically do something like this:
    unsigned short int pc = 8000;
    label1->Text = String::Format( "Address: {0,4:X}", pc);

    Anyone has idea what I'm doing wrong?

    I have also an unrelated question: If I have a managed class (for the
    form), and I define inside a pointer to an unmanaged class, is that a good
    practice? Right now it appears to work fine, but before I had an exception
    that the pointer is null (even though I did new on the form's load, and
    pointer was used after selecting an option from a menu). I have no idea how
    I even fixed it.

    Thanks for help.
    --
    ダレック
  • Ben Voigt [C++ MVP]

    #2
    Re: String::Format problem

    My goal is basically do something like this:
    unsigned short int pc = 8000;
    label1->Text = String::Format( "Address: {0,4:X}", pc);
    Would this be sufficient for you?
    label1->Text = "Address: " + pc.ToString("X4 ");
    >
    Anyone has idea what I'm doing wrong?
    >
    I have also an unrelated question: If I have a managed class (for the
    form), and I define inside a pointer to an unmanaged class, is that a good
    practice? Right now it appears to work fine, but before I had an exception
    that the pointer is null (even though I did new on the form's load, and
    pointer was used after selecting an option from a menu). I have no idea
    how
    I even fixed it.
    That is the right way to use native classes from managed code. Often, you
    create a managed class to wrap each native object, and its only
    responsibility is taking care of that one native pointer.
    >
    Thanks for help.
    --
    ????

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: String::Format problem


      [snip]
      Maybe there's some option enabled in my VC++ (it is from VS 2003), but
      which option is responsible for that?
      [snip]
      I'm really wondering what's wrong with my C++ compiler. Even examples on
      MSDN webpage didn't work.
      You need an upgrade to VC 2005, even Express Edition will work. Managed
      Extensions for C++ that came with VC 2003 were so full of problems that
      Microsoft started over in VC 2005, giving us the C++/CLI language.


      Comment

      • Derek =?utf-8?Q?Kuli=C5=84ski?= / takeda

        #4
        Re: String::Format problem

        On Tue, 4 Sep 2007 11:13:07 -0500, Ben Voigt [C++ MVP] wrote:
        You need an upgrade to VC 2005, even Express Edition will work. Managed
        Extensions for C++ that came with VC 2003 were so full of problems that
        Microsoft started over in VC 2005, giving us the C++/CLI language.
        Actually one of the readers here (I don't know if he wants me to mention
        his name since he answered privately) pointed me to use a __box() around
        basic types like integers etc. which seems to do the thing. I can put char*
        in String() and it seems to solve most of the problems.
        He also recommended me to switch to VS 2005, and I'll try it as soon as I
        can.

        Anyway, thank you Ben for troubleshooting the problem, I really appreciate
        that you tried to help.
        I have another question about creating virtual listbox, but I'll create
        another thread for it, since it is unrelated.

        --
        ダレック

        Comment

        Working...