473,395 Members | 1,488 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,395 software developers and data experts.

Variable name from string

Hi group,

Is it possible to derive a variable name from a string? So, if I have
a string "myVar" I want to define a variable myVar.

Any suggestions welcome!
Jan Roelof

Nov 16 '05 #1
11 8527
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
Is it possible to derive a variable name from a string? So, if I have
a string "myVar" I want to define a variable myVar.


Variables are declared at compile time, not run time. Now, what do you
actually want to achieve? Usually when people ask this question, they
really just want a map from string to some kind of object - in which
case, Hashtable or StringDictionary would probably be the right thing
to look at.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
On Fri, 23 Apr 2004 17:16:04 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
Is it possible to derive a variable name from a string? So, if I have
a string "myVar" I want to define a variable myVar.


Variables are declared at compile time, not run time. Now, what do you
actually want to achieve? Usually when people ask this question, they
really just want a map from string to some kind of object - in which
case, Hashtable or StringDictionary would probably be the right thing
to look at.


Thanks for your response! Actually, it's not really variable names I
want. I want to define an enumerated type whose members will be used
to identify events. The number of events and their names are not known
beforehand, but will be read from a configuration file - a simple text
file.

So, the textfile could look like this:

SystemStart
SystemStop
StartSynthesis
etc etc

and then my enum would be

enum MyEvents{ SystemStart, SystemStop, StartSynthesis };

Does this make sense? I must admit I have no experience whatsoever
with Hashtables and dictionaries, but maybe it's time got some :)

Thanks,
Jan Roelof

Nov 16 '05 #3
public enum test {

test1 = 1,

test2 =2

};

Console.WriteLine(test.test1.ToString());

gives test1

ML

TODO: Add constructor logic here

"Jan Roelof de Pijper" <jr**********@hotmail.com> schreef in bericht
news:uh********************************@4ax.com...
On Fri, 23 Apr 2004 17:16:04 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
Is it possible to derive a variable name from a string? So, if I have
a string "myVar" I want to define a variable myVar.


Variables are declared at compile time, not run time. Now, what do you
actually want to achieve? Usually when people ask this question, they
really just want a map from string to some kind of object - in which
case, Hashtable or StringDictionary would probably be the right thing
to look at.


Thanks for your response! Actually, it's not really variable names I
want. I want to define an enumerated type whose members will be used
to identify events. The number of events and their names are not known
beforehand, but will be read from a configuration file - a simple text
file.

So, the textfile could look like this:

SystemStart
SystemStop
StartSynthesis
etc etc

and then my enum would be

enum MyEvents{ SystemStart, SystemStop, StartSynthesis };

Does this make sense? I must admit I have no experience whatsoever
with Hashtables and dictionaries, but maybe it's time got some :)

Thanks,
Jan Roelof

Nov 16 '05 #4
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
Variables are declared at compile time, not run time. Now, what do you
actually want to achieve? Usually when people ask this question, they
really just want a map from string to some kind of object - in which
case, Hashtable or StringDictionary would probably be the right thing
to look at.


Thanks for your response! Actually, it's not really variable names I
want. I want to define an enumerated type whose members will be used
to identify events. The number of events and their names are not known
beforehand, but will be read from a configuration file - a simple text
file.

So, the textfile could look like this:

SystemStart
SystemStop
StartSynthesis
etc etc

and then my enum would be

enum MyEvents{ SystemStart, SystemStop, StartSynthesis };

Does this make sense? I must admit I have no experience whatsoever
with Hashtables and dictionaries, but maybe it's time got some :)


Well, you could use the CodeDom or whatever to create such a type on
the fly - but what would be the benefit? What are you actually going to
do with this type afterwards? Why does it have to be an actual type,
rather than just being a set or map?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
On Fri, 23 Apr 2004 21:35:24 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
>Variables are declared at compile time, not run time. Now, what do you
>actually want to achieve? Usually when people ask this question, they
>really just want a map from string to some kind of object - in which
>case, Hashtable or StringDictionary would probably be the right thing
>to look at.


Thanks for your response! Actually, it's not really variable names I
want. I want to define an enumerated type whose members will be used
to identify events. The number of events and their names are not known
beforehand, but will be read from a configuration file - a simple text
file.

So, the textfile could look like this:

SystemStart
SystemStop
StartSynthesis
etc etc

and then my enum would be

enum MyEvents{ SystemStart, SystemStop, StartSynthesis };

Does this make sense? I must admit I have no experience whatsoever
with Hashtables and dictionaries, but maybe it's time got some :)


Well, you could use the CodeDom or whatever to create such a type on
the fly - but what would be the benefit? What are you actually going to
do with this type afterwards? Why does it have to be an actual type,
rather than just being a set or map?


OK, here's what I am trying to do. I am writing a dialogue system. In
its present form it will allow a user to get information on train
departure and arrival times in a kiosk-like setting, using both speech
and a touch screen for input and output. There is one controlling
module, which starts up all the other modules (class libraries) that
make up the system. The controlling module reads a configuration file
to determine what classes to instantiate and in what modules they can
be found and then uses Assembly.LoadFrom, Assembly.GetTypes and
Activator.CreateInstance to do so. All communication between the
modules is done through events. These events and methods to raise them
are defined in a single central module that all other modules must
reference.

This all works pretty well and it's very easy to add new modules,
replace existing ones, and so on. Now I want to make this
infrastructure more general by also putting the events - which
basically define the functionality of the system - in a configuration
file. It would then be a snap to add new events, or use a completely
different set of events, without having to recompile anything.

So, I am thinking of having just one event, something like:

public delegate void EventHandlerKiosk( Object sender, EKioskEvents
kioskEvent, object obj );
public static event EventHandlerKiosk eventKiosk ;

The event would be raised by calling this method:

public static void RaiseEventKiosk( object sender, EKioskEvents
kioskEvent, object objKiosk ){
if( eventKiosk != null )
eventKiosk( sender, kioskEvent, objKiosk );
}

The second parameter in the EventHandler would be an enumerator
indicating the type of event. The third parameter would pass data.

I hope this explains what I am trying to do.
I am grateful for any ideas you might have!

Thanks,
Jan Roelof

Nov 16 '05 #6
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:

<snip>
I hope this explains what I am trying to do.


Sort of - but I'm not sure how you're going to respond to different
event types if you don't know them at compile time. If you're going to
create a new type at runtime, how are you actually going to *use* it in
your code? If you don't have EKioskEvents defined until runtime, you
can't use it in your code directly, can you?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
On Sat, 24 Apr 2004 11:05:30 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:

<snip>
I hope this explains what I am trying to do.


Sort of - but I'm not sure how you're going to respond to different
event types if you don't know them at compile time. If you're going to
create a new type at runtime, how are you actually going to *use* it in
your code? If you don't have EKioskEvents defined until runtime, you
can't use it in your code directly, can you?


Absolutely right. The idea is to make it easy for someone to use the
infrastructure to set up a new dialogue system for a different domain,
say a pizza ordering service. They would

1. decide on the set of events necessary to accomplish the task,
2. add these events in the configuration file,
3. write modules (class libraries) that implement the functionality
and use the events to trigger actions and pass data,
4. Add the module and class names to the configuration file.

Now if there is a module that allows a user to type in a pizza name
and another module that collects all pizza data, the first module
could simply use

RaiseEventKiosk(
this, KioskEvents.NewPizzaName, (object)strPizzaName );

to send the pizza name to the second module. The receiving module
would have something like

CKioskEvents.eventKiosk += new EventHandlerKiosk( OnKioskEvent );

private void OnKioskEvent(
object sender, EKioskEvents kioskEvent, object objKiosk )
{
switch( kioskEvent )
{
case EKioskEvents.NewPizzaName :
m_strPizzaName = (string)objKiosk ;
...........
break ;
case EKioskEvents.NewAddress :
m_strAddress = (string)objKiosk ;
...........
default :
break ;
}
}

The central Kiosk control module takes care of starting up these two
modules (and any other ones), and the common events module sees to it
that all events are known to all modules and that they can raise them
and respond to them. The emphasis on the use of events here is that
there is no need to know beforehand how many modules there will be, or
their names. This would be necessary if use were made of public
properties and/or methods. If a new module is added later, it only
needs to know about the events available and it can become a full
participant immediately.

Does this make it clearer what I'm trying to do?
And thanks for thinking with me :)

Jan Roelof

Nov 16 '05 #8
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
Sort of - but I'm not sure how you're going to respond to different
event types if you don't know them at compile time. If you're going to
create a new type at runtime, how are you actually going to *use* it in
your code? If you don't have EKioskEvents defined until runtime, you
can't use it in your code directly, can you?


Absolutely right. The idea is to make it easy for someone to use the
infrastructure to set up a new dialogue system for a different domain,
say a pizza ordering service. They would

1. decide on the set of events necessary to accomplish the task,
2. add these events in the configuration file,
3. write modules (class libraries) that implement the functionality
and use the events to trigger actions and pass data,
4. Add the module and class names to the configuration file.


But they'd have to also run a tool to generate the code for the enums,
or they wouldn't be able to compile their code, surely?

I think I'm still missing when you want the new enum to become
available for use.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
On Mon, 26 Apr 2004 08:34:40 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Jan Roelof de Pijper <jr**********@hotmail.com> wrote:
>Sort of - but I'm not sure how you're going to respond to different
>event types if you don't know them at compile time. If you're going to
>create a new type at runtime, how are you actually going to *use* it in
>your code? If you don't have EKioskEvents defined until runtime, you
>can't use it in your code directly, can you?


Absolutely right. The idea is to make it easy for someone to use the
infrastructure to set up a new dialogue system for a different domain,
say a pizza ordering service. They would

1. decide on the set of events necessary to accomplish the task,
2. add these events in the configuration file,
3. write modules (class libraries) that implement the functionality
and use the events to trigger actions and pass data,
4. Add the module and class names to the configuration file.


But they'd have to also run a tool to generate the code for the enums,
or they wouldn't be able to compile their code, surely?

I think I'm still missing when you want the new enum to become
available for use.


I finally recognize your point, and you are absolutely right, of
course.Sometimes, I need to be hit over the head more than once before
comprehension dawns. What I want cannot be done through this enum.
Instead, I suppose I could use the strings themselves, rather than
using them to derive enum constants from:

RaiseEventKiosk(
this, "NewPizzaName", (object)strPizzaName );

CKioskEvents.eventKiosk += new EventHandlerKiosk( OnKioskEvent );

private void OnKioskEvent(
object sender, string strKioskEvent, object objKiosk )
{
switch( strKioskEvent )
{
case "NewPizzaName" :
m_strPizzaName = (string)objKiosk ;
...........
break ;
case "NewAddress" :
m_strAddress = (string)objKiosk ;
...........
default :
break ;
}
}

I would lose Intellisense support and automatic documentation through
NDoc, though ..

Thanks,
Jan Roelof

Nov 16 '05 #10
<jr**********@hotmail.com> wrote:
But they'd have to also run a tool to generate the code for the enums,
or they wouldn't be able to compile their code, surely?

I think I'm still missing when you want the new enum to become
available for use.
I finally recognize your point, and you are absolutely right, of
course.Sometimes, I need to be hit over the head more than once before
comprehension dawns.


I'm glad we're finally on the same page - although it was equally
likely I wasn't seeing *your* point!
What I want cannot be done through this enum.
Instead, I suppose I could use the strings themselves, rather than
using them to derive enum constants from:

RaiseEventKiosk(
this, "NewPizzaName", (object)strPizzaName );
You don't need to cast strPizzaName to object here, unless you've also
got a method with a signature

private void OnKioskEvent(
object sender, string strKioskEvent, string objKiosk )

and you wish to make sure you get the right override.

<snip>
I would lose Intellisense support and automatic documentation through
NDoc, though ..


But if you don't have the names until runtime, you couldn't get
Intellisense support and NDoc documentation anyway.

You might want to consider writing a tool which takes the configuration
file and *generates* an enum declaration that people could then use
(and annotate, eg with documentation) in their projects.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
On Mon, 26 Apr 2004 09:50:15 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
You might want to consider writing a tool which takes the configuration
file and *generates* an enum declaration that people could then use
(and annotate, eg with documentation) in their projects.


I am going to think about this one. Thanks very much for all your
efforts (and indeed your contributions all over the newsgroup).

Jan Roelof

Nov 16 '05 #12

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

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
3
by: Konrad Mathieu | last post by:
How do I - within in a function - make Javascript recognize a string as a variable name? Thanks and cheers, Konrad
10
by: cpp | last post by:
I am trying to find out how to assign the name of a variable (aka identifier) given a string for this name. For example: string s = "whatever"; How can I obtain the following expression:...
7
by: PL | last post by:
I want to dynamicaly create variable and assigned it a particular name, for exemple: foreach (FileInfo FI in ArrayOfFiles) { string FI.Name = "AValueWhoDoesntMatter" } Of course, this...
6
by: Raed Sawalha | last post by:
i have the following case a MyClass class has method with 2 arguments as void SetProp(string varname,string varvalue); in MyClass i have following members string...
2
by: paladin.rithe | last post by:
I have no clue if this can be done, but I thought I'd ask anyway. I have a modular system where I store module information in a database. One of the types of modules that I have is one that,...
45
by: Zytan | last post by:
Shot in the dark, since I know C# doesn't have macros, and thus can't have a stringizer operator, but I know that you can get the name of enums as strings, so maybe you can do the same with an...
6
by: Bora Ji | last post by:
Please help me to creating dynamic VARIABLE in java, with predefined name.
2
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.