How to assign values to a property whose name is in a string? [duplicate] - c#-4.0

This question already has answers here:
Set object property using reflection
(10 answers)
Closed 8 years ago.
I want to assign value to the property of the object of class. I have taken the list of property names using GetProperties();
PropertyInfo [] props;
props=typeof(Import_Column).GetProperties();
I have created an object
Import_Column myobj=new Import_Column();
I want to do something like
myobj.props[2]=value; //giving me error
How can i do it? is there any different approach?

You need to set the value to the property of the current object instance
string propertyName = "TheNameOfThePropertyToSet";
myobj.GetType().GetProperty(propertyName).SetValue(myobj, value, null);
or in your case you could use
myobj.GetType().GetProperty(props[2].Name).SetValue(myobj, value, null);

Related

Sort a list of custom object with String contains in the object [duplicate]

This question already has answers here:
How to sort a list of objects based on an attribute of the objects in descending order?
(9 answers)
Closed 3 years ago.
I have a list contains custom object Box. I want to sort the list alphabetically with a String property of the object Box.
I have tried boxList.sort(), boxList.name.sort(). It does not work.
class Box(object):
name = “”
num = 0
...
box = Box(name, num)
boxList.append(box)
I have a list of box in boxList. I want to sort boxList alphabetically by the String name contains in boxList.
You can use a custom key function using the name property of your objects:
boxList.sort(key = lambda box : box.name)
Alternatively, you can use operator.itemgetter instead of a lambda function:
from operator import itemgetter
boxList.sort(key = itemgetter('name'))

VBA Object doesn't support this property or method when adding an object to a method [duplicate]

This question already has answers here:
VB6 pass by value and pass by reference
(2 answers)
Closed 1 year ago.
I have a class module called Holding. In it are several public variables. My code is this:
Dim holdings as Collection
Dim h as Holding
Set holdings = new Collection
For i = 1 to last
Set h = new Holding
h.x = y
'... etc
holdings.Add(h)
Next i
This gives me error "object doesnt support this property or method" on the holdings.Add(h) line, but everywhere I look, it gives this exact example of how to achieve this. What am I missing?
Remove the parentheses.
holdings.Add h
Otherwise you are trying to add to the collection the value of the default property of your Holding instance, and it doesn't have a default property.

How to fix 'Runtime Error '438' Object does not support this property or method' while trying to push a class into a collection within another class [duplicate]

This question already has answers here:
VB6 pass by value and pass by reference
(2 answers)
Closed 1 year ago.
I have a class module called Holding. In it are several public variables. My code is this:
Dim holdings as Collection
Dim h as Holding
Set holdings = new Collection
For i = 1 to last
Set h = new Holding
h.x = y
'... etc
holdings.Add(h)
Next i
This gives me error "object doesnt support this property or method" on the holdings.Add(h) line, but everywhere I look, it gives this exact example of how to achieve this. What am I missing?
Remove the parentheses.
holdings.Add h
Otherwise you are trying to add to the collection the value of the default property of your Holding instance, and it doesn't have a default property.

What means ?. in Java? [duplicate]

This question already has answers here:
Null-safe Method invocation Java7
(4 answers)
Closed 9 years ago.
I've got such piece of code:
private String getUsername(PersonalAccount account) {
User usr = (User)account?.usr
String name = usr?.getName()
return name
}
And in PersonalAccount class we've got field:
SimpleUser usr
User extends SimpleUser
What means this: ?. in this two lines?
User usr = (User)account?.usr
String name = usr?.getName()
That's not Java, that's Groovy. If it was Java you'd have semicolons ending each statement.
The method returns the name of the user on the account passed in, or null if the account is null or if the user is null.
It uses the safe-navigation operator. The safe-navigation operator evaluates to null if the operand is null, otherwise evaluates to the result of the method call. That way, if you have a method call on something that might be null, you don't have to worry about getting a NullPointerException.

C# use reflection to get method name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get methodinfo from a method reference C#
This is most likely something simple but so far I have not come up with anything on how to do this.
I want to be able to get the name of a method in two different ways. Please note I want a method name, not a property name.
1) Inside of a class like ClassA<T>, looking like:
var name = GetMethodName(x => x.MethodA);
2) Outside of a class, looking like:
var name = GetMethodName<ClassA<object>>(x => x.MethodA);
var name = GetMethodName<ClassB>(x => x.MethodB);
How might I do this exactly?
Thanks!
You don't need lambdas (x => x.MethodA, etc). That's just confusing the issue (and hiding the method of interest: the MethodA bit would be hidden from your GetMethodName method).
Instead, you can use reflection to get a MethodInfo object, which then has a Name property.
For example:
MethodInfo sm = typeof(SomeClass).GetMethod("SomeMethod");
string methodName = sm.Name;
Here methodName will be the string "SomeMethod". (Of course, in this simple case we've used the class name to get the MethodInfo object, so it's somewhat circular and we might as well have just used the hard-coded "SomeMethod" string instead!)

Resources