Home > DeveloperSection > Forums > Using set in a C# class
Ashish Pandey

Total Post:128

Points:898
Posted on    April-06-2013 2:04 AM

 C# C# 
Ratings:


 2 Reply(s)
 900  View(s)
Rate this:
Hi Expert!

I have the following class:

public class Class1
{
    private int pam1;

    public Class1()
    {

    }

    public void ChangePam1(int _NewValue)
    {
        UpdatePam1(_NewValue);
        pam1 = _NewValue;
    }

    public int Pam1
    {
        set { this.pam1 = value; }
        get { return this.pam1; }
    }
}

Currently, when I want to change the value of Pam1, I do the following:

int n = 500;
Class1 c1 = new Class1();
c1.ChangePam1(n);
How can I change it using Set?


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-06-2013 9:10 AM

Hi Ashish!

Try as below

You can do this (It will call the set):

public class Class1
{
    private int pam1;

    public Class1(){}

    private void ChancePam1(int newValue)
    {
        UpdatePam1(newValue);
        pam1 = newValue;
    }

    public int Pam1
    {
        set { ChancePam1(value); }
        get { return this.pam1; }
    }
}
Then:

int n = 500;
Class1 c1 = new Class1();
c1.Pam1 = n;

It may be resolve your problem


Modified On Apr-06-2013 09:27:00 AM

Vijay Shukla

Total Post:100

Points:700
Posted on    April-06-2013 9:25 AM

Hi!

Try this way 

c1.Pam1 = n;
Or you can use;

public int Pam1
{
    set { ChancePam1(value); }
    get { return this.pam1; }
}

Don't want to miss updates? Please click the below button!

Follow MindStick