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.