same Enum and property

T

Tony Johansson

Hello!

Here I have some simple code below that I have run into. I have never seen
this construction before.
I have two questions about it.
My first question is: Can somebody explain how this work. Here the enum type
Cars is the same as
the property name Cars.
My second question is: Why is it not possible to step into the property Cars
when I do
myCar.val = (Cars)7;
in main

enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}

//Tony
 
J

Jeff Johnson

Here I have some simple code below that I have run into. I have never seen
this construction before.
I have two questions about it.
My first question is: Can somebody explain how this work. Here the enum
type Cars is the same as
the property name Cars.
My second question is: Why is it not possible to step into the property
Cars when I do
myCar.val = (Cars)7;
in main

enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}

1) Whether you're using the enum or the property depends on how the item is
used and also how it is qualified. If you have a variable of type Car,
<variable>.Cars is obviously referring to the property. On the other hand, x
= (Cars)12 is using the enum, since the Cars property is not a type and
cannot therefore be used in a cast.

2) Because in that code you're referencing the field (val), not the property
(Cars).

What exactly are you trying to do?
 
P

Patrice

This is valid as the Cars property is inside the Car struct. So names are
not conflicting (you have MyProject.Cars which is the enum and
MyProject.Car.Cars which is a property inside the Car struct).

Inside void Main Cars is not the property. This the enum (place your mouse
on Cars and you'll see the declaration toolip), you'll need to use
myCar.Cars to access the property...
 
T

Tony Johansson

Hello!

This code set myCar.val to 100 in main.
I can't understand how the property Cars in struct Car can get called when I
do
myCar.val = (Cars)7;
in main.

Can somebody explain that ?


enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}


//Tony

Patrice said:
This is valid as the Cars property is inside the Car struct. So names are
not conflicting (you have MyProject.Cars which is the enum and
MyProject.Car.Cars which is a property inside the Car struct).

Inside void Main Cars is not the property. This the enum (place your mouse
on Cars and you'll see the declaration toolip), you'll need to use
myCar.Cars to access the property...

--
Patrice


Tony Johansson said:
Hello!

Here I have some simple code below that I have run into. I have never
seen this construction before.
I have two questions about it.
My first question is: Can somebody explain how this work. Here the enum
type Cars is the same as
the property name Cars.
My second question is: Why is it not possible to step into the property
Cars when I do
myCar.val = (Cars)7;
in main

enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}

//Tony
 
P

Peter Duniho

Hello!

This code set myCar.val to 100 in main.
I can't understand how the property Cars in struct Car can get called
when I do
myCar.val = (Cars)7;
in main.

Can somebody explain that ?

No, because it can't. Given the line of code you show, the property Cars
is not used at all.
 
H

Harlan Messinger

The same way you would if there *weren't* an enum with the same name:
myCar.Cars .

Tony said:
Hello!

This code set myCar.val to 100 in main.
I can't understand how the property Cars in struct Car can get called when I
do
myCar.val = (Cars)7;
in main.

Can somebody explain that ?


enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}


//Tony

Patrice said:
This is valid as the Cars property is inside the Car struct. So names are
not conflicting (you have MyProject.Cars which is the enum and
MyProject.Car.Cars which is a property inside the Car struct).

Inside void Main Cars is not the property. This the enum (place your mouse
on Cars and you'll see the declaration toolip), you'll need to use
myCar.Cars to access the property...

--
Patrice


Tony Johansson said:
Hello!

Here I have some simple code below that I have run into. I have never
seen this construction before.
I have two questions about it.
My first question is: Can somebody explain how this work. Here the enum
type Cars is the same as
the property name Cars.
My second question is: Why is it not possible to step into the property
Cars when I do
myCar.val = (Cars)7;
in main

enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}

//Tony
 
T

Tony Johansson

Hello!

I use VS2005

When I run the code and use the debugger and look at object myCar which is
of type Car I can see that
property Cars have value 100 and val have value 7.
If I do it like this it's easy to understand it
Car myCar;
int test = myCar.Cars;

but when I do
Car myCar;
mycar.val = (Cars)7;

I can't understand when property Cars get called

//Tony
 
P

Peter Duniho

[...]
but when I do
Car myCar;
mycar.val = (Cars)7;

I can't understand when property Cars get called

At this point, I am sitting here wondering how many times you'll have to
be told that the code you are posting doesn't use the Cars property at
all, before you'll understand that the code you are posting doesn't use
the Cars property at all.

Your statement "I can't understand when property Cars get [sic] called"
makes very little sense, because the property Cars is never called at
all. How can anyone "understand" something that doesn't happen at all?
Of course you "can't understand" when the property Cars is called...it's
not called, and thus there is nothing to "understand".

I might as well say "I can't understand why the sum of 5 plus 5 is 12".

Pete
 
J

Jeff Johnson

but when I do
Car myCar;
mycar.val = (Cars)7;

I can't understand when property Cars get called

IT DOESN'T GET CALLED AT ALL THEN! THAT'S WHAT WE'VE BEEN TRYING TO TELL
YOU!
 
F

Family Tree Mike

Tony said:
Hello!

This code set myCar.val to 100 in main.
I can't understand how the property Cars in struct Car can get called when I
do
myCar.val = (Cars)7;
in main.

Can somebody explain that ?


enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}


//Tony

Patrice said:
This is valid as the Cars property is inside the Car struct. So names are
not conflicting (you have MyProject.Cars which is the enum and
MyProject.Car.Cars which is a property inside the Car struct).

Inside void Main Cars is not the property. This the enum (place your mouse
on Cars and you'll see the declaration toolip), you'll need to use
myCar.Cars to access the property...

--
Patrice


Tony Johansson said:
Hello!

Here I have some simple code below that I have run into. I have never
seen this construction before.
I have two questions about it.
My first question is: Can somebody explain how this work. Here the enum
type Cars is the same as
the property name Cars.
My second question is: Why is it not possible to step into the property
Cars when I do
myCar.val = (Cars)7;
in main

enum Cars {Volvo,Saab,Opel }

struct Car
{
public Cars val;

public int Cars
{
get { return 100; }
}
}

static void Main(string[] args)
{
Car myCar;
myCar.val = (Cars)7;
}

//Tony

Your property "val" is of type Cars, and your property "Cars" is of type
int. Setting your property "val" has nothing to do with the value
_always_ returned by the "Cars" property, which is _always_ 100.
 
P

Patrice

This code set myCar.val to 100 in main.

I tried your code and this is not what I see...
I can't understand how the property Cars in struct Car can get called when
I do
myCar.val = (Cars)7;
in main.

As said previously and as you expect yourself it shoudl'nt and when I run
the code it doesn't (.NET 3.5). If this is the exact code you likely make an
error in how you inspect the myCar.val value (which is 7 as expected here
not 100).
 
B

Ben Voigt [C++ MVP]

Jeff said:
IT DOESN'T GET CALLED AT ALL THEN! THAT'S WHAT WE'VE BEEN TRYING TO
TELL YOU!

Yes it does, just not by those two lines of code. Here is the important
fact:
When I run the code and use the debugger and look at object myCar
which is of type Car I can see that
property Cars have value 100 and val have value 7.

The debugger itself calls property getters in order to display property
values. For this reason property getters should not have side effects.
 

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