Visual Basic .NET Nightmare or Guide to upgrading a Guru.
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!)
Do I still have intrinsic controls only available to a VB6 interface? Nope they are gone.
What your looking at now are actual .NET framework Objects. They are not COMs or ActiveX. They are all exposed Objects belonging to the .Net Framework. The new IDE allows you to still drag and drop controls onto forms liken to VB6, but that's pretty much where the similarity ends.
OOPs. Funny ehn? Actually it's OOP. It's an acronym I had yet to see until I read the VB. NET booklet that came with. The booklet may serve to confuse you more than teach you anything. I had to interpret everything myself, at some point I decided I was wasting my time, being self taught and all, I'd learn by throwing myself into the IDE and get busy. Anyway OOP stands for Object Oriented Programming. if your a VC6 programmer as well as VB6 you may want to scan ahead to find info more to your liking. If your a VB6 programmer who got by without ever understanding this concept (the original true beauty of a VB program) stay with me.
What's an Object really? Basically it's a bunch of reusable code that breaks down into certain aspects.
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.
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
Note : Also Mkdir is a method of the FileSystem Object.
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.
The reusable code for your Object is going to begin in a class module.
What's the difference between a class module and a module? Simple Answer: Code within a class module is never called directly from your code in a module or a form. In fact the only way to use the reusable code is to reference it as an object.
Let's make a simple add function object that raises an event when the resultant number equals 20.
Create a class module. Name it CalAdd.vb and save all.
We are going to return the answer as an integer from our function to add two numbers together, both being integers. Something to note here is that there are more than one integer variable type offered, for this code choose int16.
Below is your very simple object code.
Public Function Caladd(ByVal nNumber As Int16, ByVal nNumber2 As Int16) As Int16
Caladd = nNumber + nNumber2
If Caladd = 20 Then RaiseEvent Twenty()
Get use to the ByVal identifier, if you ever adventured into a API call you've seen it before. Basically what it means is the variable is passed as the value instead of a reference to where the variable is located in memory. It's opposite is of' course ByRef, which does exactly that, it passes the reference to the variable instead of it's value.
Now let's see our object come to life. If you don't already have a form open, add one to your project. Populate it with a couple of textboxes. One of the new features of VB7 is to be able to double click the control and have one automatically appear on the form as opposed to always having to draw it. Name 'em tbNum1 and tbNum2, then a button name it btnAdd.
The next thing your probably wondering is where is that damn caption property anyway? It's gone and has been replaced by the more common .Text property. If you have ever done html with form buttons you'll see a similarity to the Value property being the same for form controls on a web page, what this means is the whole concept is being standardized, so it makes no difference if your working with a web page, VC or VB.
Now add a label for the resultant. lblResults. Double clicking a control on the form to get to it's default event still works but can be kind of slow sometimes, that's because the IDE has to think of how to auto write the code. I'm guessing in old VB it was just simpler because the intrinsic controls methods were in some higher part of the IDE and more quickly accessed.
Go into the code of your form, if you're lost on how to do this you can always double click a control :P
Okay so where do our declaration statements go? Used to always be the top of the code window and life was simple and easy! Now what's this windows form designer thing in my way? Well, basically consider it to be the top of your code window for now. So we are going to reference our object just below that.
If you type this in rather than copy & Paste, you'll notice that VB is already aware of your Object's Class.
Public WithEvents MyAdd As CalAdd
The WithEvents keyword tells VB that you want to have the Object's events available to your code. Your Object should now be on the drop down on the left the same way your form's objects are.
Now you want to create your object, this is very easy, it's more flippant in VB6. For the sake of simplicity we will do it in the form's load event, like the good old days, later we'll wander into the dark area of the designer code and find a more appropriate spot.
To get to the load event click on Base Class events, your base class is your form Object, if you haven't renamed your form use the following code.
sender As Object,
ByVal e As
System.EventArgs) Handles_ MyBase.Load
MyAdd = New CalAdd()
Now go back to your form and double click your add button to generate it's click event code. Now let's play with your object. Basically we want to use it's .Caladd method to add 2 numbers together, which will return the results for us to pop into our label.
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
Next thing we want to do is signify our twenty event if it should happen. Click the left side drop down of Objects and select MyAdd. Now on the right side click the drop down and you should see your twenty event. Once clicked the code will show up in your form's code window.
Let's just type beep in there. So when the resultant is twenty the program will beep you :).
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.
How do my Objects have properties? Well there are actually two ways, one is considered Illegal by Programming standards and could possibly get you fired and the other used to be very annoying in VB6 is now a little easier.
What not to do. Setting Public variables in your class and pretending they are properties :) This does work though.
How to create a property. Well the first thing you want to do is create what I have read is being called a field (Simply put it's the internal variable for your property) Make this Private, then create your Property declaration balanced on that field.
Let's say we added this line in our add button click event.
If MyAdd.IsTwenty = True Then MsgBox("You can no longer add.") : Exit Sub
So in the same line of code that raises our twenty event we set the .IsTwenty property to true. You do this by actually setting your internal variable (field).
The next time code using the object requests the property, the get property procedure returns the value of the field. The same is also true for the set procedure. When you set the property of the object it sets the value of the internal field variable.
|If Caladd = 20 Then RaiseEvent Twenty() : blnTwenty = True|
Now all we need to do is code our property and field.
blnTwenty As Boolean
Property IsTwenty() As
End GetSet(ByVal Value As Boolean)
blnTwenty = Value
End SetEnd Property
You'll now see a property Icon in the right side drop down of your class.
If you have ever done the same in VB6 you'll see a noticeable improvement here in the coding concept of a property built into it's declaration all at once instead setting all the Get, Let, And Set property functions individually.
It looks and acts more like a function in of itself and can be placed anywhere in your code.