473,394 Members | 1,724 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,394 software developers and data experts.

(dumb) Question about naming variables

I have a question so basic that I suspect everyone takes it for
granted, as i cannot google the answer. Anyone here can probably answer
it!

I want to create a bunch of variable at runtime, but I don't know
beforehand how many. When the program has calculated the number of
variables (N) that I need, I pass N to a loop. I then want the loop to
generate N new variables. I want it to name each variable in turn, so
I'll end up with variable named something like this:

var1
var2
var3

etc.

How can I create a variable name that concatenates a standard string
(Var in this case) with an integer-converted-to-a-string?

Thanks!

-fool

Nov 21 '05 #1
7 979
Netloss,

Normaly I think it is done without a name by an arraylist.
http://msdn.microsoft.com/library/de...classtopic.asp
When you need it as you ask than is in my opinion the hashtable your first
class to use.
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 21 '05 #2
Thanks! You are probably right, I should be using some kind of
collection in place of individual variables. I will probably use an
arraylist for now.

However, let's assume that I use a hashtable instead. In that case I
need to provide a key value for each item added to the hashtable. I
believe this leads back to my original question: How would I generate
a unique key for each hashtable entry? If I want my keys to be a
combination of some word (say, Item) and a number, I'd try something
like this:

dim keyvalue as string
dim counter as integer

keyvalue = "Item" & counter

dim HT as hashtable

HT.add(someitem, keyvalue)

But instead of adding someitem to the hashtable with a key value of
"Item0", I imagine I'll get an error, or it will assign a key value of
"keyvalue". I am probably not very clear, but I hope you see what I'm
asking....

Thanks again!

On Sat, 23 Apr 2005 07:33:20 +0200, "Cor Ligthert"
<no************@planet.nl> wrote:
Netloss,

Normaly I think it is done without a name by an arraylist.
http://msdn.microsoft.com/library/de...classtopic.asp
When you need it as you ask than is in my opinion the hashtable your first
class to use.
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor


Nov 21 '05 #3
Hi,

When you have to create in your program a key. Than there is in my opinion
no need for a key. You just can use an index as with the arraylist.

When you have a key, than that comes from outside your program and can you
only check if it already exist and take the steps what to do with it on that
what it is. That is not to describe in an abstract way.

Just my thought,

Cor
Nov 21 '05 #4
"netloss" <ne*****@metacrawler.com> schrieb:
I want to create a bunch of variable at runtime, but I don't know
beforehand how many. When the program has calculated the number of
variables (N) that I need, I pass N to a loop. I then want the loop to
generate N new variables. I want it to name each variable in turn, so
I'll end up with variable named something like this:


\\\
Dim astr(n - 1) As String
For i = 0 To n - 1
astr(i) = ...
Next i
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #5
Herfried, just so I understand your code (I am pretty new at this):

You seem to be saying (lke Cor) that I should just declare an array and
populate it, instead of trying to declare names?

Nov 21 '05 #6
"netloss" <ne*****@metacrawler.com> schrieb:
Herfried, just so I understand your code (I am pretty new at this):

You seem to be saying (lke Cor) that I should just declare an array and
populate it, instead of trying to declare names?


Yes, if possible that's always a good idea.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #7
OK, thanks to both of you for your help

Nov 21 '05 #8

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

Similar topics

4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
3
by: Sam | last post by:
Hi all What's standard naming convention for module and global variables in .net? Do we still use lower case prefix "m" for module (or class) scope and "g" for global scope? Regards, Sam
8
by: bim_bom | last post by:
Hi, is there any tool to change naming convention in c++ sources? I mean something, that parses cpp and h files in my project, and it finds, what variables are there declared. I think, it should...
4
by: robear | last post by:
A while ago, I found a web page that describe an array of naming conventions when diming a variabe. For instance: tbl for table, str for string, i for integer.. (eg: tblMyTable, strMyString,...
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
18
by: psbasha | last post by:
Hi, I would like to know what naming conventions we can follow for the following types of variables/sequences : Variables : ------------- Integer Float Boolean
9
by: BillCo | last post by:
I'm coming from a MS Access background and so I'm very used to and comfortable with the hungarian (Leszynski et al) naming conventions. However, I'm getting started into my first SQL Server...
1
by: mk | last post by:
http://www.python.org/dev/peps/pep-0008/ "Function Names Function names should be lowercase, with words separated by underscores as necessary to improve readability." However, this PEP does...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.