-2

I'm creating my first program on Winform with C++. I have a function and a string input. However, when I run the program, I get the following error with text.size(): "Error C2228 left of '.size' must have class/struct/union"

Relevant code:

private: System::Void bntGet_Click(System::Object^  sender, System::EventArgs^  e) {
    in_itext = txtText->Text;
    itext = Convert::ToString(in_itext);
        for (Int32 index = 1; index <= itext.size(); index++)
        {
            if (itext[index] == '@')
            {

                while (itext[index - 1] != ' ')
                {
                    index--;
                }
                while (itext[index] != ' ')
                {
                    next_email += itext[index];
                    index++;
                    if (itext[index] == '\0')
                        break;
                }
                break;

            }
        }
    txtEmail->Text = next_email;
}

Any suggestions? Thanks everyone!

vich
  • 11,836
  • 13
  • 49
  • 66
  • Can you post a little more code? – logixologist May 19 '17 at 18:26
  • 1
    In addition to missing [MCVE] this post is very strangely tagged with C# when post says code in C++... – Alexei Levenkov May 19 '17 at 18:28
  • Possible duplicate of [error C2228: left of '.x' must have class/struct/union](http://stackoverflow.com/questions/20905299/error-c2228-left-of-x-must-have-class-struct-union) – NineBerry May 19 '17 at 18:29
  • C++/CLI is the correct tag if you are working in Windows Forms (and you picked C++ as the option). This is *not* standard C++, but a dialect with extensions to work with .Net. You need to show more code for us to be able to help you. – crashmstr May 19 '17 at 18:30
  • In C++/CLI, .Net objects are declared with a `^` and are allocated with `gcnew`. You use the `->` syntax to access properties, fields, and methods. – crashmstr May 19 '17 at 18:34
  • this is a my part of main code https://pastebin.com/vBtu63cN – Life Change May 19 '17 at 18:35
  • this is a firt time i have uploaded a question, i will try my best for the next time. – Life Change May 19 '17 at 18:37
  • when i use -> syntax to access i have get an error that "Error C2039 'length': is not a member of 'System::String'" – Life Change May 19 '17 at 18:40

1 Answers1

1

Standard warning: While it's certainly possible to write the main body of your application in C++/CLI, or even write the GUI in C++/CLI using WinForms, it is not recommended. C++/CLI is intended for interop scenarios: where C# or other .Net code needs to interface with unmanaged C++, C++/CLI can provide the translation between the two. For primary development, it is recommended to use C# with either WinForms or WPF if you want managed code, or C++ with MFC if you want unmanaged.


That said:

for (Int32 index = 1; index <= itext.size(); index++)

and

"Error C2039 'length': is not a member of 'System::String'"

Size is not how you get the length of a System::String in .Net. For that, you want the Length property. You were close on your second attempt, but you need a capital "L". (size() is how std::string does it, but std::string is a completely different beast from System::String.)


Other issues:

in_itext = txtText->Text;
itext = Convert::ToString(in_itext);

It's already a string, you don't need to convert it.

while (itext[index - 1] != ' ')
{
    index--;
}

You can easily run off the beginning of the string if you do this. (You'll get an exception when index is equal to zero.) Add a check against zero.

while (itext[index] != ' ')
{
    next_email += itext[index];
    index++;
    if (itext[index] == '\0')
        break;
}

Strings in .Net are not null-terminated, you need to check against the Length.


If we take a step back, what problem are you actually trying to solve here? My best guess is that you've got some text with an email address somewhere in the middle, and you just want to extract the email address. If that's the case, then I'd probably try using a regular expression to extract the email address. You've got the full power of the .Net Library available to you, use it! See Using a regular expression to validate an email address for the regex to use, and read up on regular expressions with the introduction page and class reference.

Community
  • 1
  • 1
David Yaw
  • 27,383
  • 4
  • 60
  • 93
  • I have tried to swallow it again and again but i don't understand what you said about length property. I have tried one of things such as: 1. add std::string on left of length() 2. change System::EventArgs^ e became std::String^ e 3. add string library but not success please give me an attempt @David Yaw – Life Change May 20 '17 at 03:14
  • Thank you @David Yaw and everyone who have solved and explained this problems for me very much. After i changed itext->Length it work. – Life Change May 20 '17 at 05:19