Solved

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

Posted on 2004-10-14
2
154 Views
Last Modified: 2010-04-24
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
Comment
Question by:jade03
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 80 total points
Comment Utility
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
 

Author Comment

by:jade03
Comment Utility
Thanx, drichard for the clarification! :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now