开发者

Is there a way to pass an argument to a setter

开发者 https://www.devze.com 2023-03-08 12:07 出处:网络
Is there a way to pass an argument to a setter? How would I pass a string into the setter below? How would I call the s开发者_如何学Goetter with the new string param?

Is there a way to pass an argument to a setter? How would I pass a string into the setter below? How would I call the s开发者_如何学Goetter with the new string param?

public string It
{
   get{ { return it;}

   set { it = value;}
}

Thank you very much


The setter gets value as its own "argument", based on the value you assign to the property:

foo.It = "xyz"; // Within the setter, the "value" variable will be "xyz"

If you want to use an extra parameter, you need an indexer:

public string this[string key]
{
    get { /* Use key here */ }

    set { /* Use key and value here */ }
}

You'd then access it as

foo["key"] = "newValue";

You can't give indexers names in C#, or use named indexers from other languages by name (except in the case of COM, as of C# 4).

EDIT: As noted by Colin, you should use indexers carefully... don't just use them as a way of getting an extra parameter just for the setter, which you then ignore in the getter, for example. Something like this would be terrible:

// Bad code! Do not use!
private string fullName;
public string this[string firstName]
{
    get { return fullName; }
    set { fullName = firstName + " " + value; }
}

// Sample usage...
foo["Jon"] = "Skeet";
string name = foo["Bar"]; // name is now "Jon Skeet"


You can assign it like you can assign a value to a variable:

It = "My String";

Property getters/setters are just syntactic sugar for string get_It() and void set_It(string value)


Properties do not admit arguments in C#.

If you really need aditional information in order to se It correctly then the recommended solution is to implement the setter as a method:

public void SetIt(string value, string moreInfo) {...}


Generally, for any property directly we can assign the value i.e., It = "";


To compliment what everyone else has said in this aged thread...

Perhaps a better method would be to define a struct to hold the extra information you want to pass, then pass the data this way. For instance:

struct PersonName {

    public string Forename { get; set; }
    public string Surname { get; set; }

    public PersonName(string fn, string sn) {
        Forename = fn;
        Surname = sn;
    }

}

class MyPerson {

    public string Forename { get; set; }
    public string Surname { get; set; }
    public DateTime dob { get; set; }

    ...

    public PersonName Fullname {
        get { return Forename + " " + Surname; }
        set {
            Forename = value.Forename;
            Surname = value.Surname;
        }
    }
}

...

    public void main() {
        MyPerson aPerson = new MyPerson;
        aPerson.Fullname = new PersonName("Fred", "Bloggs");
    }

Personally, though, I think this is overkill for something so minor. Also, it then beggars the question - why wasn't the Forename, Surname pair already defined as the appropriate struct (PersonName)?


That's the basics of C# properties:

Properties (C# Programming Guide)

Lets say you create an object blah that has that property:

Blah blah = new Blah();
blah.It = "Hello World";
String hey = blah.It;

The idea of properties is to wrap the calls to local variables with some more logic (and some hiding). So the syntax is similar to working with a local class variable

0

精彩评论

暂无评论...
验证码 换一张
取 消