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

Discussion in 'ASP .Net' started by MDBloemker, May 9, 2004.

  1. MDBloemker

    MDBloemker Guest

    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
    MDBloemker, May 9, 2004
    #1
    1. Advertising

  2. MDBloemker

    CT Guest

    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" <> wrote in message
    news:Vstnc.82233$...
    > 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
    >
    >
    >
    CT, May 9, 2004
    #2
    1. Advertising

  3. MDBloemker

    MDBloemker Guest

    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


    "CT" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:Vstnc.82233$...
    > > 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
    > >
    > >
    > >

    >
    >
    MDBloemker, May 9, 2004
    #3
  4. MDBloemker

    David Jessee Guest

    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" <> wrote in message
    news:Vstnc.82233$...
    > 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
    >
    >
    >
    David Jessee, May 9, 2004
    #4
  5. MDBloemker

    David Jessee Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:Vstnc.82233$...
    > > 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
    > >
    > >
    > >

    >
    >
    David Jessee, May 9, 2004
    #5
  6. MDBloemker

    MDBloemker Guest

    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


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

    > >
    > >

    >
    >
    MDBloemker, May 9, 2004
    #6
  7. MDBloemker

    MDBloemker Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:Vstnc.82233$...
    > > > 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
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
    MDBloemker, May 9, 2004
    #7
  8. MDBloemker

    Rick Spiewak Guest

    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" <> wrote in message
    news:O_wnc.83815$...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > news:Vstnc.82233$...
    > > > > 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
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Rick Spiewak, May 10, 2004
    #8
  9. MDBloemker

    MDBloemker Guest

    "Rick Spiewak" <> wrote in message news:<>...
    > 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
    MDBloemker, May 10, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. Siemel Naran
    Replies:
    4
    Views:
    790
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    2
    Views:
    270
    Victor Bazarov
    Nov 16, 2006
  4. John Goche
    Replies:
    3
    Views:
    273
    Victor Bazarov
    Nov 16, 2006
  5. A. Farber
    Replies:
    5
    Views:
    157
    A. Farber
    Aug 23, 2008
Loading...

Share This Page