[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-10-14
2
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 320 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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