Can you convert a string to a variable name?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to come up with generic routines that can create a reference to a
variable on the fly. Assume every form has a variable named for the form,
followed by 'foo'. For instance, a form named test would have a variable
named 'testfoo' and the from test2 would have one named 'test2foo'. I want
to refer to each using something like SomeMethod(Form.Name + "foo") = 2.

Any help would be greatly appreciated.
 
I am trying to come up with generic routines that can create a reference to
a
variable on the fly. Assume every form has a variable named for the form,
followed by 'foo'. For instance, a form named test would have a variable
named 'testfoo' and the from test2 would have one named 'test2foo'. I
want
to refer to each using something like SomeMethod(Form.Name + "foo") = 2.

The following method will return the value of a property named "Foo" via
reflection:
public object SomeMethod() {
Type target=this.GetType();
PropertyInfo foo=target.GetProperty("Foo");
if (foo!=null) {
return foo.GetGetMethod().Invoke(this,new object[] {});
} else return null;
}

(You must add a using System.Reflection statement to your code.)
I guess its close to what your're after...

Anders Norås
blog: http://dotnetjunkies.com/weblog/anoras/
 
Byron said:
I am trying to come up with generic routines that can create a reference to a
variable on the fly. Assume every form has a variable named for the form,
followed by 'foo'. For instance, a form named test would have a variable
named 'testfoo' and the from test2 would have one named 'test2foo'. I want
to refer to each using something like SomeMethod(Form.Name + "foo") = 2.

Variable names are compile-time beasts, not runtime beasts. (Okay, they
*are* available at runtime using reflection, but that's usually a bad
way of working.)

It sounds like you really want a mapping from form name to reference -
so use a hashtable.
 
Byron,

Did you already searched the dotnet newsgroups, you are number n who wants
that.

What is the benefit you want to reach?

Because it is asked however nobody says why they want to do it and therefore
I am curious about that.

Cor
 
Byron said:
I am trying to come up with generic routines that can create a reference to a
variable on the fly. Assume every form has a variable named for the form,
followed by 'foo'. For instance, a form named test would have a variable
named 'testfoo' and the from test2 would have one named 'test2foo'. I want
to refer to each using something like SomeMethod(Form.Name + "foo") = 2.

Any help would be greatly appreciated.

Your example would be better served by this (as I'm assuming
that you are working in a strongly typed language):

public interface IFoo {
public int Foo {
set;
get;
}
}

public Test : System.Windows.Forms.Form, IFoo
private testFoo_;

public int Foo {
set { testFoo_ = value; }
get { return testFoo_; }
}

...
}

public Test2 : System.Windows.Forms.Form, IFoo
private test2Foo_;

public int Foo {
set { test2Foo_ = value; }
get { return test2Foo_; }
}

...
}


....then...

class GenericClass {
static public void GenericFooRoutine( IFoo fooObj ){
fooObj.Foo = 2;
}
}

....

Test test = new Test();
Test2 test2 = new Test2();
IFoo fooTest;

test.Foo = 1;
test2.Foo = 3;

// Strictly using the interface
fooTest = test;
fooTest.Foo = 4;

fooTest = test2;
fooTest.Foo = 5;

// Using generic routine
GenericClass.GenericRoutine( test );
GenericClass.GenericRoutine( test2 );


.... in NET 2.0 you should be able to cut down on the lines
of code through the use of generics. In that case you could
put all the boilerplate code associated with "public int
Foo" into a template and have the template itself implement
the IFoo interface while the Form class (or a subclass
thereof) to be customized is passed in as a template
parameter.

You make a routine "generic" by having the routine operate
on the "generic" interface rather than the implementing
class.
 
Back
Top