Visual Basic .NET Nightmare or Guide to upgrading a Guru.

Page 2

I think it all works! (what to do next?)

Let's learn some basic stuff. (If you think something here is Wrong! Please post a message with corrections. Pobody's Nerfect!)

  1. Properties. Sounds familiar yes? And it really is, especially if you ever dragged and dropped a command button on a form and changed it's Caption property to "run" or whatever. Properties set all the attributes for an object, whether it's a visual control or not.

  2. Methods. Methods are simply functions of said object. In reusable code a method would look like this within the Object's code. 

     Public Function MakeXP

         FileSystem.MkDir("C:\XP")

    End Function  

    Note : Also Mkdir is a method of the FileSystem Object.  

     

  3. Events. Events take place when some part of the code is executed due to code elsewhere affecting it.  Possibly internal or external.  For instance an event can be fired off when the end user does something to the GUI (Form/Control) containing the object. Related to a control such as a button when the end user clicks it, it fires off an event that your might be familiar with as a Click event.

Where do Objects come from?  Why from the Stork, Of' course! Actually the .Net Framework contains enough reusable code to keep a single person busy for the rest of one's life!  But this is not learning about an object, so what we need to do is create one.

To Objectify Code.

Public Event Twenty()

Public Function Caladd(ByVal nNumber As Int16, ByVal nNumber2 As Int16) As Int16

Caladd = nNumber + nNumber2

If Caladd = 20 Then RaiseEvent Twenty()

End Function

Public WithEvents MyAdd As CalAdd

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

MyAdd = New CalAdd()

End Sub

Dim MyAnswer As Int16

If tbNum1.Text = "" Or tbNum2.Text = "" Then lblResults.Text = "Please enter some valid numbers" : Exit Sub

MyAnswer = MyAdd.Caladd(CInt(tbNum1.Text), CInt(tbNum2.Text))

lblResults.Text = MyAnswer

Private Sub MyAdd_Twenty() Handles MyAdd.Twenty

Beep()

End Sub

Now you have a couple choices.  You can either run your program immediately or you can build (Compile) it first to look for errors.  Either way once you run it by typing in 2 numbers that equal 20 you should hear the beep.

If MyAdd.IsTwenty = True Then MsgBox("You can no longer add.") : Exit Sub

If Caladd = 20 Then RaiseEvent Twenty() : blnTwenty = True

Private blnTwenty As Boolean

Public Property IsTwenty() As Boolean

Get

Return blnTwenty

End Get

Set(ByVal Value As Boolean)

blnTwenty = Value

End Set

End Property

 

Page 3