Drahcir
Drahcir

Reputation: 11972

Get & Set Properties

I have a problem with my code posted below, set and get methods. I want to call it like this this.LastError.Set(1);

But it gives me this error: 'int' does not contain a definition for 'Set' and no extension method 'Set' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

public class MyClass
    {
        private int ERROR_NUM = 0;
        public int LastError
        {
            get { return ERROR_NUM; }
            set { ERROR_NUM = value; }
        }

        bool IsLoaded()
        {
            int count = Process.GetProcessesByName("AppName").Length;
            if (count == 1) return true;
            if (count > 1) this.LastError.Set(1);
            return false;
        }
    }

I know this is probably a dumb question so sorry for that, I've been fighting with this thing for a couple hours now and I've even gone so far as to try and give the LastError its own class. This is my first day on C#.

Upvotes: 1

Views: 688

Answers (9)

Rafael Piccolo
Rafael Piccolo

Reputation: 2338

You are working with a property. Properties encapsulates the Get and Set method. You don't call the Set method, instead you set the value directly to the property.

this.LastError = 1; 

Internally the object will call the Set method. A property is like a mix between a field of the class and a method: it looks like a field, but in reality it will trigger a method. Code is much simpler and easier to read with properties. Maybe you are used to other languages like Java that don't have the concept of properties, forcing you to explicitly call the Get and Set methods.

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44316

The idea of properties is they act like they are fields on your class. Meaning you just get and set them like you would a normal variable Property = X; var x = Property

Upvotes: 0

svick
svick

Reputation: 244998

The whole point of properties is that they look like fields, you can get and set them like fields:

this.LastError = 1; // set the value
int lastError = this.LastError; // get the value

The property is compiled as two methods, set_LastError() and get_LastError(). You can't use them from C#, but the compiler can and compiles the code above to something like:

this.set_LastError(1);
int lastError = this.get_LastError();

Upvotes: 2

Dylan Smith
Dylan Smith

Reputation: 22255

In C# you would set it like this:

this.LastError = 1;

Upvotes: 0

N_A
N_A

Reputation: 19897

To use a property you don't actually reference the get and set members, you just use the property directly like so:

this.LastError = 1;

int lastErrorNum = this.LastError;

based on whether you are getting or setting the property, the appropriate get or set portion will be used.

Upvotes: 0

Glory Raj
Glory Raj

Reputation: 17701

just do like this ..

 this.LastError = 1; 

Upvotes: 2

Tomasz Jaskuλa
Tomasz Jaskuλa

Reputation: 16033

You don't need to do this. Just set it like this

this.LastError = 1;

That's all

Upvotes: 1

Miyagi Coder
Miyagi Coder

Reputation: 5532

To set a property in C# simply use the "=" operator.

this.LastError = <myvalue>;

Upvotes: 1

George Johnston
George Johnston

Reputation: 32278

Just set it.

this.LastError = 1;

Upvotes: 6

Related Questions