Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection problem

Status
Not open for further replies.

redshadow

Programmer
May 24, 2001
70
PH
Hi,

I am trying to create a hierarchical structure for my collection class but I got an error on the third one.

Here's my structure

class1
class1collection

class2
class2collection

class3
class3collection

class2 collection and class3 collection contains almost the same code :

for class2:
Public Function Add(firstclass as class1, Caption as string) as class1
Dim cls as class2
set cls = new class2
with cls
set .child = firstclass
.Caption = Caption
end with
mcolClass2.add cls

set Add = cls

set cls = nothing
end function

class 3 collection has the same code except for the variables.

When I reference it as

dim cl1 as class1collection
dim cl2 as class2collection
dim cl3 as class2collection

set cl1 = new class1collection
set cl2 = new class2collection
set cl3 = class2collection

cl1.add "class1"
cl2.add cl1,"First"
cl3.add cl2,"Top Level"

I receive no error during the insertion process however, if I will access it as:

cl2.item(1).cl1.item(1).Caption - it is OK
cl3.item(1).Caption - it is OK
cl3.item(1).cl2.item(1).Caption - I get the error here, I wonder why.

Can anybody give me a hint as to why this happen?

Thanks in advance, this is urgent please assist me.


 
I've only had 1/2 cup of coffee, but the first question that comes to mind is did you inherit throught your classes? The only way you can directly reference a class through another class is if you inherit the "sub" class into the "parent" class.
 
macleod1021

I think that "inherit" is VB.Net specific. In VB6 you can use "Implements" but it doesn't have quite the same implication as "Inherit" in a fully OO system.

In the sample code, I'm wondering what happened to class3collection. You have
Code:
dim cl3 as class[b][COLOR=red]2[/color][/b]collection
  :
set cl3 = class[b][COLOR=red]2[/color][/b]collection
Is that what you really intend?


[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Oh sorry for that, it's my mistake, it should be
dim cl3 as class3collection
set cl3 = new class3collection

I have not checked the use of Implements for the third class, I will give it a try.

Just another question, why did the second class managed to get the value from the first class without using Implements?
 
No, don't use implements for the third class. When you use the Implements keyword, it simply means that you are going to provide code for an existing set of methods and properties defined elsewhere. This is not what you are doing.

To expand a bit on Golom's explanation: VB6 allows "interface inheritance" but not "implementation inheritance". The interface is the methods and properties but not the code in them (the "implementation").

Implementing interfaces is part of the larger process of polymorphism, which has to do with the idea of calling different code for different classes that are in analogous situations with each other. For example, we might say that all animals Move. You can define an Animal interface to include a Move method (in VB you would just create a class called Animal and add a Public Move method with no code in it), and then have, say, Bumblebee and Buffalo classes implement that interface, each coding the process of Moving in a different way.

What we're saying is that if you want to be an Animal, you have to provide a Move method, but how you implement that Move method is up to you. (In other words, you don't inherit the Move method itself from the Animal class, you just "inherit" the fact that there is a Move method.) That's what Implementing is about.

Now, the practical value of this is that some other process can receive an instance of an Animal, and tell it to move from one place to another, without needing to know what kind of animal it is or how it accomplishes the move. That's polymorphism.

Hopefully, this explanation will also answer your other question.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top