Class like Module without the Dim

Class like Module without the Dim in VB.NET

Access of shared member, constant member, enum member or nested type, through an instance; qualifying expression will not be evaluated

On the last post Make a class like a module , I decided to present the module like a class. Is not something people use or know, but is good bring it to the front.

Check the previous code:


Public Class Module1
    Public Shared iNumber As Integer
End Class



Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oModule1 As Module1 'declare a fake class
        oModule1.iNumber = oModule1.iNumber + 1 'don't need to initiate the variable because is shared
        TextBox1.Text = oModule1.iNumber 'the variable keep raising each time youpress button1
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class



If you place your cursor over the oModule1 (with the green line) , you could bring a little error message:


Access of shared member, constant member, enum member or nested type, through an instance; qualifying expression will not be evaluated. I am sure you seen this previously in your life. If not, this will be your first one.

So why the error message? Well this is my explanation.  Normally, object need to use the new function to ask the computer the spare some space to store the data.  Shared member, constant member, enum member and all the other stuff like them are not really stealing space on your computer. They are part of your program as a definition. They are stored somewhere in your computer while your program is running in 1 single place. When you close the program, those definitions will be disappeared.
I hope this is clear for you. I have a lot of practice, but I am not good to explain these thing. I haven’t study in computer science. My training is something little bit different.

Anyway, if you replace the oModule1 and replace it by the class name Module1, it will work perfectly. Yes! You are using a class and you haven’t set any variable or object with the new function.  You could also delete the first line with the Dim oModule1 as Module1. This is only possible if your class use shared stuff (integer, string, function, methods …)

Here is what would look your class with the shared integer iNumber:

Your file Module1.vb stays the same:

Public Class Module1
    Public Shared iNumber As Integer
End Class


Your Form.vb changes a little bit:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Module1.iNumber = Module1.iNumber + 1 'don't need to initiate the variable because is shared
        TextBox1.Text = Module1.iNumber 'the variable keep raising each time youpress button1
    End Sub

End Class



What I am showing here should work the same way in C#.NET because it uses the same library .NET Framework.  I can’t tell you for sure if it will work in VB6. VB6 acts differently and is a half way from procedure-oriented programming and object oriented programming.

In VBA (Visual Basic for Application). I honestly haven’t even thought about trying doing strange things like this. VBA works like VB6, but VBA is a simplified Visual Basic and is dependent to the Main application (MS-Excel, MS-Word, AutoCAD, See Electrical Expert, Catia…)



References:


Download project sample : SampleModuleToClass.zip


Site web de Check Technologies : http://checktechno.ca

Buy the same edition I bought: Visual Studio 2010 Professional (Old Version)

0 Response to "Class like Module without the Dim"

Posting Komentar