Newbie question: Can the class object member be specified as a variable?

M

MDBloemker

I hope this is the right place for this question, and I hope I can explain
the question clearly enough to be understood. I'm using VB.Net. When I
instantiate a new object from a user-defined class, can I access its members
via a variable? For example, if I have a variable (say, marray(4)) holding
the string value 'ModDate', and I have a class object of EmpRecord that
holds the member ModDate, how can I do something along the lines of:

EmpRecord.[marray(4)] = rdr(marray(2))

Or is it even possible? If not, is there a viable alternative to assigned a
class object member to a corresponding DataReader member? Thanks!!

MDBloemker
(e-mail address removed)
 
C

CT

Yes you can, if your varibale is declared with the Public access modifier,
or even better if you expose it as a public property, and declare your
member variable as private.
 
M

MDBloemker

Aha! My savior! Many, many thanks for the really appreciated quick response.
Now to squander a sunny Sunday making things work in VB.Net....

MD Bloemker
(e-mail address removed)

CT said:
Yes you can, if your varibale is declared with the Public access modifier,
or even better if you expose it as a public property, and declare your
member variable as private.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, and MSF
http://www.apress.com/book/bookDisplay.html?bID=105
MDBloemker said:
I hope this is the right place for this question, and I hope I can explain
the question clearly enough to be understood. I'm using VB.Net. When I
instantiate a new object from a user-defined class, can I access its members
via a variable? For example, if I have a variable (say, marray(4)) holding
the string value 'ModDate', and I have a class object of EmpRecord that
holds the member ModDate, how can I do something along the lines of:

EmpRecord.[marray(4)] = rdr(marray(2))

Or is it even possible? If not, is there a viable alternative to
assigned
a
class object member to a corresponding DataReader member? Thanks!!

MDBloemker
(e-mail address removed)
 
D

David Jessee

Well, if you want to do it that way, it is possible...
I'm guessing from your syntactic example that you're working with C#

It is possible to create an indexer for your class. Indexets act as the
default property for your class. This can de an inxeder that takes either
an integer or a string as a key. Therefore you could end up with a syntax
that looks like...
MyClass("Key")=value

Which would get what you need. You'd need to, internal to this property
check which key is being passed and then set the value you're getting to the
appropriate internal variable or (preferably) use that key to call the
appropriate Property Procedure.

This limitation to this is that it does take extra implementation inside of
your class and, should you need an indexer in the future, you will be unable
to use that functionality since a class can only have one indexer per index
type (one integer index and/or one string index). The Best way to achieve
this would be to analyze the class that you're looking at and set the
appropriate property through reflection. That way the class doesn't need
any additional implementation to achieve this funcitonality. I'm saying
this because what you're trying to do, at a larger level, is USE THE CLASS
in a different way, as opposed to CHANGING THE CLASS'S BEHAVIOR.

For information on class indexes, check out the Visual Studio help section.
It tells you exactly what to do. If you want to go through reflection to do
this (reflection is VERY POWERFUL...there'll be a learning curve, but
you'll get a lot out of it) Then do a web search on the
PropertyInfo.SetValue Method.
 
D

David Jessee

I just realized you're looking at vb.net instead...You could do this..

Public Default Property SomeField(FieldName as String) as Object
Get
....get the appropriate field based on FieldName
End Get
Set (Value as Object)
...Set the appropriate field based on FieldName
End Set
End Property

Yet again, though, I'd recommend going through reflection

David Jessee said:
Well, if you want to do it that way, it is possible...
I'm guessing from your syntactic example that you're working with C#

It is possible to create an indexer for your class. Indexets act as the
default property for your class. This can de an inxeder that takes either
an integer or a string as a key. Therefore you could end up with a syntax
that looks like...
MyClass("Key")=value

Which would get what you need. You'd need to, internal to this property
check which key is being passed and then set the value you're getting to the
appropriate internal variable or (preferably) use that key to call the
appropriate Property Procedure.

This limitation to this is that it does take extra implementation inside of
your class and, should you need an indexer in the future, you will be unable
to use that functionality since a class can only have one indexer per index
type (one integer index and/or one string index). The Best way to achieve
this would be to analyze the class that you're looking at and set the
appropriate property through reflection. That way the class doesn't need
any additional implementation to achieve this funcitonality. I'm saying
this because what you're trying to do, at a larger level, is USE THE CLASS
in a different way, as opposed to CHANGING THE CLASS'S BEHAVIOR.

For information on class indexes, check out the Visual Studio help section.
It tells you exactly what to do. If you want to go through reflection to do
this (reflection is VERY POWERFUL...there'll be a learning curve, but
you'll get a lot out of it) Then do a web search on the
PropertyInfo.SetValue Method.



MDBloemker said:
I hope this is the right place for this question, and I hope I can explain
the question clearly enough to be understood. I'm using VB.Net. When I
instantiate a new object from a user-defined class, can I access its members
via a variable? For example, if I have a variable (say, marray(4)) holding
the string value 'ModDate', and I have a class object of EmpRecord that
holds the member ModDate, how can I do something along the lines of:

EmpRecord.[marray(4)] = rdr(marray(2))

Or is it even possible? If not, is there a viable alternative to
assigned
a
class object member to a corresponding DataReader member? Thanks!!

MDBloemker
(e-mail address removed)
 
M

MDBloemker

Yes, VB.Net. But you've given me a solid grasp of the direction to go and
some invaluable keywords for my search through help to get done what I want
to get done. Sometimes, all it takes is the right keyword.

MDBloemker
(e-mail address removed)

David Jessee said:
I just realized you're looking at vb.net instead...You could do this..

Public Default Property SomeField(FieldName as String) as Object
Get
....get the appropriate field based on FieldName
End Get
Set (Value as Object)
...Set the appropriate field based on FieldName
End Set
End Property

Yet again, though, I'd recommend going through reflection

David Jessee said:
Well, if you want to do it that way, it is possible...
I'm guessing from your syntactic example that you're working with C#

It is possible to create an indexer for your class. Indexets act as the
default property for your class. This can de an inxeder that takes either
an integer or a string as a key. Therefore you could end up with a syntax
that looks like...
MyClass("Key")=value

Which would get what you need. You'd need to, internal to this property
check which key is being passed and then set the value you're getting to the
appropriate internal variable or (preferably) use that key to call the
appropriate Property Procedure.

This limitation to this is that it does take extra implementation inside of
your class and, should you need an indexer in the future, you will be unable
to use that functionality since a class can only have one indexer per index
type (one integer index and/or one string index). The Best way to achieve
this would be to analyze the class that you're looking at and set the
appropriate property through reflection. That way the class doesn't need
any additional implementation to achieve this funcitonality. I'm saying
this because what you're trying to do, at a larger level, is USE THE CLASS
in a different way, as opposed to CHANGING THE CLASS'S BEHAVIOR.

For information on class indexes, check out the Visual Studio help section.
It tells you exactly what to do. If you want to go through reflection
to
do
this (reflection is VERY POWERFUL...there'll be a learning curve, but
you'll get a lot out of it) Then do a web search on the
PropertyInfo.SetValue Method.



MDBloemker said:
I hope this is the right place for this question, and I hope I can explain
the question clearly enough to be understood. I'm using VB.Net. When I
instantiate a new object from a user-defined class, can I access its members
via a variable? For example, if I have a variable (say, marray(4)) holding
the string value 'ModDate', and I have a class object of EmpRecord that
holds the member ModDate, how can I do something along the lines of:

EmpRecord.[marray(4)] = rdr(marray(2))

Or is it even possible? If not, is there a viable alternative to
assigned
a
class object member to a corresponding DataReader member? Thanks!!

MDBloemker
(e-mail address removed)
 
M

MDBloemker

Well, I'm making some headway, but it's still not quite there. As a newbie,
I don't feel I completely understand reflection, just that it was something
to do with using variables to access properties, such as using
;'dreader("ColName")' instead of 'dreader.GetString(2)' and the like. This
method is actually the first one I tried, and it works great on the
datareader part, but falls down when I try to use it to set a value into a
new instance of an object.
Using this method:
Default Public Property Whatsis(ByVal arrstr As String) As String

Get

'....get the appropriate field based on FieldName

Return mvarWhatsis

End Get

Set(ByVal Value As String)

' ...Set the appropriate field based on FieldName

mvarWhatsis = Value

End Set

End Property


And this piece of code:
If rdr.Read() Then

For z = 1 To enda

mvar = marray(z) ' marray() = columnnames, same in class and reader

arrstr = rdr(mvar) ' string = reader.columnname

SRObj(mvar) = arrstr

Next

End If






I can see in Debug mode that arrstr is getting set correctly, but mvar comes
back as the default property, Whatsis, not the columnname. Of course it
does, What am I missing? Another variable, maybe?
David Jessee said:
I just realized you're looking at vb.net instead...You could do this..

Public Default Property SomeField(FieldName as String) as Object
Get
....get the appropriate field based on FieldName
End Get
Set (Value as Object)
...Set the appropriate field based on FieldName
End Set
End Property

Yet again, though, I'd recommend going through reflection

David Jessee said:
Well, if you want to do it that way, it is possible...
I'm guessing from your syntactic example that you're working with C#

It is possible to create an indexer for your class. Indexets act as the
default property for your class. This can de an inxeder that takes either
an integer or a string as a key. Therefore you could end up with a syntax
that looks like...
MyClass("Key")=value

Which would get what you need. You'd need to, internal to this property
check which key is being passed and then set the value you're getting to the
appropriate internal variable or (preferably) use that key to call the
appropriate Property Procedure.

This limitation to this is that it does take extra implementation inside of
your class and, should you need an indexer in the future, you will be unable
to use that functionality since a class can only have one indexer per index
type (one integer index and/or one string index). The Best way to achieve
this would be to analyze the class that you're looking at and set the
appropriate property through reflection. That way the class doesn't need
any additional implementation to achieve this funcitonality. I'm saying
this because what you're trying to do, at a larger level, is USE THE CLASS
in a different way, as opposed to CHANGING THE CLASS'S BEHAVIOR.

For information on class indexes, check out the Visual Studio help section.
It tells you exactly what to do. If you want to go through reflection
to
do
this (reflection is VERY POWERFUL...there'll be a learning curve, but
you'll get a lot out of it) Then do a web search on the
PropertyInfo.SetValue Method.



MDBloemker said:
I hope this is the right place for this question, and I hope I can explain
the question clearly enough to be understood. I'm using VB.Net. When I
instantiate a new object from a user-defined class, can I access its members
via a variable? For example, if I have a variable (say, marray(4)) holding
the string value 'ModDate', and I have a class object of EmpRecord that
holds the member ModDate, how can I do something along the lines of:

EmpRecord.[marray(4)] = rdr(marray(2))

Or is it even possible? If not, is there a viable alternative to
assigned
a
class object member to a corresponding DataReader member? Thanks!!

MDBloemker
(e-mail address removed)
 
R

Rick Spiewak

Here are some methods which I wrote to support routines which use database
field names. The property names of the class much match the database field
names. This property name is changed to lower case to match the properties,
which are named in all lower case, because Reflection is case sensitive

Public Sub SetPropertyByName(ByVal PropertyName As String, ByVal value As
Object)
Dim BusinessObjectProperty As PropertyInfo
BusinessObjectProperty =
Me.GetType.GetProperty(PropertyName.ToLower)
With BusinessObjectProperty
If .PropertyType Is value.GetType Then
.SetValue(Me, value, Nothing)
Me.Modified = True
Else
Throw New ArgumentException("Property Type Mismatch")
End If
End With
End Sub

Public Function GetPropertyByName(ByVal PropertyName As String) As
Object
Dim BusinessObjectProperty As PropertyInfo
BusinessObjectProperty =
Me.GetType.GetProperty(PropertyName.ToLower)
If BusinessObjectProperty Is Nothing Then Throw New
ArgumentException("Property Not Found")
Return BusinessObjectProperty.GetValue(Me, Nothing)
End Function

MDBloemker said:
Well, I'm making some headway, but it's still not quite there. As a newbie,
I don't feel I completely understand reflection, just that it was something
to do with using variables to access properties, such as using
;'dreader("ColName")' instead of 'dreader.GetString(2)' and the like. This
method is actually the first one I tried, and it works great on the
datareader part, but falls down when I try to use it to set a value into a
new instance of an object.
Using this method:
Default Public Property Whatsis(ByVal arrstr As String) As String

Get

'....get the appropriate field based on FieldName

Return mvarWhatsis

End Get

Set(ByVal Value As String)

' ...Set the appropriate field based on FieldName

mvarWhatsis = Value

End Set

End Property


And this piece of code:
If rdr.Read() Then

For z = 1 To enda

mvar = marray(z) ' marray() = columnnames, same in class and reader

arrstr = rdr(mvar) ' string = reader.columnname

SRObj(mvar) = arrstr

Next

End If






I can see in Debug mode that arrstr is getting set correctly, but mvar comes
back as the default property, Whatsis, not the columnname. Of course it
does, What am I missing? Another variable, maybe?
David Jessee said:
I just realized you're looking at vb.net instead...You could do this..

Public Default Property SomeField(FieldName as String) as Object
Get
....get the appropriate field based on FieldName
End Get
Set (Value as Object)
...Set the appropriate field based on FieldName
End Set
End Property

Yet again, though, I'd recommend going through reflection

David Jessee said:
Well, if you want to do it that way, it is possible...
I'm guessing from your syntactic example that you're working with C#

It is possible to create an indexer for your class. Indexets act as the
default property for your class. This can de an inxeder that takes either
an integer or a string as a key. Therefore you could end up with a syntax
that looks like...
MyClass("Key")=value

Which would get what you need. You'd need to, internal to this property
check which key is being passed and then set the value you're getting
to
the
appropriate internal variable or (preferably) use that key to call the
appropriate Property Procedure.

This limitation to this is that it does take extra implementation
inside
of
your class and, should you need an indexer in the future, you will be unable
to use that functionality since a class can only have one indexer per index
type (one integer index and/or one string index). The Best way to achieve
this would be to analyze the class that you're looking at and set the
appropriate property through reflection. That way the class doesn't need
any additional implementation to achieve this funcitonality. I'm saying
this because what you're trying to do, at a larger level, is USE THE CLASS
in a different way, as opposed to CHANGING THE CLASS'S BEHAVIOR.

For information on class indexes, check out the Visual Studio help section.
It tells you exactly what to do. If you want to go through reflection
to
do
this (reflection is VERY POWERFUL...there'll be a learning curve, but
you'll get a lot out of it) Then do a web search on the
PropertyInfo.SetValue Method.



I hope this is the right place for this question, and I hope I can explain
the question clearly enough to be understood. I'm using VB.Net. When I
instantiate a new object from a user-defined class, can I access its
members
via a variable? For example, if I have a variable (say, marray(4)) holding
the string value 'ModDate', and I have a class object of EmpRecord that
holds the member ModDate, how can I do something along the lines of:

EmpRecord.[marray(4)] = rdr(marray(2))

Or is it even possible? If not, is there a viable alternative to assigned
a
class object member to a corresponding DataReader member? Thanks!!

MDBloemker
(e-mail address removed)
 
M

MDBloemker

Rick Spiewak said:
Here are some methods which I wrote to support routines which use database
field names. The property names of the class much match the database field
names. This property name is changed to lower case to match the properties,
which are named in all lower case, because Reflection is case sensitive
<snip>

Aha. Light begins to dawn on Marblehead. I very much appreciate you
taking the time and effort to supply an example; unfamiliar concepts
become so much clearer with a road map. Thanks!!!

MDBloemker
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top