473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1906

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 HybridDictionar y 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
6530
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
0
1407
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 Python 2.4 on WinMe. (I was using the simpler of the two binding definition interfaces.) When done, I hit the buttons for 'OK' (or however it is labelled -- I cannot currently check), but the definition dialog remained open. After several tries, I hit...
38
8723
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 to the color. For example: Instead of this: color: #306090 I would like to say: define MyShadeOfBlue = '#306090'
7
3014
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 objects may represent computers in a large network and the pairs represent connections between them) another real situation example that's given is:
18
1757
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 se thats happends? -- Thanx in advance! ;-)
16
6745
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 = 3; I know its setting aside storage for the variable itself; does it also use up more storage if the variable name is longer? I realize it would probably take quite a lot of long variable names to make any impact if so, but I was just curious...
26
2548
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
1626
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 a later time. The issue I'm having is as follows: the user enters in 2 names John, Greg clicks my valid button they're both valid so both names are stored in the variable so my variable looks like John,Greg and both are in the textbox. ...
1
1890
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 variable name for element in xml file. The output I would like to have is a table is produced with programme name, programme start time,programme duration and programme details. The programme start time and programme duration are taken from another...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.