How to use classes, they still puzzle me a bit

Joska Paszli

New member
Joined
Sep 7, 2012
Messages
2
Programming Experience
3-5
helllo dear readers
I have programming experiences in VB6 and now making my first steps into Visual Studio 2010, to rewrite old vb6 code to vs2010
I have a question about modules vs classes, a Fine explanation of class and module
But still I am puzzled and was thinking that maybe giving an example would be best....

STEP 1: read a (poly)line from a file, which has coordinates
STEP 2: divide this line in segments of 10 meter, start splitting from a side of the line
STEP 3: each segment of 10 meter must contain some elements like a NUMBER of the segment, a SIDE (left or right), an option to store a value on each side and such..... which i use later in the programmm to fill it with data for each side of the line different........so how i store the data.....?

What should i do in VB2010? making a module with structures or a class or ...? Also code is appreciated...


In VB6 I would make use of this code with a DIM and a TYPE statement (if i remember well, cuz cant get to code due to new windows 7 pc) and store it in a module like this

VB.NET:
Type segment 
    number as integer 
    side as integer 
    points as long
End type
Dim seg(0-100) as segment
 
Last edited:
Well the first thing to say is that it's really not modules v. classes, not least because you can write a class (or several of them) in a module and, indeed, write a structure without a module. A module is no more than an extension to the program code especially useful for truly global variables and objects.

It might be structures v classes but even that's not really a battle. The example you give looks ideal for a structure. It doesn't have any methods, complex properties, or events. It's a simple structured variable. By which you can gather that once it starts to accrue methods, properties and/or events in any number it's probably time to look at classes.

But if, you want code to be 'portable' to other projects then it always makes sense, even with the most simple definitions, to make it a class. Horses for courses, basically. Some people use classes for everything, others mix it up. There's no war, just an extended set of options.

    Private Structure Segment 
       Dim number As Integer
        Dim side As Integer
        Dim points As Long
    End Structure


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim s(10) As Segment
        s(3).number = 3
        s(3).side = 5
        s(3).points = 3
    End Sub
 
It's worth noting that, in VB.NET, modules are simply special classes. In C#, VB's sister language in the .NET world, there are no modules. There are just classes. C# has a 'static' keyword that indicates that a member is accessed via the class itself, rather than an instance of the class. An example of an instance member is the Show method of the Form class. In order to call it, you must first create an instance:
VB.NET:
var f1 = new Form1();

f1.Show();
Dim f1 As New Form1

f1.Show();
An example of a static member is the Show method of the MessageBox class. You never create an instance of the MessageBox class. You simply call Show on the class itself:
VB.NET:
MessageBox.Show("Hello World");
MessageBox.Show("Hello World")
In C# you can also apply the 'static' keyword to a class. Doing so requires that every member of that class is also declared static.

In VB, the equivalent of the 'static' keyword is 'Shared'. You can declare a member Shared and must then be accessed via the class rather than an instance of the class. The 'Shared' keyword is only applicable to members though, not classes. In VB, if you want a type that has all Shared members... you declare a module. With modules, while you don't actually use the 'Shared' keyword but every member acts like a Shared member of a class, i.e. you cannot create an instance of a module and the members are accessed on the type itself rather than an instance of the type. The authors of the language have used the module syntax to maintain the VB paradigm but, when compiled, a module produces the same output as a C# static class.
 
Back
Top