Solved

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

Posted on 2004-10-14
2
157 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
ID: 12314817
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
ID: 12339126
Thanx, drichard for the clarification! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

17 Experts available now in Live!

Get 1:1 Help Now