473,386 Members | 1,733 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.

Defining variable names on the fly

Hello,

I'm trying to re-code a site which I wrote in PHP into
ASP.NET using Visual Basic .NET.

Something PHP has which has proved invaluable is the
concept of variable variables. I haven't had any success
with working out how to do it and hoped someone might be
able to help.

What I need to do is (for example)

Dim Foods(2) As String
Foods(0) = "Fish"
Foods(1) = "Chips"
Foods(2) = "Peas"

For Foods = 0 To 2
Dim [value of Foods(x)] As String
Next

So this would declare three variables as strings: called
Fish, Chips and Peas.

I've found info on Eval and Execute in various places but
they appear not to work for ASP.NET.

Can anyone help? Apologies if this is the wrong group -
do let me know if so.

Thanks, Hamish
Nov 21 '05 #1
2 1889

Hamish Symington wrote:
Hello,

I'm trying to re-code a site which I wrote in PHP into
ASP.NET using Visual Basic .NET.

Something PHP has which has proved invaluable is the
concept of variable variables. I haven't had any success
with working out how to do it and hoped someone might be
able to help.

What I need to do is (for example)

Dim Foods(2) As String
Foods(0) = "Fish"
Foods(1) = "Chips"
Foods(2) = "Peas"

For Foods = 0 To 2
Dim [value of Foods(x)] As String
Next

So this would declare three variables as strings: called
Fish, Chips and Peas.


PHP is able to do this because as an interpreted language, the
'language engine' is available at runtime, so stuff like parsing can
happen at runtime. .NET languages are compiled, and as such at runtime
you aren't actually running VB any more, so the VB language engine is
not available.

However, don't worry, there is an easy 'Framework-y' way to achieve
what you want.

You want to: Create a list of arbitrary Strings (the 'variable names'),
and then attach a String value to each name (the 'variable values'). In
the Framework, this is exactly the functionality provided by
(implementers of) the IDictionary interface (quote from documentation):
The IDictionary class is the base interface for collections of
key-and-value pairs.

Each element is a key-and-value pair stored in a DictionaryEntry
object.

Each association must have a unique key that is not a null reference
(Nothing in Visual Basic), but the value of an association can be any
object reference, including a null reference (Nothing). The IDictionary
interface allows the contained keys and values to be enumerated, but it
does not imply any particular sort order.


(The data structure you want is also known as an *associative array*,
if that's any help)

Of the many built-in classes that implement IDictionary, the one I
would recommend to you here is the Hashtable (although I'm increasingly
thinking about using the HybridDictionary as my default IDictionary
implementation of choice). This is how you would use a Hashtable in
your scenario:

Dim Foods(2) As String
Foods(0) = "Fish"
Foods(1) = "Chips"
Foods(2) = "Peas"
' as before

Dim ht As Hashtable = New Hashtable()

For x = 0 To 2
ht.Add(Foods(x), "")
Next

Now you have three keys in the Hashtable, and thus you effectively have
three String variables ht("Fish"), ht("Chips"), ht("Peas"). You can
treat these as normal String variables from here on.

Of course there's much more to the Hashtable - both key and value can
be any object, not just Strings - but this is enough for the moment :)

--
Larry Lard
Replies to group please

Nov 21 '05 #2
Larry,

You should have a reason for that, why not a normal array?

dim Foods() as String = {"Fish", "Chips", "Peas"}

Dim Value as string = ""
For x as integer = 0 To 2
Value = Value & " " & Food(x)
Next
Value = Value.substring(1)

I would use what you show when I want to get what is in this case the
indexer however I don't see the need for this. (To say it better when it is
as the subject says, I see completly your explanation, however when I see
the sample not).

What do I see wrong?

Cor
Nov 21 '05 #3

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
0
by: Brian van den Broek | last post by:
Hi all, IDLE refuses to launch, and I believe it is because I attempted to define a custom key-binding that it doesn't like. I was recently defining a custom keybinding in IDLE 1.1 under...
38
by: Ted | last post by:
Is there a way to define a constant value for a color in a CSS declaration? I have numerous colors in different CSS elements, and if I make a change in color, I have to change all the reference...
7
by: ben | last post by:
hello, an algorithm book i'm reading talks about the connectivity problem/algorithm. it gives a number of examples where the connectivity problem applies to real life situations (like, the...
18
by: Carramba | last post by:
Hi! I am wondering what is the best way to control that compiler manage variable names longer then 8 signs. Does it enought to set 2 variables with the same name and difference after 8 sign and...
16
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n =...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
2
by: Tom | last post by:
I have a textbox on my web form were users can enter in 1 name or many names. The form then validates the name against the database. If the name is valid its then saved into a varaible to be used at...
1
by: mrsmith221189 | last post by:
Hello everyone. I'm a novice to XML/XSL so it would be great to learn from the experts. The ouput of my xml file is BBC EPG(Electronic Programme Guide) table. I'm having problems defining a...
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: 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
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
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...

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.