As I learn class modules, one issue that seems to come up repeatedly involves class modules inside class modules - how the "child" class module can never see anything inside the "parent".

Say, for example, I'm writing a program involving a card game, and so I have a class module for each card, then a parent class module for the entire deck of cards. So, it makes sense that in the individual card modules I have procedures allowing me to specify the card type, such as Queen of Hearts. However, I also want to have error checking so that if the program accidentally specifies two different cards the Queen of Hearts, an error pops up. However, as the individual card modules can't "see" the deck-of-cards module or the other card modules, there is no way implement this error check in the Property Let CardName routine. You would have to make a routine inside the deck-of-cards module if you wanted to check this way, which seems messy to me and a programmer could still bypass it by specifiying the card type directly in the card class module.

Sorry if that wasn't clear enough, but what is the best way to handle situations such as this?

9 Years
Discussion Span
Last Post by Tekito


Though I have not understand you complete requirement, but will try to explain some work-around..
In such cases, what you need to do is Create a General Boolean array of say(54)
and logically assin numbers your self..
Say ClubA=1... ClubKing=13, HeartA=14.. HeartKing=26.. and so on..
Initailly make Whole array =false (by loop thru)
And as soon as a card is declared, Loop thru the array and check if the corresonding value is true.
if True, then that card is already taken, Give some message and come out..
else make it true and save the array value..
All this checking has to be done in Parent Class..



That would work, but here is where I get some issues. Number 1, as you say, this checking routine is in the Parent Class. But, when you give a card a name, since that is a specific property of a card, often isn't that done inside the child card class? (To be specific, if ClsDeckofCards is the parent class, and ClsCard is the child class, then it is in ClsCard that there is some "Property Let Name" procedure where you classify the card.)

So, continuing, if I wanted to error check *every* time I name a card, I would need the "Property Let Name" procedure to call the error check routine. Which would mean the child class is calling a procedure of the parent class? It seems that this violates the independence of the class modules, that it is using outside procedures inside its property procedures. Does this make sense?


I am not clear understanding your points. but from your discussion i guess something that Your parent class

(ClsDeckOfCards) may be collection class. Because Collection is a generic dynamically growing class. It uses "Key" string to represent an object in a collection.

So Key should be unique. You can create a class Card as Child Class. Collection class has Add method to add an element. Here you may Give the Card name as Key. So it will raise the exception if Card is already exist. Here you can check error at Add function.

Any No Card object with Same Name can be created but It cannot be added to the Parent Class. Only one can be added.

i Hope it is useful to you


That might solve that particular problem, but I was only using the deck of cards situation as an example of a general problem, not just unique names/keys. It is hard to think of commonplace examples, but anytime you change a property in a certain class, if it happens to conflict with another property in another class, how can it error check automatically? If you have ClsA and ClsB inside of ClsC, but if setting a property of ClsA that is not compatible with another property in ClsB, then there is a problem.

However, I think perhaps the solution is creating my own events within the class module. I haven't done this before, but then maybe everytime certain properties are changed, it raises an event, and then the parent class can verify there is no problem.


And now I've learned that Visual Basic cannot do WithEvents for arrays of objects. Sometimes I really dislike this language.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.