CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2012
    Posts
    12

    Classes and Interfaces.... Code included, what is wrong...?

    Goal

    A) read the points (X1, Y1, X2, Y2 and so on) of a shapeline and store them
    B) divide the line(s) in parts of 10meter and being able to assign values to each part

    I have trouble to convert the old vb6 approach with TYPES and arrays and DIMs to visual studio. So far I did this.... for GOAL A.

    I have made a class, which you will find below, where I declared all variables used by using an interface..... the array containing the coordinates must be public... so it coordinates could be used in drawingroutines...

    Code:
    BaanShapePuntDecl
    Then I made a second class which handles reading the shapefile. And suddenly it gets confusing... I will implement only the relevant parts of the code below....

    Code:
    Imports DMCviewer.BaanShapePuntDecl
    
    Public class ReadBaanShapePoint
    	Private Sub LeesBAANASLijnen(ByVal FileNumber As Integer, ByRef Offset As Long, ByVal FileType As Integer)
            Dim X1 As Double, Y1 As Double, X2 As Double, Y2 As Double
    	Dim iBaanShapePunt() As BaanShapePuntDecl
    	Dim Vertex as long
    
    	for count = 200 to 275
          	      Vertex += 1
                  << HERE X1, Y1, X2, Y2 get a value>>
         	      iBaanShapePunt(vertex).X1lineShape = CSng(X1)
         	      iBaanShapePunt(vertex).X2lineShape = CSng(X2)
         	      iBaanShapePunt(vertex).Y1lineShape = CSng(Y1)
          	      iBaanShapePunt(vertex).Y2lineShape = CSng(Y2)
    	next
    End sub
    
    End class
    The question is, will this work, or are there any big mistakes as I assume/feel there are.....

    Below the declaration interface of the lineshape points

    Code:
    Public Class BaanShapePuntDecl
        REM ************************************************
        REM * Variable for the shape axis
        REM ************************************************
        Implements iBaanShapePunt
    
        Public Interface iBaanShapePunt
            Property X1lineShape As Single
            Property Y1lineShape As Single
            Property X2lineShape As Single
            Property Y2lineShape As Single
        End Interface
        Public arraylist As iBaanShapePunt
    
        Sub New(ByVal a, ByVal b, ByVal c, ByVal d)
            a = X1lineShape
            b = Y1lineShape
            c = X2lineShape
            d = Y2lineShape
        End Sub
    
        Public Property X1lineShape As Single Implements iBaanShapePunt.X1lineShape
            Get
                Return X1lineShape
            End Get
            Set(ByVal value As Single)
    
            End Set
        End Property
    
        Public Property X2lineShape As Single Implements iBaanShapePunt.X2lineShape
            Get
                Return X2lineShape
            End Get
            Set(ByVal value As Single)
    
            End Set
        End Property
    
        Public Property Y1lineShape As Single Implements iBaanShapePunt.Y1lineShape
            Get
                Return Y1lineShape
            End Get
            Set(ByVal value As Single)
    
            End Set
        End Property
    
        Public Property Y2lineShape As Single Implements iBaanShapePunt.Y2lineShape
            Get
                Return Y2lineShape
            End Get
            Set(ByVal value As Single)
    
            End Set
        End Property
    
    End Class

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Classes and Interfaces.... Code included, what is wrong...?

    1) Who set your GOAL? You or a teacher?
    2) Have you TRIED to execute the code?
    3) If the answer is NO to #2, then try that, POST the code with the ERROR LINE and MESSAGES.
    4) Put mouse over ERROR, click and press F1 for help. Attempt to resolve errors, YOURSELF (You'll feel better)
    5) If stuck, then ask a question.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2012
    Posts
    12

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Hi dglienna

    I am not a student, im an emplyee at www.movares.nl in the netherlands.. I am not a programmer but teached myself vb6 to programm fast tools to enlighten my work

    Since a few weeks i decided to step towards vb2010 and as i saw that i could programm in the same style as vb6 i decided - to try at least- to programm in OOP....

    I bought books and spended already many many hours to try to step away from my vb6 approach..... I made already a working part of teh programm i am working on but things getting complex fast due to the way u could use class.... it feels you have so many options that it starts puzzling me..... in this example i start using an interface....

    If noone can see if its well done how i classed things than i will make a mini programm to test this but i feel it would take me many many hours to find out whats wrong.... I still refuse to proramm it in a vb6 style...

    I also checked dozens of examples, tried approaches with dims, arraylists, structures and such.... I just want a push in the good direction )

  4. #4
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Hello,

    1. INTERFACE is not for storing data but for defining "contract" for your objects (MSDN)
    2. Use STRUCTURE if you want to store simple data
    3. ArrayList can be used without having to worry about dynamically increasing your array (REDIM)
    4. VB.NET comment is now single quote (')


    Hope that will start you off the right direction.
    Good Luck,
    -Cool Bizs

  5. #5
    Join Date
    Sep 2012
    Posts
    12

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Well you point out my struggle with vb2010..... I see so many examples and read so many things about it and it feels as if there are a 100 ways to build the bridge, some say use steel, some wood, some say use all these things... (

    in vb6 i uses TYPE....END TYPE and a DIM statement like
    TYPE elements
    wood as integer
    steels as integer
    END TYPE
    dim bridge(0 to 10) as element

    but right now after about 50 hours of OOP i am still confused ( and i refuse to misuse OOP and programm VB2010 as it were VB6....

    U feel along with this newby when u were once one? ;o)

  6. #6
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Good attitude.

    1. Replace TYPE w/ STRUCTURE or CLASS
    2. Replace REDIM PRESERVE myArray(n) w/ ArrayList (MSDN link) which exposes .Add() to add items into the list. The link to MSDN contains sample on how to use ArrayList.


    VB6:
    Code:
    Type Coordinates
      x As Double
      y As Double
    End Type
    VB.NET
    Code:
    Structure Coordinates
      x As Double
      y As Double
    End Structure
    VB6
    Code:
    Dim myArray(5) As Coordinates
    
    myArray(0) = new Coordinates
    myArray(0).x = 2.0
    myArray(0).y = 3.0
    VB.NET
    Code:
    Dim myArray As New ArrayList
    Dim coors As Coordinates
    
    coors = new Coordinates
    coors.x = 2.0
    coors.y = 3.0
    
    myArray.Add(coors)
    Good Luck,
    -Cool Bizs

  7. #7
    Join Date
    Sep 2012
    Posts
    12

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Thanx , believe me I started with structures but i was told to use properties and i read a book which advised to use an interface...... so in my first attempt (actually i have a nice working part of my programm) i stumble over storing and retrieving array-able data.....

  8. #8
    Join Date
    Sep 2012
    Posts
    12

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Hooray, I solved it this way...... it works fine but now i want to ask a few things about the theory of this....

    I used a Module to store the structure (like in VB6) is this a good way to go?

    Code:
    Module DMCviewerModule
    
        REM ************************************************
        REM * Variable for the shape axis
        REM ************************************************
        Public Structure BaanShapePunt
            Public X1lineShape As Single
            Public Y1lineShape As Single
            Public X2lineShape As Single
            Public Y2lineShape As Single
        End Structure
    
    End Module
    Then I use this on my mainform, is this correct technically, it felt for me )

    Code:
    Public Class Form1
        Private BaanPunten As New ArrayList
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim vertex As Long
            Call LeesBAANASLijnen()
            For vertex = 0 To 200
                ListBox1.Items.Add(BaanPunten(vertex).X1lineShape & "  " & BaanPunten(vertex).X2lineShape & "  " & BaanPunten(vertex).y1lineShape & "  " & BaanPunten(vertex).y2lineShape)
            Next
        End Sub
    
        Private Sub LeesBAANASLijnen()
            Dim x1, x2, y1, y2 As Long
            Dim counter As Integer
            Dim BaanPunt As BaanShapePunt
            For counter = 200 To 400
                x1 = counter
                x2 = counter * 2
                y1 = counter
                y2 = counter / 2
                BaanPunt.X1lineShape = CSng(x1)
                BaanPunt.X2lineShape = CSng(x2)
                BaanPunt.Y1lineShape = CSng(y1)
                BaanPunt.Y2lineShape = CSng(y2)
                BaanPunten.Add(baanpunt)
            Next
    
        End Sub

  9. #9
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Hello,

    1. STRUCTURE does not have to appear in a container like MODULE. It works almost the same like CLASS.
    2. CALL is no longer required.


    But you should be good to go with the current code to start off
    Good Luck,
    -Cool Bizs

  10. #10
    Join Date
    Sep 2012
    Posts
    12

    Class, property and code included, what is wrong? one step away from a working prog

    Hello I have still problems in understanding combinations of a class and a list defined in it

    Below you see 3 parts of my programm, with only relevant code
    1 First a class with properties, see first part... this works
    2 Than a subprogramm where some values are assigned to a list, second part... this full part works
    3 Than a subprogramm where i need values out the list....full code below in the last part, here it goes wrong

    It fails on this 3rd point as it does not know Bakjeslijst at that point but what i try (using dim and such), i can not get it working.... in some solution its error free but it can not retrieve a list.count > 0...

    Any of you know what is going wrong... it is a nooby mistake, that i am sure of )

    Thx Joska

    Code:
    Public Class Class_BaanAsBakje
        REM ************************************************
        REM * Fields voor de bakjes van de baan
        REM ***********************************************
        Private mIDbakje As Long
        Private mX1bakje As Single
    
        Private mY1bakje As Single
        Private mX2bakje As Single
        Private mY2bakje As Single
        Private mLengte As Single
        Private mReductiepuntenLinks As Long
        Private mReductiepuntenRechts As Long
        REM ************************************************
        REM * Properties voor de bakjes van de baan
        REM ************************************************
        Public Property IDbakje As Long
            Get
                Return mIDbakje
            End Get
            Set(ByVal value As Long)
                mIDbakje = value
            End Set
        End Property
        Public Property X1bakje As Single
            Get
                Return mX1bakje
            End Get
            Set(ByVal value As Single)
                mX1bakje = value
            End Set
        End Property
    
        Public Property Y1bakje As Single
            Get
                Return mY1bakje
            End Get
            Set(ByVal value As Single)
                mY1bakje = value
            End Set
        End Property
        Public Property X2bakje As Single
            Get
                Return mX2bakje
            End Get
            Set(ByVal value As Single)
                mX2bakje = value
            End Set
        End Property
    
        Public Property Y2bakje As Single
            Get
                Return mY2bakje
            End Get
            Set(ByVal value As Single)
                mY2bakje = value
            End Set
        End Property
        Public Property Lengte As Single
            Get
                Return mLengte
            End Get
            Set(ByVal value As Single)
                mLengte = value
            End Set
        End Property
     
        Public Property ReductiepuntenLinks As Long
            Get
                Return mReductiepuntenLinks
            End Get
            Set(ByVal value As Long)
                mReductiepuntenLinks = value
            End Set
        End Property
        Public Property ReductiepuntenRechts As Long
            Get
                Return mReductiepuntenRechts
            End Get
            Set(ByVal value As Long)
                mReductiepuntenRechts = value
            End Set
        End Property
    
       REM ************************************************
        REM * VULLEN VANUIT DE OPKNIPPER
        REM ************************************************
        Public Sub New(ByVal IDbakje As Long, ByVal X1bakje As Single, ByVal Y1bakje As Single, ByVal X2bakje As Single, ByVal Y2bakje As Single, ByVal lengte As Single)
            mIDbakje = IDbakje
            mX1bakje = X1bakje
            mY1bakje = Y1bakje
            mX2bakje = X2bakje
            mY2bakje = Y2bakje
            mLengte = lengte
    
        End Sub
    
    End class

    Code:
         Dim BakjesLijst As New List(Of Class_BaanAsBakje)
            Dim AantalBakjes As Integer
            Try
    
    
    IDbakje = 8
    BakjeX1 = 4
    BakjeY1 = 3
    BakjeX2 = 2
    BakjeY2 = 1
    LengteBakje = 9
    Dim bakje As Class_BaanAsBakje = New Class_BaanAsBakje(IDbakje, BakjeX1, BakjeY1, BakjeX2, BakjeY2, LengteBakje)
                        BakjesLijst.Add(bakje)
    Code:
    Public Sub Maak_BakjesShape(ByVal sFilename)
            REM ***************************************************************
            REM * Maak van de as een shapefile en voeg hem toe aan de layers
            REM ***************************************************************
           
            Try
                Dim count As Long
                Dim filename As String = Form1.txtPadFileNaam.Text & sFilename & "_DMC_bakjes.shp"
                Dim f As New Feature()
                Dim fs As New FeatureSet(f.FeatureType)
                fs.Projection = Form1.myVenster.Projection
                Dim coord As Coordinate() = New Coordinate(BakjesLijst.Count - 1) {}
                For count = 0 To Bakjeslijst.Count - 1
                    coord(count) = New Coordinate(BakjesLijst(count).X1bakje, BakjesLijst(count).Y1bakje)
                Next
                Dim ls As New LineString(coord)
                f = New Feature(ls)
                fs.Features.Add(f)
                fs.SaveAs(filename, True)
                Form1.myVenster.AddLayer(filename)
                ' *************************************************************************
                ' *  VOEG DATA TOE AAN STATUSBAR en LOGBOEK
                ' *************************************************************************
                Dim statusTEKST As String
                statusTEKST = "Bakjes opgeslagen en als laag toegevoegd met de aanvulling _DMCbakjes"
                frmDMClogboek.LogboekListBox.Items.Add(statusTEKST)
                Form1.ToolStripStatusLabel.Text = statusTEKST
                frmDMClogboek.LogboekListBox.Items.Add("Bakjes opgeslagen en als laag toegevoegd met de aanvulling _DMCbakjes")
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Critical, "Fout in Maak_BakjesShape")
            End Try
        End Sub
    Last edited by Joska Paszli; September 13th, 2012 at 05:30 PM.

  11. #11
    Join Date
    Sep 2012
    Posts
    12

    Re: Class, property and code included, what is wrong? one step away from a working p

    hmmm wrong place i will remake a post with smaller dummy code, sorry

  12. #12
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Class, property and code included, what is wrong? one step away from a working p

    I think you're saying this code is not working:

    Code:
                Dim coord As Coordinate() = New Coordinate(BakjesLijst.Count - 1) {}
                For count = 0 To Bakjeslijst.Count - 1
                    coord(count) = New Coordinate(BakjesLijst(count).X1bakje, BakjesLijst(count).Y1bakje)
                Next
    And the faulting code is:

    Code:
    Dim coord As Coordinate() = New Coordinate(BakjesLijst.Count - 1) {}
    Which I assume you're trying to create an array of "Coordinate" type. If so, the correct way is:

    Code:
    Dim coord As Coordinate() = DirectCast(Array.CreateInstance(GetType(Coordinate), BakjesLijst.Count), Coordinate())
    This will dynamically create an array of Coordinate with the specified count. Note that you should not do "BakjesLijst.Count - 1" in the code since Array.CreateInstance() will create the correct amount elements.
    Good Luck,
    -Cool Bizs

  13. #13
    Join Date
    Sep 2012
    Posts
    12

    Re: Classes and Interfaces.... Code included, what is wrong...?

    mmm thx but it still isnt working, plz better reply

    http://forums.codeguru.com/showthrea...perty-and-list

    as it makes the example easier to understand.....

    greetings!

  14. #14
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Classes and Interfaces.... Code included, what is wrong...?

    Start with samples from MSDN Pick VB or C# and STEP THRU CODE TO UNDERSTAND the HOW IT WORKS part....
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured