Solved

funny error with putting values into char

Posted on 2004-11-02
148 Views
Last Modified: 2010-04-24
Hello all,

I have the following code:

char* dest3 = new char(textBox1->Text->Length);
for(int f=0; f < textBox1->Text->Length; f++){
     dest3[f] = (char)textBox1->Text->Chars[f];
     MessageBox(NULL, dest3, "inside", 0x00010);
}

I need to put the value of a text box into a char* which I then use later on. I had this code workgin this afternoon but have just tried it again and it does not work. it works when the textbox->text->length is short. I have debugged it with the message box and it seems to error when f gets to around 17. It says that "Unhandled exception....[...]...Object reference not set to an instance of an object"
Im sure this was working before for paths of any size not just those below 16/17. I had this error yesterday but it seemed to disappear this morning.
Any ideas?

Cheers
Richard
0
Question by:richjo100
    3 Comments
     
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    I guess it is something like this:

    char* dest3 = new char[textBox1->Text->Length+1];
    for(int f=0; f < textBox1->Text->Length; f++){
         dest3[f] = (char)textBox1->Text->Chars[f];
    }
    dest3[f] = 0;  // Close string with a null character
    MessageBox(NULL, dest3, "inside", 0x00010);   // Put messagebox outside the for loop

    // Remember: somewhere you have to delete dest3 after using
    0
     
    LVL 16

    Accepted Solution

    by:
    char* dest3 = new char[textBox1->Text->Length + 1]; //an extra char for the terminating NULL;
    ZeroMemory(dest3, textBox1->Text->Length + 1);        //fill with null chars

    for(int f=0; f < textBox1->Text->Length; f++){
         dest3[f] = (char)textBox1->Text->Chars[f];
         MessageBox(NULL, dest3, "inside", 0x00010);
    }

    //and (as previously said) you'll have to "delete[] dest3;"
    0
     

    Author Comment

    by:richjo100
    Thanks for the help guys!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    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 Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    884 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

    21 Experts available now in Live!

    Get 1:1 Help Now