Results 1 to 15 of 15

Thread: [RESOLVED] Classes hierarchy

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [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)

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Classes hierarchy

    Quote Originally Posted by LaVolpe View Post
    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)

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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)

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Classes hierarchy

    Quote Originally Posted by LaVolpe View Post
    ...
    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)

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Classes hierarchy

    Quote Originally Posted by krtxmrtz View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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:
    1. 'This goes inside the 'Contour class
    2. Private mcolItems As Collection
    3. '
    4. '
    5. Public Function Add(ByRef X As Single, ByRef Y As Single) As Vertex
    6.     Dim objVertex As Vertex
    7.    
    8.     Set objVertex = New Vertex
    9.     objVertex.X = CSng(X)
    10.     objVertex.Y = CSng(Y)
    11.     mcolItems.Add objVertex
    12.     Set Add = objVertex
    13.     Set objVertex = Nothing
    14. 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)

  9. #9
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  10. #10

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Classes hierarchy

    OK, then here's my second issue.

    This prints a list of the current vertices for the different contours:

    VB Code:
    1. Dim i As Long, j As Long
    2. Dim X As Single, Y As Single
    3. Dim objVertex As Vertex
    4. Dim objContour As Contour
    5. '...
    6. '...
    7. For i = 1 To objPolygon.Count
    8.     For j = 1 To objPolygon.Item(i).Count
    9.         Debug.Print CStr(objPolygon.Item(i).Item(j).X) & "," & CStr(objPolygon.Item(i).Item(j).Y)
    10.     Next
    11. Next
    ...but why isn't this working (error 438: 'object doesn't support this property or method)?:
    VB Code:
    1. For Each objContour In objPolygon
    2.     For Each objVertex In objContour
    3.        Debug.Print CStr(objVertex.X) & "," & CStr(objVertex.Y)
    4.     Next objVertex
    5. 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)

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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).

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Classes hierarchy

    Quote Originally Posted by brucevde View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Classes hierarchy

    Quote Originally Posted by brucevde View Post
    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)

  14. #14
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  15. #15

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Classes hierarchy

    Quote Originally Posted by LaVolpe View Post
    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
  •  



Click Here to Expand Forum to Full Width