Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Newbie question: Can the class object member be specified as a variable?

Reply
Thread Tools

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

 
 
MDBloemker
Guest
Posts: n/a
 
      05-09-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
CT
Guest
Posts: n/a
 
      05-09-2004
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" <(E-Mail Removed)> wrote in message
news:Vstnc.82233$(E-Mail Removed).. .
> 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 Removed)
>
>



 
Reply With Quote
 
 
 
 
MDBloemker
Guest
Posts: n/a
 
      05-09-2004
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 Removed)

"CT" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:Vstnc.82233$(E-Mail Removed).. .
> > 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 Removed)
> >
> >

>
>



 
Reply With Quote
 
David Jessee
Guest
Posts: n/a
 
      05-09-2004
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" <(E-Mail Removed)> wrote in message
news:Vstnc.82233$(E-Mail Removed).. .
> 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 Removed)
>
>



 
Reply With Quote
 
David Jessee
Guest
Posts: n/a
 
      05-09-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:Vstnc.82233$(E-Mail Removed).. .
> > 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 Removed)
> >
> >

>
>



 
Reply With Quote
 
MDBloemker
Guest
Posts: n/a
 
      05-09-2004
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 Removed)

"David Jessee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:Vstnc.82233$(E-Mail Removed).. .
> > > 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 Removed)
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
MDBloemker
Guest
Posts: n/a
 
      05-09-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:Vstnc.82233$(E-Mail Removed).. .
> > > 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 Removed)
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Rick Spiewak
Guest
Posts: n/a
 
      05-10-2004
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" <(E-Mail Removed)> wrote in message
news:O_wnc.83815$(E-Mail Removed).. .
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:Vstnc.82233$(E-Mail Removed).. .
> > > > 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 Removed)
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
MDBloemker
Guest
Posts: n/a
 
      05-10-2004
"Rick Spiewak" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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 Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
member function with no class name specified John Goche C++ 3 11-16-2006 05:36 PM
member function with no class name specified jackgoche@googlemail.com C++ 2 11-16-2006 04:59 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Differences between "class::member" & "object.member" Jian H. Li C++ 4 02-13-2004 10:54 AM



Advertisments