Link to home
Start Free TrialLog in
Avatar of jade03
jade03

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jade03
jade03

ASKER

Thanx, drichard for the clarification! :)