Help a newbie with method.method?

S

Steve Thackery

I'm teaching myself C# using "C# in easy steps" by Tim Anderson, which seems
pretty good for a complete newbie.

But I'm puzzled by this piece of example code:

string theColor = textBox1.Text.Trim().ToUpper();

My understanding of textBox1.Text is that textBox1 is an object, and Text is
a property of that object. This seems straightforward so far. I also
appreciate that textBox1 has a number of methods, too, such as Dispose, so
that I could write:

textBox1.Dispose.

But Trim() is obviously a method, and the way it's written suggests that the
Text property has a Trim() method.

But how can a property (Text) have its own method? I thought methods and
properties both belonged to classes.

Is .Text actually an object, hidden away inside textBox1 and pretending to
be a property? Perhaps I could get may brain around that, but.....

Even more confusing for me is ToUpper(). This seems to belong to the Trim()
method, so one method has another method. Totally confusing!

I thought the '.' notation was only used in a hierarchical way, such as:

form.control.method
or
namespace.form.control.property

The quoted example seems to be:

control.property.method.method

....which makes no sense to me at all. Or is it just a free-for-all and I
can string as many methods in a row as I like:

control.property.method.method.method.etc....

If so, does the order matter?

I must be missing something really obvious about the '.' notation. Can
anyone illuminate me?

Many thanks!

SteveT
 
B

Bob Johnson

Strings in .net are objects.

This goes for any string - whether a string literal or a string-typed
property [of a class].

Try this:

string name = "Steve";

string x = name.ToUpper();
string y = "Steve".ToUpper();

x and y will each hold "STEVE" at this point.

Another idea that will help is that in C# you can "chain" methods together -
provided, of course, that all of the methods belong to the class in
question. In our case that would be the string class.
Consequently, the following is perfectly valid:

bool z = name.Trim().ToUpper().StartsWith("S");
the above is equivalent to the following:

name = name.Trim();
name = name.ToUpper();
bool z = name.StartsWith("S");

-HTH
 
L

Lew

Morten said:
The Text property will actully return a string object (System.String),
which in turn has a Trim method. Calling string.Trim() will return
another string object. The string object also has a ToUpper() method
returning yet another string,

If I may add to that - the dot notation applies to the result of the method
invocation. A member, a property and a method result could any or all of them
be objects, in turn having their own members, properties and methods.

Dot is simply a binary operator, like +. For example, assume three methods
that return an int - foo(), bar(), baz().

int result = foo() + bar() + baz();

That seems pretty normal, right?

It's pretty much the same deal with dot.
 
G

Guest

Lew said:
If I may add to that - the dot notation applies to the result of the
method invocation. A member, a property and a method result could any
or all of them be objects, in turn having their own members, properties
and methods.

Dot is simply a binary operator, like +.

Actually, it's not an operator.

As an operator it would take the form:

operand . operand

However, what follows the dot has to be an identifier:

operand . identifier

You can for example not use a variable after the dot:

int len;
len = "asdf".Length; // works just fine
string theory = "Length";
len = "asdf".theory; // doesn't work
 
S

Steve Thackery

Morten, Lew, Goran and Bob,

Thank you for your help. You have explained it perfectly and all has become
clear.

I really appreciate you taking the time.

Thanks again,

Steve
 
D

donna.gravell

To be fair, in 7.5.4 (member access) of the MS spec it is cited more
as a token than an operator. In the ECMA spec [334, 3rd edition]
section 17.9 (operators) it isn't mentioned; in 10.4 (members) if the
same it is again referred to as a token. Regardless of the semantic,
it really does represent a different scenario to an operator
(regardless of whether you call it unary or binary).

I'm just saying...

Marc
 
L

Lew

To be fair, in 7.5.4 (member access) of the MS spec it is cited more
as a token than an operator. In the ECMA spec [334, 3rd edition]
section 17.9 (operators) it isn't mentioned; in 10.4 (members) if the
same it is again referred to as a token. Regardless of the semantic,
it really does represent a different scenario to an operator
(regardless of whether you call it unary or binary).

Except for the fact that it is an operator.

Every token in the language is a token, whether it represents an operator, a
keyword or whatnot. "Token" is just the first step in parsing - symbols are
gathered into distinct tokens. I think you've misunderstood the meaning of
the word "token".

No operator can take arbitrary data types. Absent an overload, have you tried
to apply plus (+) to an object type?

Dot is also an operator in C, c++ and Java.

Since dot is clearly defined by Microsoft as an operator in their own
literature, and is listed in the operator precedence table, and is an operator
in the family of related languages, and the word "token" is for a completely
different purpose, I suggest that you revise your understanding.
 
M

Marc Gravell

I suggest that you revise your understanding.
Happily; in all seriousness, I do take an interest in this sort of
thing; I would be genuinely interested to read a clarification on this
point, ideally in the ECMA spec.
I think you've misunderstood the meaning of the word "token".
No, I just wasn't elevating it to the term "operator" since the
sections cited don't refer to it as an "operator". By 9.4.5, I guess
it must be either an "operator" or "puctuator", and since it isn't
defined in the list of operators, it could equally be "punctuator",
although this term is not fully explored in the spec.
No operator can take arbitrary data types.
I didn't mention data types.
Dot is also an operator in C, c++ and Java.
I'm happy for them
Since dot is clearly defined by Microsoft as an operator in their own
literature
You'll permit me to quote ECMA, though? Looking at 17.9 (operators)
and 14.5.4 (member-access), I can't see anything that would state it
as an operator.
and is listed in the operator precedence table
I'll guess, then, that it means that member-access takes precedence
over any operator; that doesn't really imply that member-access *is*
an operator.

To be honest, it is a fairly academic language point, but I would be
interested in your thoughts; again, I stress that in the above I'm
just trying to explain my view-point. I'm not saying "nah, can't be",
I'm just inviting you "show me...".

And I am not a [.net language] lawyer ;-p

Marc
 
M

Marc Gravell

Minor additional; in the precedence table, it is "a.b" that appears,
not "." - compare this to every other entry in the table that you
might refer to as an operator, i.e. "+", "-" which are not "a+b", "a-
b". Which hints again to it referring to "a.b" member-access as an
expression taking precedence over the operators, but doesn't (to me at
least) state that dot is classed as an operator in this context.
 
L

Lew

Marc said:
Minor additional; in the precedence table, it is "a.b" that appears,
not "." - compare this to every other entry in the table that you
might refer to as an operator, i.e. "+", "-" which are not "a+b", "a-

Except auto-{inc,dec}rement and the cast operators, which show whole
expressions in that table also.
b". Which hints again to it referring to "a.b" member-access as an
expression taking precedence over the operators, but doesn't (to me at
least) state that dot is classed as an operator in this context.

And yet the operator-precedence table clearly lists dot in the section
entitled "operators".

I think it's valid to call dot an operator; Microsoft certainly has no qualms
about it. Even the ECMA spec is not entirely innocent of this usage.

Dot is an operator.
 
M

Marc Gravell

I think it's valid to call dot an operator; Microsoft certainly has no qualms
about it. Even the ECMA spec is not entirely innocent of this usage.

I'm happy to agree with you - I'm just trying to highlight that it is
not entirely clear purely from the spec. You need to infer a lot,
which is unfortunate. For instance, I just found this gem in 14.2.2:

<q>Only the operators listed above can be overloaded. In particular,
it is not possible to overload member
access, method invocation, or the...</q>

Which is indeed including member-access and method-invocation in the
scope of operators. If anything, the combined specs are both a little
lax and conflicting on the subject, with us having to read between a
lot of lines, rather than simply referring to section {blah}. As for
it being <q>simply a binary operator, like +.</q>, well - the
treatment of an identifier for the second operand is clearly quite a
bit different than for a typical operator, but the left-associativity
remains.
Dot is an operator.

If you like ;-p
At the end of the day, the behavior is "as expected", which will be
good enough for me...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top