Is it possible to assign dynamic variables?

  • Thread starter Thread starter Shelby
  • Start date Start date
S

Shelby

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?
 
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
 
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
 
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?
 
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?
 
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?
 
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
 
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)
 
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
 
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
 
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
 
* "Shelby said:
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.
 
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
 
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
 
Back
Top