Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / November 2004

Tip: Looking for answers? Try searching our database.

Can you convert a string to a variable name?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Byron - 21 Nov 2004 21:35 GMT
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.
Anders Nor?s - 21 Nov 2004 21:51 GMT
>I am trying to come up with generic routines that can create a reference to
>a
[quoted text clipped - 3 lines]
> 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/
Jon Skeet [C# MVP] - 21 Nov 2004 21:53 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Cor Ligthert - 22 Nov 2004 10:00 GMT
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" <Byron@discussions.microsoft.com>

>I am trying to come up with generic routines that can create a reference to
>a
[quoted text clipped - 5 lines]
>
> Any help would be greatly appreciated.
UAError - 22 Nov 2004 12:34 GMT
>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,
[quoted text clipped - 3 lines]
>
>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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.