Multiciplicity

F

Fabian

Hi,

I've got two user-defined classes, Country and Product. Every product is
sold in every country, therefore the countries should contain a collection of
products (reference) and vice versa.

So I wrote:

*** class product

Public Countries As Collection

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

Public AddCountry(ByRef c As Country)
Me.Countries.Add c
c.Products.Add Me
End Property

*** class country

Public Products As Collection

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

But that doesn't seem to work at all. What am I doing wrong?

Regards, Fabian


----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/com...5b77857&dg=microsoft.public.excel.programming
 
B

Bob Bridges

To get intelligent replies from folks, you really need to say more than "it
didn't work", they need you to tell them what it DID do -- error message,
garbled response, whatever. But I can at least attempt a comment or two,
here:

In the below code you have an AddCountry method which adds the argument c to
the Countries collection. But instead of saying Countries.Add, it says
Me.Countries.Add; why? What is Me? I've seen it done, but I've never been
clear on what Me is even in Access, much less in Excel.

I think if you change that code to just "Countries.Add c" you'll have added
the country object or code or whatever it is to your Countries collection.
But then your code adds the statement "c.Products.Add Me", which seems to
assume that the Products collection is a property of the country (and I don't
know why it would be), and it's adding Me to the Products as though Me is a
product. Well, maybe it is, but I don't know where your code would be
getting it.

Let's review your requirements, because I'm not sure: You want a Countries
collection to contain one entry for every country, right? And a Products
collection to hold one of every product? Or do you need something like every
combination of country and product (because if you do, I would probably
create it a different way)?

Maybe we should start here: What do you want to DO with these things? How
do you want to use them?
 
F

Fabian

Hi Bob,

thank you for your answer. The error message is in German - it is the
runtime error 91. Object not defined or something like that. Not quite
telling much. What is weired to me though, is the fact that the same code
runs perfectly fine in VB!

The association between the classes should be "n to n", so that I can access
all associated products from within a country and all associated countries
from within a product. And yes each Product is sold in each Country.

Example from VB:

Dim p1, p2, pr As Product, c1, c2, co As Country
c1 = New Country
c2 = New Country

p1 = New Product
p2 = New Product

p1.addCountry(c1)
p1.addCountry(c2)
p2.addCountry(c1)
p2.addCountry(c2)

c1.CountryName = "c1"
p1.ProductName = "p1"
c2.CountryName = "c2"
p2.ProductName = "p2"

For Each co In p1.Countries
MsgBox(co.CountryName)
Next

For Each pr In c1.Products
MsgBox(pr.ProductName)
Next

Regards
 
B

Bob Bridges

I still see an asymmetry in the Country and Product classes that
I don't understand. Maybe I need to step back and ask about the
overall assumptions. If you really want every product in every
country then you don't need collections for each product and
country; just two simple collections will do. Like this:

' Assuming the products and countries are listed in a
' worksheet somewhere:
Dim Products as Collection, Countries as Collection
For Each co In ProductRange
Products.Add co.Value
Next co
For each co In CountryRange
Countries.Add co.Value
Next co

Now Products and Countries have everything in them you might
need to work with, and if all products are sold in all countries
then you don't need Product and Country classes at all. But if
you really want to track the countries for each product individually,
and/or the products for each country, it must be because you
envision the possibility in the future that some products will not
be sold in some countries. If so, your idea is sound but I would
probably implement it slightly differently.

Rather than just tell you how I would do it, I'll go the Socratic
route and make you tell me what's not working. I suspect that
in the process of describing in more detail what's not happening,
you'll see your own processes more clearly. You say what you're
doing, but you don't say what's wrong with it...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top