字符串中的变量名称 [英] Variable name from string
问题描述
嗨组,
是否可以从字符串派生变量名?所以,如果我有一个字符串myVar,那么
我想定义一个变量myVar。
欢迎任何建议!
Jan Roelof
Jan Roelof de Pijper< jr ********** @ hotmail.com>写道:是否可以从字符串派生变量名称?所以,如果我有一个字符串myVar,那么我想定义一个变量myVar。
变量是在编译时声明的,而不是运行时。现在,你实际想要实现的是什么?通常当人们提出这个问题时,他们只需要一个从字符串到某种对象的地图 - 其中
case,Hashtable或StringDictionary可能是正确的
来看看。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该团体,请不要给我发邮件
2004年4月23日星期五17:16:04 + 0100,Jon Skeet [C# MVP]
< sk *** @ pobox.com>写道:
Jan Roelof de Pijper< jr ********** @ hotmail.com>写道:是否可以从字符串派生变量名称?所以,如果我有一个字符串myVar,那么我想定义一个变量myVar。
变量是在编译时声明的,而不是运行时。现在,你实际想要实现什么?通常当人们提出这个问题时,他们真的只想要一个从字符串到某种对象的地图 - 其中
case,Hashtable或StringDictionary可能是正确的东西来看待。
感谢您的回复!实际上,这并不是我想要的变量名。我想定义一个枚举类型,其成员将使用
来识别事件。事件的数量及其名称不知道
事先,但将从配置文件中读取 - 一个简单的文本
文件。
所以,文本文件可能如下所示:
SystemStart
SystemStop
StartSynthesis
等等
然后我的枚举将是
enum MyEvents {SystemStart,SystemStop,StartSynthesis};
这有意义吗?我必须承认我没有任何经验
与Hashtables和词典,但也许是时候有一些:)
谢谢,
Jan Roelof
公共枚举测试{
test1 = 1,
test2 = 2
};
Console.WriteLine(test.test1.ToString());
给test1
ML
TODO:在这里添加构造函数逻辑
" Jan Roelof de Pijper" < JR ********** @ hotmail.com> schreef在bericht
新闻:呃******************************** @ 4ax.com ...2004年4月23日星期五17:16:04 +0100,Jon Skeet [C#MVP]
< sk *** @ pobox.com>写道:
Jan Roelof de Pijper< jr ********** @ hotmail.com>写道:是否可以从字符串派生变量名称?所以,如果我有一个字符串myVar,那么我想定义一个变量myVar。
变量是在编译时声明的,而不是运行时。现在,你实际想要实现什么?通常当人们提出这个问题时,他们真的只想要一个从字符串到某种对象的地图 - 其中
case,Hashtable或StringDictionary可能是正确的东西来看待。
感谢您的回复!实际上,它并不是我想要的变量名称。我想定义一个枚举类型,其成员将用于识别事件。事件的数量及其名称事先未知,但将从配置文件中读取 - 一个简单的文本文件。
因此,文本文件可能看起来像这样:
SystemStart
SystemStop
StartSynthesis
等
然后我的枚举将是
枚举MyEvents {SystemStart,SystemStop,StartSynthesis};
这有意义吗?我必须承认我没有任何经验和Hashtables和词典,但也许是时候了一些:)
谢谢,
Jan Roelof
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
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
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
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
这篇关于字符串中的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!