ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Class within a class (https://www.excelbanter.com/excel-programming/351589-class-within-class.html)

Josh Rolfe

Class within a class
 
I have defined a custom class called deductionFile. It has several
properties like fileName,fileDate,etc. It refers to a fixed-width text file
that I am importing into excel. The file is comprised of several fields,
and each field has information about it like width, typeOfData, Description,
etc. What I want to do is have a class called colDefinition that is a
member of the deductionFile class. Therefore, each deductionFile object
would have multiple colDefinition objects, each with their own properties.
I would then refernce it something like this:

deductionFile.colDefinition(i).width

Basically I want to have the same type of functionality as when you write
worksheets(1).range("foo").cells(1,1). Worksheets has a collection of
ranges, which has a collection of cells. I want to have a deductionFile
object that has a collection of colDefinitions.



Leith Ross[_491_]

Class within a class
 

Hello Josh,

The class functionality you are describing exists in code written in
"C". In my experience, VBA has never had more than 1 level of class
exposure (MyClass.Myproperty). It may be possible to do, but I have
never seen or read about it being done. If it can, I would love to know
how.

Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=505533


tony h[_38_]

Class within a class
 

try collections.
You can have collections of objects and of collections.

regard

--
tony
-----------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...fo&userid=2107
View this thread: http://www.excelforum.com/showthread.php?threadid=50553


Josh Rolfe

Class within a class
 
This is fine if all you are using the class for is storing and retrienving
variables, but you aren't able to define your own methods.
i.e. myClass.mySubClass.getInfo()

"tony h" wrote in
message ...

try collections.
You can have collections of objects and of collections.

regards


--
tony h
------------------------------------------------------------------------
tony h's Profile:
http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=505533




Tim Williams

Class within a class
 
Create a "column" class and then create instances of it in your main class.

Not really that different from creating any other object within a class.

Tim.

--
Tim Williams
Palo Alto, CA


"Josh Rolfe" wrote in message
...
This is fine if all you are using the class for is storing and retrienving
variables, but you aren't able to define your own methods.
i.e. myClass.mySubClass.getInfo()

"tony h" wrote in
message ...

try collections.
You can have collections of objects and of collections.

regards


--
tony h
------------------------------------------------------------------------
tony h's Profile:
http://www.excelforum.com/member.php...o&userid=21074
View this thread:

http://www.excelforum.com/showthread...hreadid=505533






Tushar Mehta

Class within a class
 
While you cannot have a class nested in another class, there is no
restriction on the use of 'peer' classes.

Create a class module named colDefinition. Create another class named
deductionFile. Now, in deductionFile you can declare a single variable of
type colDefinition or a static array or a dynamic array or insert objects of
type colDefinition in a collection or...

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I have defined a custom class called deductionFile. It has several
properties like fileName,fileDate,etc. It refers to a fixed-width text file
that I am importing into excel. The file is comprised of several fields,
and each field has information about it like width, typeOfData, Description,
etc. What I want to do is have a class called colDefinition that is a
member of the deductionFile class. Therefore, each deductionFile object
would have multiple colDefinition objects, each with their own properties.
I would then refernce it something like this:

deductionFile.colDefinition(i).width

Basically I want to have the same type of functionality as when you write
worksheets(1).range("foo").cells(1,1). Worksheets has a collection of
ranges, which has a collection of cells. I want to have a deductionFile
object that has a collection of colDefinitions.




Edward Ulle

Class within a class
 
Josh,

Out of curiosity I built a small test case of a class within a class,
Class1 and Class2. I'm sure there are some limitation but the behaviour
in VBA is very much like the Excel Object Model. In particular in the
Test module the Add method simply returns a new instance of Class2 and
assigns the name. With the Collection in Class1 you could also assign
key names to select the particular instance of Class2.

Option Explicit

Sub Test()

Dim oClass1 As Class1

Set oClass1 = New Class1

oClass1.Add.SetName = "first"
oClass1.Add.SetName = "second"
oClass1.Add.SetName = "third"

MsgBox oClass1.GetClass2(2).GetName

End Sub
Option Explicit
' Class1
Private cClass2 As Collection

Private Sub Class_Initialize()
Set cClass2 = New Collection
End Sub

Private Sub Class_Terminate()
Dim oClass2 As Class2
For Each oClass2 In cClass2
Set oClass2 = Nothing
Next
Set cClass2 = Nothing
End Sub

Public Function Add() As Class2
Dim oTemp As Class2
Set oTemp = New Class2
cClass2.Add oTemp
Set Add = oTemp
End Function

Public Property Get GetClass2(i As Long) As Class2
If i < 1 Or i cClass2.Count Then
Set GetClass2 = Nothing
Else
Set GetClass2 = cClass2(i)
End If
End Property

Option Explicit
' Class2
Private strName As String

Public Property Let SetName(strText As String)
strName = strText
End Property

Public Property Get GetName() As String
GetName = strName
End Property




*** Sent via Developersdex http://www.developersdex.com ***

Edward Ulle

Class within a class
 
Josh,

I realized my Class1 Class_Terminate was wrong. In fact it was doing
nothing. It should be.

Private Sub Class_Terminate()
Dim i As Long
For i = 1 To cClass2.Count
cClass2(i) = Nothing
Next
Set cClass2 = Nothing
End Sub




*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 06:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com