473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can you convert a string to a variable name?

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.
Jul 21 '05 #1
4 1749
>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/
Jul 21 '05 #2
Byron <By***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
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" <By***@discussions.microsoft.com>
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.

Jul 21 '05 #4
"Byron" <By***@discussions.microsoft.com> wrote:
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.
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
3
by: ET | last post by:
I don't know whats the problem, but after I added functions to first verify, then relink linked tables if not found, now I can't convert that database to MDE format. I can split the database, but...
2
by: Jason | last post by:
In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string variable to double variable, I got something interesting: Dim stringVal As String = "101.01" Dim doubleVal As Double...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
4
by: thomasc1020 | last post by:
This is regarding VB.NET 2003. Variable 'Date' is a string and it contains date information in this format: "DEC/05/2007". Now I am trying to convert the format as "2007-12-05". Is it...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
0
by: =?Utf-8?B?cm9uZSBtYXRpYXM=?= | last post by:
I have the same task to do but everytime I tried to parse my code I get a null value returned after executing "dtMaterials.WriteXml(swMaterials);". I am using the following code: Hope you can hep...
12
by: Miro | last post by:
How can I convert this part of the line: Me.dgvmyData.Columns.Item("txtCellName") ' "txtCellName" which is within this line - Me.dgvmyData.Sort(Me.dgvmyData.Columns.Item("txtCellName"),...
4
by: Gunnar Hurtig | last post by:
How do I convert a string name into a variable name? example L= I want to create two variables from L so that I can assign values to them. say a=4
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.