Is an Array a solution here ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've a class module that has a procedure like this:

Public Sub ResizeCtl(CtlToResize As String)

Now, this procedure accepts only one argument per instance. I want it to
accept one or more arguments, being flexible enough to accept any number of
arguments. I guess that I've to use an Array here, but that is an unexplored
territory for me. Can some one please help me with this ?

Thanks.
 
Sreedhar said:
I've a class module that has a procedure like this:

Public Sub ResizeCtl(CtlToResize As String)

Now, this procedure accepts only one argument per instance. I want it to
accept one or more arguments, being flexible enough to accept any number of
arguments. I guess that I've to use an Array here, but that is an unexplored
territory for me. Can some one please help me with this ?
You can use optional parameters, when the number of parameters is known.
Otherwise use a ParamArray.

Public Sub ResizeCtl(CtlToResize As String, _
Optional Height As Long = -1, _
Optional Widht As Long = -1)

or

Public Sub ResizeCtl(CtlToResize As String, _
ParamArray Params() As Variant)

Take a look at the OH for the correct use of ParamArray.

mfG
--> stefan <--
 
If you want this to be able to accept any number of controls, then
consider a custom collection class. You can add in individual control
objects, then pass the Controls collection to your subroutine.

Sample Collection Class Code (paste this into notepad, save it with
a .cls extension, import into Access):

Option Compare Database
Option Explicit

Private mCol As Collection
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Public Sub Add(ctl As Control)
mCol.Add ctl, ctl.Name
End Sub
Property Get Count()
Count = mCol.Count
End Property
Property Get Item(ByVal Index) As Control
Attribute Item.VB_UserMemId = 0
Set Item = Control.Item(Index)
End Property
Public Sub Delete(ByVal Index)
mCol.Remove Index
End Sub
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
'Enumeration function for the collection
Set NewEnum = mFlds.[_NewEnum]
End Function




Now, you can have:

Dim cls as YourClassName
dim col as new CollectionClassName
col.add control1
col.add control2
etc.

cls.ResizeAllControls(col)


Public Sub ResizeAllControls(col as Collection)
dim ctl as control
for each ctl in col
resizectl ctl
end sub


Public Sub ResizeCtl(ctlToResize as Control)






Chris Nebinger
 
Hi Chris,

Thanks for introducing me to a new animal - the "Collection Class". The idea
is bang on what I wanted and I will try this.

Thanks once again.
 
Sreedhar,

Let me recommend a book for you:

Access 2000 Developer's Handbook

I highly recommend (as probably do most other developers) this book.
It is a few editions old, but it still works. It talks about how to
use the collection class, as well as some of the items that you need
to be aware of. Mainly, it discusses the Attribute line. That will
not work if you paste it directly into a class in MS Access, but will
work if you paste it into a text file with a .cls extension, then use
the Import functionality in the IDE.

Property Get Item(ByVal Index) As Control
Attribute Item.VB_UserMemId = 0
Set Item = Control.Item(Index)
End Property

This attribute sets the default item.

Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
'Enumeration function for the collection
Set NewEnum = mFlds.[_NewEnum]
End Function

This enables For Each functionality. Let me know if you run into any
problems.


Chris Nebinger

Hi Chris,

Thanks for introducing me to a new animal - the "Collection Class". The idea
is bang on what I wanted and I will try this.

Thanks once again.
--
Sreedhar



If you want this to be able to accept any number of controls, then
consider a custom collection class. You can add in individual control
objects, then pass the Controls collection to your subroutine.
Sample Collection Class Code (paste this into notepad, save it with
a .cls extension, import into Access):
Option Compare Database
Option Explicit
Private mCol As Collection
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Public Sub Add(ctl As Control)
mCol.Add ctl, ctl.Name
End Sub
Property Get Count()
Count = mCol.Count
End Property
Property Get Item(ByVal Index) As Control
Attribute Item.VB_UserMemId = 0
Set Item = Control.Item(Index)
End Property
Public Sub Delete(ByVal Index)
mCol.Remove Index
End Sub
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
'Enumeration function for the collection
Set NewEnum = mFlds.[_NewEnum]
End Function
Now, you can have:
Dim cls as YourClassName
dim col as new CollectionClassName
col.add control1
col.add control2
etc.

Public Sub ResizeAllControls(col as Collection)
dim ctl as control
for each ctl in col
resizectl ctl
end sub
Public Sub ResizeCtl(ctlToResize as Control)
Chris Nebinger- Hide quoted text -

- Show quoted text -
 
Back
Top