Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

[C# 2.0] Convert String to Variable Name


  • Please log in to reply

#1
xyzz1233

xyzz1233

    Member

  • Member
  • PipPip
  • 16 posts
Would anyone be kind enough to tell me if it is possible to take a string and convert it to the name of an object or variable such as in the code below?

[codebox]
string VariableName = "Hello";
string Convert.ToObj(VariableName) = "Made from VariableName string"
[/codebox]

Is this possible where Convert.ToObject is a function to convert the string to the name of an object? Thanks!

Edited by xyzz1233, 09 June 2008 - 06:01 PM.

  • 0

Advertisements


#2
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
You may be able to use the FindControl method to do this.
  • 0

#3
xyzz1233

xyzz1233

    Member

  • Topic Starter
  • Member
  • PipPip
  • 16 posts
It's not like that... I have an array that holds a bunch of strings. I need to pull a string out of the array, and call a function that takes an object, whose name is the value of the string. I just don't know how to convert the string's value to an object name.
  • 0

#4
TomWij

TomWij

    Member

  • Member
  • PipPipPip
  • 125 posts

It's not like that... I have an array that holds a bunch of strings. I need to pull a string out of the array, and call a function that takes an object, whose name is the value of the string. I just don't know how to convert the string's value to an object name.


Why do you need this specific behaviour?
This looks like an unsafe way to program, can't you just use an array with pointers to the variable names?
  • 0

#5
xyzz1233

xyzz1233

    Member

  • Topic Starter
  • Member
  • PipPip
  • 16 posts
I'm creating a game engine, and in my editor, there needs to be a way to create objects. These objects will be assigned a name and other properties based on the user's input in a bunch of textboxes. What I need to do is create an object with the name that the user puts into the textbox. Additionally, I need to store this name so that when everything needs to be rendered, code for each object doesn't need to be added, only a method that cycles through the array and renders everything.
  • 0

#6
TomWij

TomWij

    Member

  • Member
  • PipPipPip
  • 125 posts

I'm creating a game engine, and in my editor, there needs to be a way to create objects. These objects will be assigned a name and other properties based on the user's input in a bunch of textboxes. What I need to do is create an object with the name that the user puts into the textbox. Additionally, I need to store this name so that when everything needs to be rendered, code for each object doesn't need to be added, only a method that cycles through the array and renders everything.


Letting the user access the variables is a very unsafe way to program, I don't even know if it is possible. (Maybe with an eval function)
You don't want this bad design in your engine, believe me...

Why don't just create an ArrayList with the type of your Objects?
If you don't understand this then show how your object is made (Defined, initialised, name, ...) and I'll show an exact example how you should do it. :)

Here is an example I found: http://www.java2s.co...looptocheck.htm
You can add your objects instead of the strings.

I need to store this name so that when everything needs to be rendered

You could create a class RenderObject or NamedObject that inherits your Object and adds a name field to it, then use these objects to add to your array.
Or when you don't have objects you could make an ArrayList of strings.

--- My post is a bit bloated I think, hope you understand it, I'm writing a game engine too since yesterday. :) ---

Edited by TomWij, 22 June 2008 - 02:56 PM.

  • 0

#7
TomWij

TomWij

    Member

  • Member
  • PipPipPip
  • 125 posts

I need to pull a string out of the array, and call a function that takes an object, whose name is the value of the string.


Why don't you change the function to accept strings?
  • 0

#8
xyzz1233

xyzz1233

    Member

  • Topic Starter
  • Member
  • PipPip
  • 16 posts
Thanks! I really didn't know that was possible until just now. I come from an old Visual Basic background (pre .NET), and I never really had any formal .NET training, and so when I got to C#, I was mossing a chunk of knowledge. I never even knew VB6 that well, just picked up stuff as I went. :)

EDIT: I've got a simple example working with an arraylist to store the objects, and a hashtable to store the names and indexed of the objects. Thanks so much for your help!

Edited by xyzz1233, 25 June 2008 - 01:00 AM.

  • 1

#9
TomWij

TomWij

    Member

  • Member
  • PipPipPip
  • 125 posts
Switching languages can indeed often be confusing, I would suggest you to read a set of tutorials or a wikibook on C# and .NET.
Writing a good game engine can be a hard job. :)
  • 0

#10
igoumeninja

igoumeninja

    New Member

  • Member
  • Pip
  • 1 posts

Thanks! I really didn't know that was possible until just now. I come from an old Visual Basic background (pre .NET), and I never really had any formal .NET training, and so when I got to C#, I was mossing a chunk of knowledge. I never even knew VB6 that well, just picked up stuff as I went. :D

EDIT: I've got a simple example working with an arraylist to store the objects, and a hashtable to store the names and indexed of the objects. Thanks so much for your help!


Hi xyzz1233,
I am trying to built a HashTable.
Is it possible to share your code?
Thanks for sharing

Aris
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP