Is it possible to write such a methode?
-
I want to write a methode where i can deliver named parameter like this
private void MethodeName( aVar = 10, bVar = "20", cVar = 30, = , ... ){}
where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)
-
I want to write a methode where i can deliver named parameter like this
private void MethodeName( aVar = 10, bVar = "20", cVar = 30, = , ... ){}
where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)
Standard CLR languages do not support this, but nothing can stop you from creating a new language and a compiler for it.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
I want to write a methode where i can deliver named parameter like this
private void MethodeName( aVar = 10, bVar = "20", cVar = 30, = , ... ){}
where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)
I don't think you can write that in C#. Attributes are a special case I suppose. Why would you need such a method anyway? :confused:
MarkPhB wrote:
where aVar, bVar and cVar are properties of the class.
Do you mean properties as in get-set or just fields. In the first case, you could just use the properties as they were designed to be used: myClass.aVar = 10, etc...It's a good practice in this case to capitalize the first letter of the property name. In the second case, you could encapsulate the field in a property get-set and use it as described in the first case.
-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
-
I want to write a methode where i can deliver named parameter like this
private void MethodeName( aVar = 10, bVar = "20", cVar = 30, = , ... ){}
where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)
Based on your example code, you are showing that you want to provide default values to the function parameters when you define the method. To do this you need to provide different overloads to the function. Based on your explanation, you want to call a method and pass property initializers to it. You can do something close, but you will need to wait until the .NET Framework v3.5 is released (or use the Beta 2 release). The reason I say "close" is that you can only do this on object creation. For example, if you have a class
public class UserProfile
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
pubic UserProfile() { }
}You can create a new instance like this
UserProfile profile = new UserProfile
{
UserId = 123
FirstName = "John"
LastName = "Smith"
};Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
I want to write a methode where i can deliver named parameter like this
private void MethodeName( aVar = 10, bVar = "20", cVar = 30, = , ... ){}
where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)
However you can create a method with an undefined number of arguments :
private void MyBike(string one, params object[] BikeParts) { foreach (Object BikePart in BikeParts) { } }
however you cannot assign names to them and they must be of the same type.: I love it when a plan comes together :. http://www.zonderpunt.nl
-
I want to write a methode where i can deliver named parameter like this
private void MethodeName( aVar = 10, bVar = "20", cVar = 30, = , ... ){}
where aVar, bVar and cVar are properties of the class. I saw know that at Attributes....and i hope there is a way to achieve this with normal methods. If not then i need to know how can i do something similar. thx :-)
This does not look like acceptable syntax. In C# you always should specify the parameter type in the declaration. C# does not support default parameters. If you intend your sample to mean a method call, private void are irrelevant qualifiers. I don't thing C# supports method calls with this syntax.
Rudolf Heijink