Is there any way to pass an argument to the installer

Is there a way to pass a setter argument? How to pass a line to the setter below? How can I call setter with a new string parameter?

public string It { get{ { return it;} set { it = value;} } 

Thank you very much

+4
source share
6 answers

The installer receives 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 additional parameter, you need an index:

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

Then you will access it as

 foo["key"] = "newValue"; 

You cannot specify index names in C # or use named indexes from other languages โ€‹โ€‹by name (except in the case of COM starting with C # 4).

EDIT: As Colin pointed out, you should use indexers carefully ... not just use them as a way to get an extra parameter only 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" 
+10
source

You can assign it the same way you can assign a value to a variable:

 It = "My String"; 

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

+4
source

Properties do not allow arguments in C #.

If you really need more information for proper se It , then the recommended solution is to implement the installer as a method:

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

What are based on the properties of C #:

Properties (C # Programming Guide)

Suppose you created a blah object with this property:

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

The idea of โ€‹โ€‹properties is to wrap calls with local variables with some logic (and some concealment). Thus, the syntax is similar to working with a local class variable

+1
source

In the general case, we can directly assign a value to any property, i.e. It = "";

0
source

To compliment what everyone else said in this old thread ...

Perhaps the best way would be to define a struct to store the additional information that you want to pass, and then pass the data in 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, however, I think this is too much for something so insignificant. In addition, then he asks the question: why is the pair Forename , Surname already defined as a suitable struct ( PersonName )?

0
source

All Articles