Simple Get/Set Question

D

Davej

How do you use get/set in the manner of normal methods?

I mean...

if ( m.getX() > 10)
{
....
}

but...

if ( m.X > 10)
{
....
}

does not compile.
 
M

Matt

How do you use get/set in the manner of normal methods?

I mean...

if ( m.getX() > 10)
{
...

}

but...

if ( m.X > 10)
{
...

}

does not compile.

It depends. If X is a property, which is what get/set is designed for,
then:

public int X { get; set; }
if ( m.X > 10 )
{
}

should compile fine. How are you trying to use it? I'm guessing you
have it as
a private variable.

Matt
 
D

Davej

It depends. If X is a property, which is what get/set is designed for,
then:

public int X { get; set; }
if ( m.X > 10 )
{

}

should compile fine. How are you trying to use it? I'm guessing you
have it as
a private variable.

Matt

I'm not sure what I did but I will go back and try to re-create it.

So if I say...

m.X; // this will execute a setX?

if ( m.X > 0) y = 100; // this will execute a getX?

Thanks!
 
D

Davej

Ok,

m.X;

....is not accepted as valid, but...

if ( m.X > 10) {...}

....works...

I thought neither had worked, but apparently I had mistyped something
somewhere.

Thanks!
 
M

Matt

Ok,

m.X;

...is not accepted as valid, but...

Correct, the statement doesn't do anything. It is actually calling the
get version.
m.X = 10; would call the set.

C# doesn't allow do nothing statements, so the compiler complains.
if ( m.X > 10)  {...}

...works...

I thought neither had worked, but apparently I had mistyped something
somewhere.

Probably just a typo.

Matt
 

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