.NET syntax ??

P

Peter

Hi,

In recent .NET code I found a variable/reference like

item1.item2.item3

From my .NET courses I try to understand the meaning of this
....

Of course item1 must be a class/object. But what are the
possible meanings of item2.item3.

Does that mean that item2 is a nested class (inner class) in
class item1, or are there more explanations ??

E.g. item2 might be a property, but a property can't have
member/fields, so in that case item2.item3 is incorrect .NET
syntax ...

Or an interface maybe ??

Right ?

Still struggling with .NET code,

Some help would be nice.

Thanks,
P.
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

It depends on how it is being used. Generally though, if item1 is a
variable, then item2 is a property on that variable which exposes an
instance, and then item3 is a property on that result that is returned.

It could be used for static properties on types as well, where item1 is
a type, item2 and item3 are properties, but I would have to see the context.

Hope this helps.
 
C

Christopher Kimbell

item1; this is an instance of an object
item2; this is a property or public field of item1 that returns another
object
item3; this is a property or public field on item2

Properties are normally used to expose a private field, but can also return
a value that is calculated based on other internal values/states.

An interface only specifes what the signature of the class is, it doesn't
contain any code to return or perform calculations on any values.


Chris
 
P

Peter

variable, then item2 is a property on that variable which
exposes an
instance,

You mean that property item2 operates on an object of class
item1.

Does next code make sense:

public class MyClass {
MyItem _item2;
public MyItem item2
{
get
{
return _item2;
}
}
}

public class MyItem
{
int _item3;
public MyItem item3
{
set
{
_item3 = value;
}
}
}

And then in the Main code:

MyClass item1;
item1.item2.item3 = 25;

Not quite sure ;)

Peter
 
M

Morten Wennevik

Hi Peter,

Consider this:

class Items
{
public string item2;
}

now, if item3 is Length you can do

Items item1 = new Items();

Instead of doing

string s = item1.item2;
s.Length;

you can do

item1.item2.Length;

now, Length has a ToString() method so in effect you could do

item1.item2.Length.ToString();

In other words item1 has some properties, variables or methods in this
case one of them is item2.
item2 is another object with other properties variables or methods, in
this case one of them is item3.
You simply access public variables, properties, methods of the object
immediatly preceding '.' Any other '.' is irrelevant.

You mean that property item2 operates on an object of class
item1.

Does next code make sense:

public class MyClass {
MyItem _item2;
public MyItem item2
{
get
{
return _item2;
}
}
}

public class MyItem
{
int _item3;
public MyItem item3
{
set
{
_item3 = value;
}
}
}

And then in the Main code:

MyClass item1;
item1.item2.item3 = 25;

Not quite sure ;)

Peter

Yes, this is correct. It does not need to be properties.

int n = 12345;
n.ToString().Length.ToString()[0].ToString().Length;

is perfectly legal.
 
M

Mike Schilling

Peter said:
Hi,

In recent .NET code I found a variable/reference like

item1.item2.item3

From my .NET courses I try to understand the meaning of this

Perhaps it's simpler if you expand it. It's the same as temp2 in:

temp = item1.item2;
temp2 = temp.item3

That is, item2 is a property of item1. item1.item2 is itself an object
(everything in .NET is an object), and item3 is a property of *that* object.
 

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