Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Class modules: parametrize class object fields

Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax similar to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Class modules: parametrize class object fields

Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld" objects and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either

to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax similar to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Class modules: parametrize class object fields

Roedd <<NickHK wedi ysgrifennu:

Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events
etc.


Nick,

In some circles (traditional C programmers, I think) object properties are
often called 'Fields'. I think what the OP is after is soemthing like a
Properties collection for an object so that property values can be retrieved
by name. Presumably the Properties property could be made the default so
that it wouldn not have to be explicitly called.

Using an Excel analogy, imagine that a Worksheet object had a properties
collection. Then, as well as returning the name of the sheet by calling the
Name property (RetVal = Worksheets(1).Name), you could call the Properties
property (RetVal = Worksheets(1).Properties("Name") - or, if the Properties
property was the default, RetVal = Worksheets(1)("Name")).

At least I think that's what's required.

Rob


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Class modules: parametrize class object fields

Roedd <<Robert Bruce wedi ysgrifennu:


At least I think that's what's required.


I've just taken a look at this.

Say we have a simple class called Class1:

Option Explicit

Private m_strName As String

Public Property Get Name() As String
Name = m_strName
End Property

Public Property Let Name(ByVal strName As String)
m_strName = strName
End Property

'We can use callByName to provide a Properties property:

Public Property Get Properties(Name As String) As Variant
Dim varRet As Variant
varRet = CallByName(Me, Name, VbGet)
Properties = varRet
End Property

Now use a simple bas module to test:

Option Explicit

Sub test()

Dim o As New Class1

o.Name = "Hello World"

MsgBox o.Properties("Name")

End Sub

This seems to work fine. I'll leave all of the validation and error checking
as an exercise for the reader ;-)

Rob


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Class modules: parametrize class object fields

Nice! I am struggling to think of a use for it, but it is a good technique.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Robert Bruce" <rob@analytical-dynamicsdotcodotukay wrote in message
...
Roedd <<Robert Bruce wedi ysgrifennu:


At least I think that's what's required.


I've just taken a look at this.

Say we have a simple class called Class1:

Option Explicit

Private m_strName As String

Public Property Get Name() As String
Name = m_strName
End Property

Public Property Let Name(ByVal strName As String)
m_strName = strName
End Property

'We can use callByName to provide a Properties property:

Public Property Get Properties(Name As String) As Variant
Dim varRet As Variant
varRet = CallByName(Me, Name, VbGet)
Properties = varRet
End Property

Now use a simple bas module to test:

Option Explicit

Sub test()

Dim o As New Class1

o.Name = "Hello World"

MsgBox o.Properties("Name")

End Sub

This seems to work fine. I'll leave all of the validation and error

checking
as an exercise for the reader ;-)

Rob






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Class modules: parametrize class object fields

Thank you for your answer.
Sorry, I was not clear. Your answer was not what I looked for.
I'll try to formulate it again with hopefully the right terminology.
I defined a class modul "myclass" and an instance "myobj".
Set myobj = New myclass
Let assume too that this class has 2 properties: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either to
"myfld1" or to "myfld2".
Is it possible to get (resp. set) the value of the parametrized expression
myobj.{stfld} (syntax ??), that would be equal to :
- myobj.myfld1 if stfld="myfld1"
- myobj.myfld2 if stfld="myfld2"
Does it exist a way (either direct or indirect) to do it ?
Thanks for your help.


"NickHK" a écrit dans le message de news:
...
Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld" objects and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either

to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax similar
to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Class modules: parametrize class object fields

Sounds like you are looking for something like Robert's CallByName
suggestion.

NickHK

"Jean-Pierre Bidon" wrote in message
...
Thank you for your answer.
Sorry, I was not clear. Your answer was not what I looked for.
I'll try to formulate it again with hopefully the right terminology.
I defined a class modul "myclass" and an instance "myobj".
Set myobj = New myclass
Let assume too that this class has 2 properties: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either

to
"myfld1" or to "myfld2".
Is it possible to get (resp. set) the value of the parametrized

expression
myobj.{stfld} (syntax ??), that would be equal to :
- myobj.myfld1 if stfld="myfld1"
- myobj.myfld2 if stfld="myfld2"
Does it exist a way (either direct or indirect) to do it ?
Thanks for your help.


"NickHK" a écrit dans le message de news:
...
Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events

etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld" objects

and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal

either
to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax similar
to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does

it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17








  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Class modules: parametrize class object fields

Doesn't the excellent suggestion posted by Robert Bruce do what you want.
Adapting his example the syntax might be something like

= myobj.Properties(stfld)

Regards,
Peter T


"Jean-Pierre Bidon" wrote in message
...
Thank you for your answer.
Sorry, I was not clear. Your answer was not what I looked for.
I'll try to formulate it again with hopefully the right terminology.
I defined a class modul "myclass" and an instance "myobj".
Set myobj = New myclass
Let assume too that this class has 2 properties: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either

to
"myfld1" or to "myfld2".
Is it possible to get (resp. set) the value of the parametrized

expression
myobj.{stfld} (syntax ??), that would be equal to :
- myobj.myfld1 if stfld="myfld1"
- myobj.myfld2 if stfld="myfld2"
Does it exist a way (either direct or indirect) to do it ?
Thanks for your help.


"NickHK" a écrit dans le message de news:
...
Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events

etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld" objects

and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal

either
to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax similar
to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does

it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17








  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Class modules: parametrize class object fields

Yes, it looks like the solution of my problem. I'll try it. Thanks.

"Peter T" <peter_t@discussions a écrit dans le message de news:
...
Doesn't the excellent suggestion posted by Robert Bruce do what you want.
Adapting his example the syntax might be something like

= myobj.Properties(stfld)

Regards,
Peter T


"Jean-Pierre Bidon" wrote in message
...
Thank you for your answer.
Sorry, I was not clear. Your answer was not what I looked for.
I'll try to formulate it again with hopefully the right terminology.
I defined a class modul "myclass" and an instance "myobj".
Set myobj = New myclass
Let assume too that this class has 2 properties: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either

to
"myfld1" or to "myfld2".
Is it possible to get (resp. set) the value of the parametrized

expression
myobj.{stfld} (syntax ??), that would be equal to :
- myobj.myfld1 if stfld="myfld1"
- myobj.myfld2 if stfld="myfld2"
Does it exist a way (either direct or indirect) to do it ?
Thanks for your help.


"NickHK" a écrit dans le message de news:
...
Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events

etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld" objects

and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal

either
to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax
similar
to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does

it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17










  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Class modules: parametrize class object fields

The synatx "= myobj.Properties(stfld)" looks promising, unfortunately it is
not accepted by Excel.

"Peter T" <peter_t@discussions a écrit dans le message de news:
...
Doesn't the excellent suggestion posted by Robert Bruce do what you want.
Adapting his example the syntax might be something like

= myobj.Properties(stfld)

Regards,
Peter T


"Jean-Pierre Bidon" wrote in message
...
Thank you for your answer.
Sorry, I was not clear. Your answer was not what I looked for.
I'll try to formulate it again with hopefully the right terminology.
I defined a class modul "myclass" and an instance "myobj".
Set myobj = New myclass
Let assume too that this class has 2 properties: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal either

to
"myfld1" or to "myfld2".
Is it possible to get (resp. set) the value of the parametrized

expression
myobj.{stfld} (syntax ??), that would be equal to :
- myobj.myfld1 if stfld="myfld1"
- myobj.myfld2 if stfld="myfld2"
Does it exist a way (either direct or indirect) to do it ?
Thanks for your help.


"NickHK" a écrit dans le message de news:
...
Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events

etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld" objects

and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal

either
to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax
similar
to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is : does

it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17












  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Class modules: parametrize class object fields

I think he means without the = Jean-Pierre.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Jean-Pierre Bidon" wrote in message
...
The synatx "= myobj.Properties(stfld)" looks promising, unfortunately it

is
not accepted by Excel.

"Peter T" <peter_t@discussions a écrit dans le message de news:
...
Doesn't the excellent suggestion posted by Robert Bruce do what you

want.
Adapting his example the syntax might be something like

= myobj.Properties(stfld)

Regards,
Peter T


"Jean-Pierre Bidon" wrote in message
...
Thank you for your answer.
Sorry, I was not clear. Your answer was not what I looked for.
I'll try to formulate it again with hopefully the right terminology.
I defined a class modul "myclass" and an instance "myobj".
Set myobj = New myclass
Let assume too that this class has 2 properties: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal

either
to
"myfld1" or to "myfld2".
Is it possible to get (resp. set) the value of the parametrized

expression
myobj.{stfld} (syntax ??), that would be equal to :
- myobj.myfld1 if stfld="myfld1"
- myobj.myfld2 if stfld="myfld2"
Does it exist a way (either direct or indirect) to do it ?
Thanks for your help.


"NickHK" a écrit dans le message de news:
...
Jean-Pierre,
Your terminology is a little confusing.
In a class, you would normally talk about properties, methods, events

etc.
So following the description below, something like

private m_myfld1 as string
private m_myfld2 as string
Public property Let myfld1 (vData as string)
m_myfld1=vdata
end property

Public property Get myfld1 as string
myfld1=m_myfld1
end property

If myobj.myfld1=stfld Then
...
ElseIf myobj.myfld2=stfld Then
...

Is that what you mean ?

Otherwise maybe you need to look in using a Collection "myfld"

objects
and
"stfld" is the key (if a string) or the index (if numeric).

NickHK

"Jean-Pierre Bidon" wrote in message
...
Hi,
My question concerns class object fields.
To be more specific, let assume I defined an object "myclass" and an
instance "myobj".
Set myobj = New myclass
Let assume too that this object has 2 fields: "myfld1" and "myfld2".
In other respects, I have a string variable {stfld} that is equal

either
to
"myfld1" or to "myfld2".
Is it possible to get the value of myobj(stfld), with a syntax
similar
to
DAO with Access ?
I know that the previous syntax doesn't work, so my question is :

does
it
exist an indirect (roundabout) way to do it ?
Thanks for your help.

--
Jean-Pierre Bidon
Interstat
5 place de la République
75003 Paris
Tél: 01 45 49 19 17












Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class Modules MattShoreson[_70_] Excel Programming 5 March 22nd 06 10:34 AM
Basic question - modules and class modules - what's the difference? Mark Stephens[_3_] Excel Programming 9 May 8th 05 11:48 AM
Class Modules mark Excel Programming 7 April 14th 04 10:10 PM
Class modules pk Excel Programming 2 October 3rd 03 03:45 AM
Class Modules Siphuncle Excel Programming 2 August 12th 03 06:37 PM


All times are GMT +1. The time now is 10:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"