Home > Software engineering >  Cannot get String Length due to its type in C
Cannot get String Length due to its type in C

Time:09-30

I am trying to build a C /CLR form app
And in the code below
I am trying to get the length of the string of GetTb
Which is a string retrieved from a textbox Tb_Return

Tb_Return->Text = "This is a test String";
String^ GetTb = Tb_Return->Text;
int len = GetTb.Length();
Tb_Return->Text = GetTb;

In Line 3, Visual Studio keeps highlighting the GetTb variable with the error below

expression must have class type but it has type

And when I try to change the . into ->, the error message changes to below

expression preceding parentheses of apparent call must have have (pointer-to-) function type

How can I fix these errors?

CodePudding user response:

There are 2 issues:

  1. GetTb is a handle to an object in c /cli.
    In order to dereference it you must use the -> operator (or * with . similarly to c pointers).
  2. System::String::Length is a property.
    In order to access it you need to use its name without parentheses (()).

Therefore the correct way to access the Length property of GetTb is:

int len = GetTb->Length;
  • Related