|
-
Jun 19th, 2009, 09:55 AM
#1
[RESOLVED] Classes hierarchy
I want to create a class to perform some operations with polygons. A polygon consists of one or more closed contours and each of these can have 3 or more vertices, each with 2 coordinates, X and Y.
I have been working for a while with 1-contour polygons and I have a polygon class plus a vertex class (from a demo I found in this forum). The latter was used inside the polygon class as a collection. But the problem is how to go about it when there are more contours. Should I make an intermediate 'contour' class? Can someone help me to figure out the basic layout? I have used classes very little and almost always using pre-defined templates from other people, so I'm a little confused. Doing it with UDTs was easier.
Last edited by krtxmrtz; Jun 19th, 2009 at 09:59 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 19th, 2009, 10:36 AM
#2
Re: Classes hierarchy
You could use 3 classes: Polygon.Contour.Vertex
The polygon class would have a collection/array of Contour classes per polygon
The Contour class would have a collection/array of Vertex classes per contour
To get the vertex from the polygon class, the Polygon class would have a property that allows you to dictate which Contour to select (1-n for example), and probably very similar to how you currently access a specific vertex from the polygon class now. Then you'd query the Contour class for the specific vertex. It might look something like this:
Code:
' get Vertex 3 of contour 2 from polygon 5
myPolygons(5).Contour(2).Vertex(3).X
-
Jun 19th, 2009, 11:44 AM
#3
Re: Classes hierarchy
 Originally Posted by LaVolpe
You could use 3 classes: Polygon.Contour.Vertex
The polygon class would have a collection/array of Contour classes per polygon
The Contour class would have a collection/array of Vertex classes per contour
To get the vertex from the polygon class, the Polygon class would have a property that allows you to dictate which Contour to select (1-n for example), and probably very similar to how you currently access a specific vertex from the polygon class now. Then you'd query the Contour class for the specific vertex. It might look something like this:
Code:
' get Vertex 3 of contour 2 from polygon 5
myPolygons(5).Contour(2).Vertex(3).X
I had the feeling that this was the correct scheme but now I'll have to rack my brain quite a bit to figure it out. I'll be back if I run into trouble.
Thanks for now.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 22nd, 2009, 09:03 AM
#4
Re: Classes hierarchy
Browsing the forum in order to get more documented I ran into this,
Code:
Public Function NewEnum() As IUnknown
Set NewEnum = mcolEmployees.[_NewEnum]
End Function
which apparently is used to enable the For...Each loops. But the explanation wasn't all that clear to me. Why wouldn't the loop work in the first place?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 22nd, 2009, 11:45 AM
#5
Re: Classes hierarchy
Well the mcolEmployees has For:Each capabilities, but since your collection is in a class, and the class is a wrapper for the collection, you have no way of exposing the collection's For:Each ability, other than exposing the collection itself which you wouldn't normally want to do. You don't want the user directly manipulating your class' collection. If you did, it would work like this
Code:
Pubic Property Get TheCollection() As Collection
Set TheCollection = mcolEmployees
End Property
' then the user can do
Dim V As Variant
For Each V In Class1.TheCollection
....
Next
Edited: By using For:Each, yes the user is directly manipulating your class' collection. However, by exposing the class thru a property, the user could do horrible things like: Set Class1.Collection = Nothing, or setting it to another collection entirely.
Last edited by LaVolpe; Jun 22nd, 2009 at 12:41 PM.
-
Jun 22nd, 2009, 01:03 PM
#6
Re: Classes hierarchy
 Originally Posted by LaVolpe
...
It's a bit clearer now, thank you.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 22nd, 2009, 02:18 PM
#7
Re: Classes hierarchy
 Originally Posted by krtxmrtz
It's a bit clearer now, thank you.
You're welcome. The For:Each will work within the class proper, but you are adding the NewEnum property to allow outside objects to enumerate the collection.
-
Jun 23rd, 2009, 08:04 AM
#8
Re: Classes hierarchy
Well, after some trial & error I finally have got it working, but I feel like I'm walking on shaky ground and there's a number of things that have me a bit confused.
Let me start with this. In a class template I've borrowed somewhere, there's an Add method to add new items to a collection:
VB Code:
'This goes inside the 'Contour class
Private mcolItems As Collection
'
'
Public Function Add(ByRef X As Single, ByRef Y As Single) As Vertex
Dim objVertex As Vertex
Set objVertex = New Vertex
objVertex.X = CSng(X)
objVertex.Y = CSng(Y)
mcolItems.Add objVertex
Set Add = objVertex
Set objVertex = Nothing
End Sub
As you see it is declared as a function and returns an instance of the newly added vertex class. The question is why would I need a returned value (as a matter of fact, in the example I've been working on I have changed it to a Sub and it works fine).
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 23rd, 2009, 08:31 AM
#9
Re: Classes hierarchy
Returning a reference is not necessary but it might well be useful in some cases. It provides a means of identifying and manipulating the newly created vertex object.
-
Jun 23rd, 2009, 09:13 AM
#10
Re: Classes hierarchy
OK, then here's my second issue.
This prints a list of the current vertices for the different contours:
VB Code:
Dim i As Long, j As Long
Dim X As Single, Y As Single
Dim objVertex As Vertex
Dim objContour As Contour
'...
'...
For i = 1 To objPolygon.Count
For j = 1 To objPolygon.Item(i).Count
Debug.Print CStr(objPolygon.Item(i).Item(j).X) & "," & CStr(objPolygon.Item(i).Item(j).Y)
Next
Next
...but why isn't this working (error 438: 'object doesn't support this property or method)?:
VB Code:
For Each objContour In objPolygon
For Each objVertex In objContour
Debug.Print CStr(objVertex.X) & "," & CStr(objVertex.Y)
Next objVertex
Next objContour
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 23rd, 2009, 09:33 AM
#11
Re: Classes hierarchy
In order to support For Each capabilities, the Procedure Id of the NewEnum function must be set to -4.
The Procedure Id can be set on the Procedure Attributes screen. Access this screen from the Tools menu or Object Browser (right click on the NewEnum procedure and select properties).
-
Jun 23rd, 2009, 09:40 AM
#12
Re: Classes hierarchy
 Originally Posted by brucevde
In order to support For Each capabilities, the Procedure Id of the NewEnum function must be set to -4.
The Procedure Id can be set on the Procedure Attributes screen. Access this screen from the Tools menu or Object Browser (right click on the NewEnum procedure and select properties).
In addition to what Bruce mentioned, you will want to check the box in that screen to hide the member. This way your NewEnum property is not displayed with VB's Intellisense.
-
Jun 23rd, 2009, 09:41 AM
#13
Re: Classes hierarchy
 Originally Posted by brucevde
In order to support For Each capabilities, the Procedure Id of the NewEnum function must be set to -4.
The Procedure Id can be set on the Procedure Attributes screen. Access this screen from the Tools menu or Object Browser (right click on the NewEnum procedure and select properties).
Thanks a lot, I had read about this but had forgotten.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jun 23rd, 2009, 09:44 AM
#14
Re: Classes hierarchy
I think it might need to be more like this...
Code:
For Each objContour In objPolygon.Contours
For Each objVertex In objContour.Vertex
Debug.Print CStr(objVertex.X) & "," & CStr(objVertex.Y)
Next objVertex
Next objContour
..assuming that .Contours and .Vertex return the appropriate collections
Problem with that is that the Collections are exposed which you maybe don't want to happen, better perhaps to expose elements one at a time like your first code snip and like what LaVolpe shows in post #2
Edit: omg, I'm about 5 post behind already and barking up the wrong tree, very slow me
Last edited by Milk; Jun 23rd, 2009 at 09:48 AM.
-
Jun 23rd, 2009, 09:45 AM
#15
Re: Classes hierarchy
 Originally Posted by LaVolpe
In addition to what Bruce mentioned, you will want to check the box in that screen to hide the member. This way your NewEnum property is not displayed with VB's Intellisense.
Thanks!
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|