Friday, November 2, 2012

Make a class like a module

Make a class like a module


On a previous post Use the Module in VB NET, I show that we could use a module to share all type of object or variables anywhere in the project. There are many advantages to use the module, but not everyone work that way. Many people use the new oriented object programming. This is like C and C++. The only very big difference is oriented object programming.
OK, you love to program the old way and you don’t like whatever geek’s doing. Then let’s try to make one step ahead…

Make a new project in Visual Basic Express, Visual Studio 2010 or any edition of Microsoft Visual Studio. Make a form1 with a TextBox1 and a Button1. Make a Module1.


In your Module1, make this very simple:


Module Module1
    Public iNumber As Integer
End Module



iNumber is a simple global variable. This variable is available anywhere, everywhere and almost at all time.
Make something with your form1 to read or write that iNumber. Here an example:


Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Module1.iNumber = Module1.iNumber + 1
        TextBox1.Text = Module1.iNumber
    End Sub
End Class



I can’t make it simpler than that. Now if you run the program, each time you press the Button1, it increment the counter and displays is in the TextBox1. Notice that the variable is inside Module1 and not inside the Form1.
Time to change the module to a class FOR NO REASON.
Do the following changes:

Public Class Module1
    Public Shared iNumber As Integer
End Class


And


Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oModule1 As Module1
        oModule1.iNumber = oModule1.iNumber + 1
        TextBox1.Text = oModule1.iNumber
    End Sub
End Class




Run the program and it will work. WHAT ???????? HOW ??????
First of all, the shared function tells “everyone” could use the variable or the method.
Second of all, you don’t need to initialize the class with new. WHAT???  That class is not really using any space. I hate to say that, but is kind of true.  So even if Visual Studio warns you that you haven’t initialized the variable, it will still work.  So ignore all the green underline under oModule1.
The matter the fact, you are now smarter than Visual Studio or Visual Basic Express.
And third, when you leave the function button1_Click , iNumber is not delete, not reset, not disappeared, not sent in garbage collector, not destroyed, what the hell????? Hahaha!
So what is the whole point of using this fake class or this sophisticated Module ?? Well this helps me and you to understand other type of class and how things work. I discovered that the more you do strange things, the more you master that thing.

Next Article, I’ll show you another class that doesn’t need the new function. I don’t know how they call it in .NET but is a parent class that must have a child class. Stay tune for a new article.

Reference:

Buy the same edition I bought:



No comments:

Post a Comment