• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

System::String* need initalization in order to get length? why code in .h not .cpp?

I have 2 questions...being new to the .NET envrironment I'm still trying to get used to all these formats and syntax issues:

I'm basically trying to build a GUI for users to input stuff, for now, I'm making it simple...just input things, and I'll grab it and output it back in a message box to check that it's ok....

1. I decided to use Windows Forms Application and noticed that after the GUI is designed using the GUI tools, I doubled clicked on a button to add an even handler, and the thing takes me to the .h (header) file where it creates a new function for me so I could input codes to do what I want.

I'm a traditional c++ programmer, and am not used to putting lots of messy code in the header...it just looks weird to me..is there any way to move it to the .cpp file and still have it work?

2. In my GUI I have many check boxes and radio buttons. I try to grab everything and concatenate them into one string to print out...in doing so I declared a string var:

// noticed I did not initialize it
System::String* str;

then I go to check which box(es) are checked and grab the text corresponding to the checked one:

if(checkCat->Checked)
{
   str = checkCat->text;
}
if(checkDog->Checked)
{
    // if cat is already checked, I want to add a comma between cat and dog in
   // the final str
   if(str->Length !=0 )
      str = System::Concat(str, S",");
 
  str = System::Concat(str, checkDog->text);
}

The program runs fine if I have either of the boxes checked. But if BOTH are checked, it catches an exception error...right at the line where I'm checking the Length.

So I'm thinking, should I have initialized str first before getting its length? If so, how should I go about doing it?

Any helps would be greatly appreciated.

jade
0
jade03
Asked:
jade03
1 Solution
 
drichardsCommented:
1. You can move methods off to the cpp file just as in a regular C++ app, but you have to be careful because you can render the form designer non-operational if you move the wrong stuff.  In any event, the form designed will be confused when you try to view the code.  For instance, if you move the code for a button click handler into the cpp file, double clicking on the button in the form designer will not take you to the code.  It's best to just leave things in the header when coding forms.


2. Looking at the code, it seems it would work as long as you have the cat box checked, as this is where the string gets initialized.  If you ONLY check dog, the string is not initialized and 'str->Length' should fail.  Try this instead:

    System::String *str = S"";
    if (checkCat->Checked)
    {
        str = checkCat->Text;
    }
    if (checkDog->Checked)
    {
        if (str->Length != 0)
        {
            str = System::Concat(str, S",");
        }
        str = System::Concat(str, checkDog->text);
    }

or

    System::String *str = NULL;
    if (checkCat->Checked)
    {
        str = checkCat->Text;
    }
    if (checkDog->Checked)
    {
        if (str != NULL)
        {
            str = System::Concat(str, S",");
            str = System::Concat(str, checkDog->Text);
        }
        else
        {
            str = checkDog->Text;
        }
    }
0
 
jade03Author Commented:
Thanx, drichard for the clarification! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now