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

declaring variables in a counted loop

i was trying to find out how to declare a variable using a string variable
for its name. for example if i declare\\ string a="b"; double (string a) =x
-this code actually doesn't work-\\in this case i want a double variable
named b. The whole point is that i want to be able to declare variables
without writing the code for all of them myself. thanks for any help
Nov 17 '05 #1
1 1392
it works-it just wont do anything wrote:
i was trying to find out how to declare a variable using a string variable
for its name. for example if i declare\\ string a="b"; double (string a) =x
-this code actually doesn't work-\\in this case i want a double variable
named b. The whole point is that i want to be able to declare variables
without writing the code for all of them myself. thanks for any help


You are looking for something called "variable variables." PHP uses
these, and they're dangerous, and can create very odd bugs that are hard
to track down. I don't recommend thinking in that way or even using php
because it lets you think that way. Avoid variable variables. Yuck.

If you're using .NET 1.1, you can use a Hashtable:

using System.Collections;
...
Hashtable ht = new Hashtable();

ht.Add("x", 321.3);
ht.Add("y", 123.1);

// later you can access the numbers by their name:
Console.WriteLine(ht["x"]);
Console.WriteLine(ht["y"]);

If you're using .NET 2.0, you can use either a Hashtable as above (don't
forget to include that using statement above), or you can use a
Dictionary<K,V>:

using System.Collections.Generic;
...
Dictionary<string, double> d = new Dictionary<string, double>();

d.Add("x", 321.3);
d.Add("y", 123.1);

// later you can access the numbers by their name:
Console.WriteLine(d["x"]);
Console.WriteLine(d["y"]);

Hope that helps.
Nov 17 '05 #2

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

Similar topics

2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
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: 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
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...

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.