0

Usually, if the object was a class instance, I'd use reflection to set values to its members. Consider the following:

class Foo
{
  public Control _tCtrl { get; set; }
  public Object _tObj { get; set; }

  public void Set()
  {
    // How do I give tObj the converted value of _tCtrl.ToString() ?

    // var val = Convert.ChangeType( _tCtrl.ToString(), tObj.GetType() );
    // _tObj.SetValue( val );
  }
}

Calling it would be like:

Class Bar
{
  public Int32 _i { get; set; }
}


Bar bar = new Bar();   
Foo foo = new Foo();

foo._tCtrl = new TextBox() { Text = "100" };
foo._tObj = bar._i;    
foo.Set();

Or:

Foo foo = new Foo();
Int32 i;

foo._tCtrl = new TextBox() { Text = "100" };
foo._tObj = i;

foo.Set();    

Anything could be passed into Set(). Assume that I can convert from a String to whatever type is tObj.GetType().

5
  • How can you convert from String to the type? Commented May 10, 2012 at 23:49
  • Using Convert.ChangeType() for basic types, otherwise, I'll write a converter. I'm interested in how one would set the value to the object. Commented May 10, 2012 at 23:56
  • your sample code is not clear at all... try to show code that compiles. Don't put all the "surroundings". Show only the code that you can't implement, and the expected result. Commented May 11, 2012 at 0:01
  • That's because you have the rest of the code. Commented May 11, 2012 at 0:05
  • It compiles fine: pastebin.com/1UMxiL3m Commented May 11, 2012 at 0:15

2 Answers 2

1

I think what this boils down to is converting a string to any other type. That has problems. How would you convert a string to, say, a Form, a List<>, or a Random? It would require you to write a converter for every type in the .NET Framework (or possibly every existing type in the universe).

You shouldn't be trying to do this. I don't know what you are trying to achieve, but there is almost surely a better way.

Sign up to request clarification or add additional context in comments.

4 Comments

Well, I'm mostly concerned with the basic built-in types { Int32, Decimal, String, Enum, etc... }. I would rather not have a whole bunch of if statements for those types, rather, something neater using reflection? For classes that I define, I'll use a converter.
What is the big picture? Why would you want to do this? There has to be a better solution.
I have a UserControl that is a container for a control. I want to be able to get the value from that control, using .ToString(), convert it, and apply it to a variable of type Object. I'd like to call MyUserControl.Get(ref i);, where i is an Int32 or String or an instance of MyOtherClass.SomeVariable. Inside the Set() method, I'd do a conversion and apply the value to i.
Yes, but why? What are you trying to do? What does this give to the user?
0

There is no way to allow that kind of syntax, but you can do the following:

foo.Set(bar, "_i");

Where Set uses the following:

type.GetProperty(propertyName, Type.EmptyTypes).SetValue(tObj, value, Type.EmptyTypes);

Where propertyName, tObj, and value are defined elsewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.