471,078 Members | 828 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,078 software developers and data experts.

Is it possible to assign dynamic variables?

Hi,
if I have :
rs.Fields("firstname").Value = "John"
rs.Fields("middlename").Value = "D"
rs.Fields("lastname").Value = "Paul"

Is it possible to Dim the column name and make it a variable and reassign
the value to this new variable? Because I have a long list of the above, and
I wish to loop thru it, instead of assigning it one by one.

So I hope to achieve:
while not rs.eof
firstname = "John"
.....
.....
end while
Is it possible?

Nov 20 '05 #1
13 2222
Hi Shelby

Are you sure that you are with this classic ADO question in the right
newsgroup.

Probably you need a newsgroup as

microsoft.public.data.ado
or one of the classic VB newsgroups
microsoft.public.vb*

I hope this helps?

Cor
Nov 20 '05 #2
Hi,
actually it is not so much on the ADO problem.
I just use that as an example.
The main question is about the dynamic assigning of variables.

Another example would be:

Public Structure MyUsers
Public prop1
Ens Structure

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi Shelby

Are you sure that you are with this classic ADO question in the right
newsgroup.

Probably you need a newsgroup as

microsoft.public.data.ado
or one of the classic VB newsgroups
microsoft.public.vb*

I hope this helps?

Cor

Nov 20 '05 #3
Hi,
actually it is not so much on the ADO problem.
I just use that as an example.
The main question is about the dynamic assigning of variables.

Another example would be:
--------------------------------------------
Public Structure MyUsers
Public firstname
Public middlename
.....
Public birthdate
' 20 Properties
Ens Structure
Dim prop1 as string = "firstname"
Dim prop2 as string = "birthdate"
Dim prop3 as string = "companyname"
..... and so on

I would like to have:
Dim objMyUsers as MyUsers
objMyUsers.prop1 = "John" ' So it's actually objMyUsers.firstname =
"John"
objMyUsers.prop2 = "1/5/1977" ' So it's actually objMyUsers.prop2 =
"1/5/1977"

Is it possible to do this?

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi Shelby

Are you sure that you are with this classic ADO question in the right
newsgroup.

Probably you need a newsgroup as

microsoft.public.data.ado
or one of the classic VB newsgroups
microsoft.public.vb*

I hope this helps?

Cor



Nov 20 '05 #4
Hi,
actually it is not so much on the ADO problem.
I just use that as an example.
The main question is about the dynamic assigning of variables.

Another example would be:
--------------------------------------------
Public Structure MyUsers
Public firstname
Public middlename
.....
Public birthdate
' 20 Properties
Ens Structure
Dim prop1 as string = "firstname"
Dim prop2 as string = "birthdate"
Dim prop3 as string = "companyname"
..... and so on

I would like to have:
Dim objMyUsers as MyUsers
objMyUsers.prop1 = "John" ' So it's actually objMyUsers.firstname =
"John"
objMyUsers.prop2 = "1/5/1977" ' So it's actually objMyUsers.prop2 =
"1/5/1977"

Is it possible to do this?

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi Shelby

Are you sure that you are with this classic ADO question in the right
newsgroup.

Probably you need a newsgroup as

microsoft.public.data.ado
or one of the classic VB newsgroups
microsoft.public.vb*

I hope this helps?

Cor


Nov 20 '05 #5
Hi,
actually it is not so much on the ADO problem.
I just use that as an example.
The main question is about the dynamic assigning of variables.

Another example would be:
--------------------------------------------
Public Structure MyUsers
Public firstname
Public middlename
.....
Public birthdate
' 20 Properties
Ens Structure
Dim prop1 as string = "firstname"
Dim prop2 as string = "birthdate"
Dim prop3 as string = "companyname"
..... and so on

I would like to have:
Dim objMyUsers as MyUsers
objMyUsers.prop1 = "John" ' So it's actually objMyUsers.firstname =
"John"
objMyUsers.prop2 = "1/5/1977" ' So it's actually objMyUsers.prop2 =
"1/5/1977"

Is it possible to do this?

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi Shelby

Are you sure that you are with this classic ADO question in the right
newsgroup.

Probably you need a newsgroup as

microsoft.public.data.ado
or one of the classic VB newsgroups
microsoft.public.vb*

I hope this helps?

Cor


Nov 20 '05 #6
Hi Shelby,

Is this something you are after?

Dim a As Date = Now
Dim b As Integer = 1
Dim c As String = "Hello"
Dim d As New ArrayList
d.Add(a)
d.Add(b)
d.Add(c)
For Each f As Object In d
If TypeOf f Is Date Then
MessageBox.Show(DirectCast(f, _
Date).DayOfWeek.ToString)
Else
'I only show one cast
MessageBox.Show(f.ToString)
End If
Next

If can of course as well with a for index

I hope this helps?

Cor
Nov 20 '05 #7
What you are trying to do does not make sense unless you have two
collections of corresponding Column Names and Values

then you could do

For i =0 to Collection.Count -1

Tables(0).Column( Names(i)) = CollectionOfValues(i)




--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
"Shelby" <sh****@itsupport.com.sg> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Hi,
if I have :
rs.Fields("firstname").Value = "John"
rs.Fields("middlename").Value = "D"
rs.Fields("lastname").Value = "Paul"

Is it possible to Dim the column name and make it a variable and reassign
the value to this new variable? Because I have a long list of the above, and I wish to loop thru it, instead of assigning it one by one.

So I hope to achieve:
while not rs.eof
firstname = "John"
.....
.....
end while
Is it possible?

Nov 20 '05 #8
guy
you can iterate through the properties (as well as lots of other stuff) of a class or structure using reflection, have a look at Type.GetFields etc.

hth

guy
"Shelby" wrote:
Hi,
if I have :
rs.Fields("firstname").Value = "John"
rs.Fields("middlename").Value = "D"
rs.Fields("lastname").Value = "Paul"

Is it possible to Dim the column name and make it a variable and reassign
the value to this new variable? Because I have a long list of the above, and
I wish to loop thru it, instead of assigning it one by one.

So I hope to achieve:
while not rs.eof
firstname = "John"
.....
.....
end while
Is it possible?

Nov 20 '05 #9
Hi Cor,
that is not exactly what I want.
I think the term should call Variables's variable.
Oh yes PHP does have that capapbility.
For example:
$var1 = "firstname";
$$var1 = "John";
print $firstname; // result is John

You get what I mean? So I was wondering does .Net have the Variable's
variable capability?

Thanks
Shelby
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ud**************@TK2MSFTNGP11.phx.gbl...
Hi Shelby,

Is this something you are after?

Dim a As Date = Now
Dim b As Integer = 1
Dim c As String = "Hello"
Dim d As New ArrayList
d.Add(a)
d.Add(b)
d.Add(c)
For Each f As Object In d
If TypeOf f Is Date Then
MessageBox.Show(DirectCast(f, _
Date).DayOfWeek.ToString)
Else
'I only show one cast
MessageBox.Show(f.ToString)
End If
Next

If can of course as well with a for index

I hope this helps?

Cor

Nov 20 '05 #10
Hi,
let see my actual problem. I have a file attributes.txt which contain:

AttID AttValue
firstname john
middlename D
lastname paul
birthdate 5/20/1977
.........
30 over attributes
so with that file, I would like to assign AttValue to it's AttID so that
later on, I can reference from AttID directly.

' Program should execute:
firstname = "john"
middlename = "D"
console.out.writeline (firstname)

' Output should display
john
Nov 20 '05 #11
* "Shelby" <sh****@itsupport.com.sg> scripsit:
let see my actual problem. I have a file attributes.txt which contain:

AttID AttValue
firstname john
middlename D
lastname paul
birthdate 5/20/1977
........
30 over attributes
so with that file, I would like to assign AttValue to it's AttID so that
later on, I can reference from AttID directly.

' Program should execute:
firstname = "john"
middlename = "D"
console.out.writeline (firstname)

' Output should display


You could read the file line-by-line:

\\\
Imports System.IO
..
..
..
Dim sr As StreamReader = _
New StreamReader("C:\WINDOWS\WIN.INI")
Do While sr.Peek > -1
Debug.WriteLine(sr.ReadLine())
Loop
sr.Close()
///

And then popuplate the members of an instance of a class or use a
'Hashtable' to store (key, value) pairs.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12
Hi Shelby,

I do not really see from your sample what is the benefit from that, while I
can see some benefit with my sample (it was the first time I made it)

I am trying to show you with bringing variables to objects and cast them
back, how you in my opinion can archieve your goal, did you try my sample?

I took the most difficult value "date" as example.

To set something to string is simple because in dotNet almost everything has
the method ToString.

Cor

that is not exactly what I want.
I think the term should call Variables's variable.
Oh yes PHP does have that capapbility.
For example:
$var1 = "firstname";
$$var1 = "John";
print $firstname; // result is John

You get what I mean? So I was wondering does .Net have the Variable's
variable capability?

Nov 20 '05 #13
guy
Hi shelby, read each AttId/AttValue pair from the file then use System.Type.SetField to assign the values to the variables in your class /structure.

Dim TargetField As FieldInfo
Dim TargetFields() As FieldInfo
Dim ObjectType As System.Type = SourceType '= type of your class
TargetFields = ObjectType.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Static)
For Each TargetField In TargetFields
If AttId = TargetField.Name Then
TargetField.SetValue(Target,AttValue)
End If
Next
Next
not pretty and could be tidie up alot but you should get the idea

hth

guy
"Shelby" wrote:
Hi,
let see my actual problem. I have a file attributes.txt which contain:

AttID AttValue
firstname john
middlename D
lastname paul
birthdate 5/20/1977
.........
30 over attributes
so with that file, I would like to assign AttValue to it's AttID so that
later on, I can reference from AttID directly.

' Program should execute:
firstname = "john"
middlename = "D"
console.out.writeline (firstname)

' Output should display
john

Nov 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by comp.lang.php | last post: by
1 post views Thread by Tommy Lang | last post: by
1 post views Thread by Nathan Bloomfield | last post: by
12 posts views Thread by scott | last post: by
2 posts views Thread by deejayquai | last post: by
4 posts views Thread by .Net Sports | last post: by

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.