PC Review


Reply
Thread Tools Rate Thread

Call the property of a class using a variable

 
 
Rob Wills
Guest
Posts: n/a
 
      11th Aug 2008
I'm sure this is possible.

I have written a class module which contains a number of properties.

I wish to change the value of one of the properties, however I want to
dynamically identify the property that I'm changing

For example: if my class has integer properties of X and Y I wish to be able
to do somthing similar to below

========================================
Function Change_Value(strProperty as String, iValue as Integer)
Dim objClass As myClass

Set objClass = New myClass

objclass(strproperty) = iValue

End Function
=========================================

Thanks in Advance
Rob

 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      11th Aug 2008
You can do it but it is a bit of a brute force attack... Define our get and
let to take a string argument and then you can use a select case to evaluate
the string input to determine the appropriate property...

Function Change_Value(strProperty as String, iValue as Integer)
Dim objClass As myClass

Set objClass = New myClass

objclass.LetVariableProperty(strproperty) = iValue

End Function

--
HTH...

Jim Thomlinson


"Rob Wills" wrote:

> I'm sure this is possible.
>
> I have written a class module which contains a number of properties.
>
> I wish to change the value of one of the properties, however I want to
> dynamically identify the property that I'm changing
>
> For example: if my class has integer properties of X and Y I wish to be able
> to do somthing similar to below
>
> ========================================
> Function Change_Value(strProperty as String, iValue as Integer)
> Dim objClass As myClass
>
> Set objClass = New myClass
>
> objclass(strproperty) = iValue
>
> End Function
> =========================================
>
> Thanks in Advance
> Rob
>

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      11th Aug 2008
Function Change_Value(strProperty As String, iValue As Integer)
Dim objClass As MyClass

Set objClass = New MyClass

CallByName objClass, strProperty, VbLet, iValue

End Function


--
__________________________________
HTH

Bob

"Rob Wills" <(E-Mail Removed)> wrote in message
news:ED321F1D-F062-4D1E-B631-(E-Mail Removed)...
> I'm sure this is possible.
>
> I have written a class module which contains a number of properties.
>
> I wish to change the value of one of the properties, however I want to
> dynamically identify the property that I'm changing
>
> For example: if my class has integer properties of X and Y I wish to be
> able
> to do somthing similar to below
>
> ========================================
> Function Change_Value(strProperty as String, iValue as Integer)
> Dim objClass As myClass
>
> Set objClass = New myClass
>
> objclass(strproperty) = iValue
>
> End Function
> =========================================
>
> Thanks in Advance
> Rob
>



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      11th Aug 2008
I can't imagine ever doing this (other ways) but as you ask

' code in normal module

Dim c As Class1

Function foo(sProp As String, LetGet As VbCallType, arg) As Long

If c Is Nothing Then Set c = New Class1

If LetGet = VbLet Then
CallByName c, sProp, LetGet, CLng(arg)
ElseIf LetGet = VbGet Then
foo = CallByName(c, sProp, LetGet)
End If

End Function

Sub test()
arrProps = Array("PropA", "PropB")
arrvals = Array(100&, 200&)

For i = 0 To 1
foo CStr(arrProps(i)), VbLet, arrvals(i)
Next

For i = 0 To 1
Debug.Print foo(CStr(arrProps(i)), VbGet, a)
Next

Set c = Nothing
End Sub
''''''' end normal module

''''' code in Class1

Dim x As Long
Dim y As Long

Public Property Let propA(n As Long)
x = n
End Property
Public Property Get propA() As Long
propA = x
End Property

Public Property Let propB(n As Long)
y = n
End Property
Public Property Get propB() As Long
propB = y
End Property

''''' end Class1

Regards,
Peter T

"Rob Wills" <(E-Mail Removed)> wrote in message
news:ED321F1D-F062-4D1E-B631-(E-Mail Removed)...
> I'm sure this is possible.
>
> I have written a class module which contains a number of properties.
>
> I wish to change the value of one of the properties, however I want to
> dynamically identify the property that I'm changing
>
> For example: if my class has integer properties of X and Y I wish to be
> able
> to do somthing similar to below
>
> ========================================
> Function Change_Value(strProperty as String, iValue as Integer)
> Dim objClass As myClass
>
> Set objClass = New myClass
>
> objclass(strproperty) = iValue
>
> End Function
> =========================================
>
> Thanks in Advance
> Rob
>



 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      11th Aug 2008
Well that's a new one for me to look into... I don't do classes that often,
but that could be handy. General interest question. Do you actually know
everything or does it just appear that way to the rest of us... ;-)

To the OP... here is a resource on CallByName
http://msdn.microsoft.com/en-us/library/chsc1tx6(VS.80).aspx
--
HTH...

Jim Thomlinson


"Bob Phillips" wrote:

> Function Change_Value(strProperty As String, iValue As Integer)
> Dim objClass As MyClass
>
> Set objClass = New MyClass
>
> CallByName objClass, strProperty, VbLet, iValue
>
> End Function
>
>
> --
> __________________________________
> HTH
>
> Bob
>
> "Rob Wills" <(E-Mail Removed)> wrote in message
> news:ED321F1D-F062-4D1E-B631-(E-Mail Removed)...
> > I'm sure this is possible.
> >
> > I have written a class module which contains a number of properties.
> >
> > I wish to change the value of one of the properties, however I want to
> > dynamically identify the property that I'm changing
> >
> > For example: if my class has integer properties of X and Y I wish to be
> > able
> > to do somthing similar to below
> >
> > ========================================
> > Function Change_Value(strProperty as String, iValue as Integer)
> > Dim objClass As myClass
> >
> > Set objClass = New myClass
> >
> > objclass(strproperty) = iValue
> >
> > End Function
> > =========================================
> >
> > Thanks in Advance
> > Rob
> >

>
>
>

 
Reply With Quote
 
JMB
Guest
Posts: n/a
 
      12th Aug 2008
At one time, he was working on a paper regarding the use of class modules. I
think he got sidetracked or was just teasing me <g>

"Jim Thomlinson" wrote:

> Well that's a new one for me to look into... I don't do classes that often,
> but that could be handy. General interest question. Do you actually know
> everything or does it just appear that way to the rest of us... ;-)
>
> To the OP... here is a resource on CallByName
> http://msdn.microsoft.com/en-us/library/chsc1tx6(VS.80).aspx
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Bob Phillips" wrote:
>
> > Function Change_Value(strProperty As String, iValue As Integer)
> > Dim objClass As MyClass
> >
> > Set objClass = New MyClass
> >
> > CallByName objClass, strProperty, VbLet, iValue
> >
> > End Function
> >
> >
> > --
> > __________________________________
> > HTH
> >
> > Bob
> >
> > "Rob Wills" <(E-Mail Removed)> wrote in message
> > news:ED321F1D-F062-4D1E-B631-(E-Mail Removed)...
> > > I'm sure this is possible.
> > >
> > > I have written a class module which contains a number of properties.
> > >
> > > I wish to change the value of one of the properties, however I want to
> > > dynamically identify the property that I'm changing
> > >
> > > For example: if my class has integer properties of X and Y I wish to be
> > > able
> > > to do somthing similar to below
> > >
> > > ========================================
> > > Function Change_Value(strProperty as String, iValue as Integer)
> > > Dim objClass As myClass
> > >
> > > Set objClass = New myClass
> > >
> > > objclass(strproperty) = iValue
> > >
> > > End Function
> > > =========================================
> > >
> > > Thanks in Advance
> > > Rob
> > >

> >
> >
> >

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      12th Aug 2008
You are absolutely right, on both counts. I was/am working on it, and I got
sidetracked. The hardest part is getting a meaningful case to build a class
upon. Although I use them extensively, I need to find a custom object that
will ring a bell with the target audience. Most people use the ubiquitous
person class, which I think falls far short of the mark.

But I must admit that I had not intended to introduce CallByName within that
paper, I never use it, and I don't see much use for it personally.

--
__________________________________
HTH

Bob

"JMB" <(E-Mail Removed)> wrote in message
news:9779B477-7543-4C56-8586-(E-Mail Removed)...
> At one time, he was working on a paper regarding the use of class modules.
> I
> think he got sidetracked or was just teasing me <g>
>
> "Jim Thomlinson" wrote:
>
>> Well that's a new one for me to look into... I don't do classes that
>> often,
>> but that could be handy. General interest question. Do you actually know
>> everything or does it just appear that way to the rest of us... ;-)
>>
>> To the OP... here is a resource on CallByName
>> http://msdn.microsoft.com/en-us/library/chsc1tx6(VS.80).aspx
>> --
>> HTH...
>>
>> Jim Thomlinson
>>
>>
>> "Bob Phillips" wrote:
>>
>> > Function Change_Value(strProperty As String, iValue As Integer)
>> > Dim objClass As MyClass
>> >
>> > Set objClass = New MyClass
>> >
>> > CallByName objClass, strProperty, VbLet, iValue
>> >
>> > End Function
>> >
>> >
>> > --
>> > __________________________________
>> > HTH
>> >
>> > Bob
>> >
>> > "Rob Wills" <(E-Mail Removed)> wrote in message
>> > news:ED321F1D-F062-4D1E-B631-(E-Mail Removed)...
>> > > I'm sure this is possible.
>> > >
>> > > I have written a class module which contains a number of properties.
>> > >
>> > > I wish to change the value of one of the properties, however I want
>> > > to
>> > > dynamically identify the property that I'm changing
>> > >
>> > > For example: if my class has integer properties of X and Y I wish to
>> > > be
>> > > able
>> > > to do somthing similar to below
>> > >
>> > > ========================================
>> > > Function Change_Value(strProperty as String, iValue as Integer)
>> > > Dim objClass As myClass
>> > >
>> > > Set objClass = New myClass
>> > >
>> > > objclass(strproperty) = iValue
>> > >
>> > > End Function
>> > > =========================================
>> > >
>> > > Thanks in Advance
>> > > Rob
>> > >
>> >
>> >
>> >



 
Reply With Quote
 
JMB
Guest
Posts: n/a
 
      13th Aug 2008
When I began learning about classes, I attempted a sudoku puzzle. I figured
it offered opportunities to learn custom classes, events, and parent child
relationships. But I got sidetracked and never finished it <g>. I've never
really found a lot of practical uses for custom classes in my work (or I'm
not using enough imagination), so my interest is mostly academic at this
point.

"Bob Phillips" wrote:

> You are absolutely right, on both counts. I was/am working on it, and I got
> sidetracked. The hardest part is getting a meaningful case to build a class
> upon. Although I use them extensively, I need to find a custom object that
> will ring a bell with the target audience. Most people use the ubiquitous
> person class, which I think falls far short of the mark.
>
> But I must admit that I had not intended to introduce CallByName within that
> paper, I never use it, and I don't see much use for it personally.
>
> --
> __________________________________
> HTH
>
> Bob
>
> "JMB" <(E-Mail Removed)> wrote in message
> news:9779B477-7543-4C56-8586-(E-Mail Removed)...
> > At one time, he was working on a paper regarding the use of class modules.
> > I
> > think he got sidetracked or was just teasing me <g>
> >
> > "Jim Thomlinson" wrote:
> >
> >> Well that's a new one for me to look into... I don't do classes that
> >> often,
> >> but that could be handy. General interest question. Do you actually know
> >> everything or does it just appear that way to the rest of us... ;-)
> >>
> >> To the OP... here is a resource on CallByName
> >> http://msdn.microsoft.com/en-us/library/chsc1tx6(VS.80).aspx
> >> --
> >> HTH...
> >>
> >> Jim Thomlinson
> >>
> >>
> >> "Bob Phillips" wrote:
> >>
> >> > Function Change_Value(strProperty As String, iValue As Integer)
> >> > Dim objClass As MyClass
> >> >
> >> > Set objClass = New MyClass
> >> >
> >> > CallByName objClass, strProperty, VbLet, iValue
> >> >
> >> > End Function
> >> >
> >> >
> >> > --
> >> > __________________________________
> >> > HTH
> >> >
> >> > Bob
> >> >
> >> > "Rob Wills" <(E-Mail Removed)> wrote in message
> >> > news:ED321F1D-F062-4D1E-B631-(E-Mail Removed)...
> >> > > I'm sure this is possible.
> >> > >
> >> > > I have written a class module which contains a number of properties.
> >> > >
> >> > > I wish to change the value of one of the properties, however I want
> >> > > to
> >> > > dynamically identify the property that I'm changing
> >> > >
> >> > > For example: if my class has integer properties of X and Y I wish to
> >> > > be
> >> > > able
> >> > > to do somthing similar to below
> >> > >
> >> > > ========================================
> >> > > Function Change_Value(strProperty as String, iValue as Integer)
> >> > > Dim objClass As myClass
> >> > >
> >> > > Set objClass = New myClass
> >> > >
> >> > > objclass(strproperty) = iValue
> >> > >
> >> > > End Function
> >> > > =========================================
> >> > >
> >> > > Thanks in Advance
> >> > > Rob
> >> > >
> >> >
> >> >
> >> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Property vs Variable of class Audwin Microsoft Dot NET Framework 12 18th Jul 2007 11:06 AM
cant assign variable values to property in Class file Avi Microsoft VB .NET 4 12th Dec 2006 01:12 PM
Get class property value where name in variable rambalep Microsoft VB .NET 3 27th Oct 2005 05:45 PM
Modify/Access private field from within the class through property or variable? Mark Microsoft C# .NET 1 25th Mar 2004 05:06 PM
private variable vs. actual property in a class module? SteveS Microsoft Dot NET 1 13th Jan 2004 02:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:07 PM.