Dim a Variable from a string???Possible??
Author |
Message |
Marc #1 / 16
|
 Dim a Variable from a string???Possible??
i want to read my variable names from a file or database. is it possible to do sth like: dim rs!name as string [len(rs!name)] where rs!name is for example "Adress" ? I know there's a way, but I forgot how.....
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Klaus H. Probs #2 / 16
|
 Dim a Variable from a string???Possible??
Marc, First, it's not polite to post to so many newsgroups. Keep your posts to relevant groups only. Second, no it cannot be done. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Please post/reply to the newsgroup(s) so that everyone can benefit from the discussion. Regards, Klaus H. Probst, MCP
ICQ: 22454937 The VB Box: http://members.xoom.com/kprobst/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quote: > i want to read my variable names from a file or database. > is it possible to do sth like: > dim rs!name as string [len(rs!name)] > where rs!name is for example "Adress" ? > I know there's a way, but I forgot how.....
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Bob Butle #3 / 16
|
 Dim a Variable from a string???Possible??
Quote:
>First, it's not polite to post to so many newsgroups. Keep your posts to >relevant groups only. >Second, no it cannot be done.
>> i want to read my variable names from a file or database. >> is it possible to do sth like: >> dim rs!name as string [len(rs!name)] >> where rs!name is for example "Adress" ? >> I know there's a way, but I forgot how.....
An off-the-wall thought.... could a collection be used to get the functionality: Dim mcStuff As Collection Set mcStuff=New Collection mcStuff.Add thevalue,rs!Name & "k" (I append the k to ensure that the key value is not numeric) You can't change values so to update the value of a variable you'd have to remove & re-add the item in the collection but it might work. You could keep another collection of the field names if you need to be able to retrieve them as well.
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Klaus H. Probs #4 / 16
|
 Dim a Variable from a string???Possible??
Bob, Well, yes, that's possible. But that's another thing entirely, because you're storing values, not declaring variables. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Please post/reply to the newsgroup(s) so that everyone can benefit from the discussion. Regards, Klaus H. Probst, MCP
ICQ: 22454937 The VB Box: http://members.xoom.com/kprobst/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Jeff Johnso #5 / 16
|
 Dim a Variable from a string???Possible??
[Excessive newgroups snipped] Quote: > i want to read my variable names from a file or database. > is it possible to do sth like: > dim rs!name as string [len(rs!name)] > where rs!name is for example "Adress" ? > I know there's a way, but I forgot how.....
No there isn't. Variables names cannot be variable. There may be workarounds, but there is no way to TRULY create a variable name dynamically.
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
SevenStar Technologie #6 / 16
|
 Dim a Variable from a string???Possible??
You can "kind of" do this using a collection. Try the following code: Dim c As New Collection Call c.Add(rs!value,rs!name) 'don't try to put a duplicate in, though! Afterwards, to get the value out, you would have to do this: Debug.Print c.Item(rs!name) This is the closest I can think of... -Rob Hajicek SevenStar Technologies ______________________________________________________________________ SEVENSTAR TECHNOLOGIES * PO Box 937 * St. Louis, MO 63011-0937
**** OBJECT BRIEFCASE for VB 6.0 and VB 5.0 is now available! **** A PROFESSIONAL control and code library add-in! **** Create your own code wizards! Add error handling to your code! **** Free trial version at: http://www.SevenStarTech.com/download.htm ______________________________________________________________________ Quote:
> i want to read my variable names from a file or database. > is it possible to do sth like: > dim rs!name as string [len(rs!name)] > where rs!name is for example "Adress" ? > I know there's a way, but I forgot how.....
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
MSMV #7 / 16
|
 Dim a Variable from a string???Possible??
Klaus... Quote: >Well, yes, that's possible. But that's another thing entirely, because >you're storing values, not declaring variables.
Not to be argumentative, but in the most technical sense it is declaring a variable. A variable is simply a named location in memory that stores a value, which is exactly what Bob was doing with his collection example. -- Ben Baird, MVP Visual Basic Thunder http://www.vbthunder.com Common Controls Replacement Project, Official Member http://www.mvps.org/ccrp/ Please direct your programming questions to the newsgroups.
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Klaus H. Probs #8 / 16
|
 Dim a Variable from a string???Possible??
Hi Ben, Quote: > Not to be argumentative, but in the most technical sense it is declaring a > variable. A variable is simply a named location in memory that stores a > value, which is exactly what Bob was doing with his collection example.
You're being argumentative ;-) Actually, no, I don't agree with you there. In the case of a collection what you're doing is allocating memory to hold a variable in an indexed sequence of memory addresses, placing the variable there, and then using a key (or index) to gain access to that address. OTOH, declaring a variable means allocating memory for a given type and assigning a "selector" (not the best definition, but you get the idea) to it, which is the symbolic name. The original post wanted to create and assign the symbolic name out of the value of a database field (i.e., out of thin air), which is dumb in any case. Hell, you can't even do that in assembler. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Best regards, Klaus H. Probst, MCP
ICQ: 22454937 The VB Box: http://members.xoom.com/kprobst/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Thomas Gans #9 / 16
|
 Dim a Variable from a string???Possible??
Quote: >i want to read my variable names from a file or database. >is it possible to do sth like: >dim rs!name as string [len(rs!name)] >where rs!name is for example "Adress" ? >I know there's a way, but I forgot how.....
Not in VB, but in FoxPro or dBase or Clipper with the dreaded "&" (=Makroexpansion). But still not a great programming style ;-) Thomas
|
Sun, 16 Sep 2001 03:00:00 GMT |
|
 |
Graham Stanbur #10 / 16
|
 Dim a Variable from a string???Possible??
Aren'y you always going to find this impossible in a compiled language, where the variables namespace is not available to you. Once compiled, variable names don't mean anything I suppose that if you really wanted to you could set up some kind of binding structure which would bind you variables to something in a database (a bit like the DFX mechanism used by DAO for C++), but it would be a lot of work, and still requires recompilation if the variables change. Graham Quote:
>>i want to read my variable names from a file or database. >>is it possible to do sth like:
|
Mon, 17 Sep 2001 03:00:00 GMT |
|
 |
Larry Serflate #11 / 16
|
 Dim a Variable from a string???Possible??
I argree with Klaus to the effect that placing a value in a collection would not be considered dimensioning the 'key', although, the collection may be re-dimensioning itself. The solution Bob gave, appears to be what the poster was looking for, he may have used Dim for lack of a better term. I would define dimensioning more like the method that assigns (RAM) memory to a named variable, based on its specific data type. The collection does not allow full access, and the memory used is not dependant on any named data type. You don't dimension a constant, for example, yet it would fit your description.... LFS Quote:
> Klaus... > >Well, yes, that's possible. But that's another thing entirely, because > >you're storing values, not declaring variables. > Not to be argumentative, but in the most technical sense it is declaring a > variable. A variable is simply a named location in memory that stores a > value, which is exactly what Bob was doing with his collection example. > Ben Baird, MVP
|
Mon, 17 Sep 2001 03:00:00 GMT |
|
 |
MSMV #12 / 16
|
 Dim a Variable from a string???Possible??
Klaus... Quote: >Actually, no, I don't agree with you there. In the case of a collection >what you're doing is allocating memory to hold a variable in an indexed >sequence of memory addresses, placing the variable there, and then using a >key (or index) to gain access to that address.
I noticed that while you disagree, at the same time you yourself call it a variable. So I ask you: Quote: >In the case of a collection >what you're doing is allocating memory
-- Was the memory allocated before the new item was added, or was it allocated to make room for it? Quote: >to hold a variable in an indexed >sequence of memory addresses
-- Does the fact that the value is contained in an "indexed sequence" of addresses make it any less a variable? If so, then what do you call an array? Quote: >and then using a >key (or index) to gain access to that address.
Any variables you declare within your own code are accessed through keys and indexes. The "Dim" keyword stands for "Dimension", which essentially means making room in memory for a new value. When you add an item to a collection, the collection must be re-*dimensioned* to hold it. The fact that you access it via a string has nothing to do with whether it is a variable or not. As far as VB is concerned, your "selector" or "symbolic name" is a string (or "key") in the code window, and an index once compiled. Don't think that just because the Dim keyword wasn't used, the value cannot be a variable. -- Ben Baird, MVP Visual Basic Thunder http://www.vbthunder.com Common Controls Replacement Project, Official Member http://www.mvps.org/ccrp/ Please direct your programming questions to the newsgroups.
|
Mon, 17 Sep 2001 03:00:00 GMT |
|
 |
Larry Serflate #13 / 16
|
 Dim a Variable from a string???Possible??
I have to smile, nit picking over words.... 2 points: Quote:
> When you add an item to a collection, the > collection must be re-*dimensioned* to hold it.
The new value is accessed through the collection, it is the collection's Count property that increases. How is that re-dimensioning being attributed to the 'Key'? Quote: > Don't think that just because the Dim keyword wasn't used, the value cannot > be a variable.
The definition of a variable precludes your assertion: (Glossary quote for 'Variable') "A named storage location that can contain data that can be modified during program execution." Values in a Collection cannot be modified, only added or removed, full access is not allowed. Another tip off comes from the Help file on Dim: [Dim] "Declares variables and allocates storage space." ... "Use the Dim statement at module or procedure level to declare the data type of a variable." The purpose of Dim is to declare a data type, and give it a name. In the case of a collection, a member is added, and given an index. So... Dim a variable from a string? Nope. (Not with standard VB commands) Create and Use a variable from a string? Yes. (Declare it in a Collection) LFS
|
Mon, 17 Sep 2001 03:00:00 GMT |
|
 |
MSMV #14 / 16
|
 Dim a Variable from a string???Possible??
Larry... Quote: >I have to smile, nit picking over words....
I thought it was pretty funny myself, but hey... :-) Quote: >> When you add an item to a collection, the >> collection must be re-*dimensioned* to hold it. >The new value is accessed through the collection, it is the >collection's Count property that increases.
As well as the collection's size. Quote: >How is that >re-dimensioning being attributed to the 'Key'?
I believe all I said is that the new item is accessed through the Key. If not, well, that's all I meant. Quote: >The definition of a variable precludes your assertion: >(Glossary quote for 'Variable') >"A named storage location that can contain data that can be modified >during program execution." >Values in a Collection cannot be modified, only added or removed, >full access is not allowed.
I would challenge the definition given, but I don't wish to prolong the discussion more than need be. As the saying goes, "You're entitled to your opinion -- even if it is wrong." <g> Quote: >The purpose of Dim is to declare a data type, and give it a name. In the >case of a collection, a member is added, and given an index. So...
The member has data type as well -- however, much like VBScript, you have no control over what that type is. As for the index... variables declared with Dim also have an index, more commonly called a memory address. Quote: >Dim a variable from a string? Nope. (Not with standard VB commands) >Create and Use a variable from a string? Yes. (Declare it in a Collection)
I'll agree with that much, anyway :-) -- Ben Baird, MVP Visual Basic Thunder http://www.vbthunder.com Common Controls Replacement Project, Official Member http://www.mvps.org/ccrp/ Please direct your programming questions to the newsgroups.
|
Mon, 17 Sep 2001 03:00:00 GMT |
|
 |
Klaus H. Probs #15 / 16
|
 Dim a Variable from a string???Possible??
Ben, Quote: > I noticed that while you disagree, at the same time you yourself call it a > variable. So I ask you:
A variable in the sense that it's a memory address with a modifiable value. But I cannot bring myself to think of collection or array indices as "variables" in the "Dim X As Y" type of memory allocation through a compiler-recognized symbolic name. Quote: > -- Was the memory allocated before the new item was added, or was it > allocated to make room for it?
The array grew dynamically by so many bytes to hold an indexed pointer to a new address with a new value. *Where* the address is need not concern us -- just the fact that I can access it through an index or a key. I consider an array index to be part of the variable that the array actually is. So, again, I can't call it a variable all by itself. Quote: > -- Does the fact that the value is contained in an "indexed sequence" of > addresses make it any less a variable? If so, then what do you call an > array?
Again, the variable is the array. Individual items inside the array are not, IMO, variables. Well, they are, but not in the strict sense of the word. Quote: > >and then using a > >key (or index) to gain access to that address. > Any variables you declare within your own code are accessed through keys and > indexes.
No, that's the difference. An array index does not have a symbolic name. Just an index *into* a symbolic name, because the container --if you will-- is the array, which does have a symbolic name. Quote: > The "Dim" keyword stands for "Dimension", which essentially means making > room in memory for a new value. When you add an item to a collection, the > collection must be re-*dimensioned* to hold it. The fact that you access
it *Dimension* is the act of allocating space either on the stack or on the heap, *before* the program or the procedure is actually executed. Adding a new item to an array just grows the memory address to hold the new item(s). Granted, in terms of memory allocation it is theoretically the same thing, but the difference --and I think we've gotten away from the purpose of the original post-- is that you cannot "create" a symbolic name from another one, or from the value of another variable or whatever. I don't think that's a limitation of any language. It's just how computer languages work. Thus Bob Butler's suggestion of using a collection creates a viable alternative to the original impossible question. Quote: > via a string has nothing to do with whether it is a variable or not. As far > as VB is concerned, your "selector" or "symbolic name" is a string (or > "key") in the code window, and an index once compiled.
Well, I guess I'm being argumentative Ben, but I see it differently from you. Quote: > Don't think that just because the Dim keyword wasn't used, the value cannot > be a variable.
Uhmpf. Again, it is a variable in the sense that it's an indexed address of memory whose value can be modified. But it does not have a symbolic name. The array holds the symbolic name, and everything else goes through that. "Redim" in VB is used to grow or shrink an existing array, not to create new, independent variables with their own symbolic names. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Best regards, Klaus H. Probst, MCP
ICQ: 22454937 The VB Box: http://members.xoom.com/kprobst/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Mon, 17 Sep 2001 03:00:00 GMT |
|
|
Page 1 of 2
|
[ 16 post ] |
|
Go to page:
[1]
[2] |
|