Visual Basic .NET Nightmare or Guide to upgrading a Guru.
What's the point of Objects?
Can't I program without making Objects? Yes, but without understanding what they are you are going to hit the wall rather quickly while trying to write your application.
Without whipping out those lovely charts that came with, let's try and figure out what the heck the hierarchy is.
Let's do that by closing your current solution so that all you have is the IDE open. or if your just getting to it, open the IDE without a solution.
Now let's open the object browser. This is one of the most handy tools in VB. If your good at figuring stuff out it's also a learning tool although it is meant as a reference.
So what do we have for objects? Nothing as of yet, the point is you can't have objects without a project and vice versa. Now create a new solution and then add a project, let's call our Windows Application "Demo". This creates our default form object. Our form's object is also based on it's class. You can see the relationship between your class's code and the object you created from it. So your Form's object is based on the class code created by the .NET framework developers.
Now we have all kinds of Objects or Base classes for our project. And a lot of them are system. So what does this mean?
Setting the cosmic Key.
The system is of' course the operating system that the .NET Framework is installed on. It gives you objects that you use in your code to interact with the operating system and will probably give enough code precompiled to get you up and running.
If your old enough to have been playing with computers to remember MOS-DOS, you'll probably already be aware that the system handles much of the control over the computer's hardware, software and IO functionality. MOS-DOS stands for Machine Operating System/Disk Operating System. In the days of windows, DOS became a subset of code, the underlying code to the front end GUI windows. GUI-Graphical User Interface.
So what if I just want a simple application that has one window and doesn't do all that much? Well no matter how much of it you use you don't drag all that code around with your program. It's part of the .NET frame work that is installed by the end user on the target computer. Your code creates objects from the classes of .NET to use while your program is running, there for you do not need to ship the Visual Basic interpreter code like VB 6 and previous versions. Nor do you have to ship any support dll and OCX files, if you had them referenced in your project. This idea applies only if you stay within the framework's object collection and don't add any COM or ActiveX components. Hopefully you won't have to.
So my form is a class, too? Your form is an object based on the form class. To see this clearly open your class viewer in the solution's explorer tool window. The first thing you see is your application's name. Now expand the tree until you see your form1 object. Below that is it's source code.
Bases and interfaces are the actual classes that your form object is made from, similar to the way your MyAdd object is based on your CalAdd class.
What's in a Name Space? This is kind of new to me as well. I have always taken the dot extension for granted in VB the idea that I could just type Me and the Dot and get all kinds of aspects automatically was amazing enough. But what it really is, is the structure of your code's referencing objects. AKA the Name space. For System there are a few different ones listed already in your object browser. System itself is one of them. Let's focus on System.Windows.Forms.
Anytime you see a plural in the name it's referencing a collection of objects. System is of' course your computer's OS. Windows is the collection of classes that create objects that handle and create windows on a computer that has a GUI. And forms contains all the code necessary to create a windows form. This includes textboxes, buttons, etc.
As you can see by looking at your form's code window the only thing public in the windows collection is forms. So the exact name space in order to create the form object is System.Windows.Forms.Form .
Next let's generate an object based on the form class. Under the declarations put this statement
|Private MyForm As New System.Windows.Forms.Form()|
This will set the variable MyForm and create the new object all in one statement. At this point in your code execution the form already exists and you can manipulate it. Next let's make a button on the current form and name it btnShow.
Double click it to generate it's click event in the code window then put in this line of code.
sender As System.Object, ByVal
e As System.EventArgs) Handles
You'll notice when you type in methods it automatically appends the parenthesis to the end of it. It will also automatically indent code according to it's structure. This is a great enhancement to the code window. I hate re-indenting every time I change my code around.
Now run your code. Click the button. And your new form will appear, everything about it. it's shape size, window state, etc are all preset by the form class.
Okay next thing you really want to be aware of, is the form window was generated before the .Show method. That means the form doesn't exit beyond the underlying code you wrote. Clicking the X on the window will call the .Close method. In dot net that means the object is destroyed. Clicking the button again will cause an error.
This is very different than VB 6. In VB 6 you could not create a form directly from code, you could however create a new form object from one that already exists. That's because VB6 handled the Base class creation behind the scenes. Also the X closed the current form, but you could reload it with the form .Show method. In order to show your form again you have to regenerate the object from it's class.
Let's try something similar with both VB6 and VB7 combined. The new way of getting your form on the screen.
Let's add a new form to the project from the new items window. This should now be viewed as form2.
Okay vb6 says in order to use another form all you have to do is call it's .Show method and Walla it pops up. But the thing is, even though you have created it's class with it's view all setup, you have not created an instance of it as an object yet.
so let's change out the statement in the declarations to this
|Private MyForm As New form2|
This then creates an instance of your form's class for which you can now open with the same call as before.
If you haven't tried it yet, close form2 and then click the button again you'll get a lovely error message.
So how do I show my form2 again? You have to create a new instance of it or cancel the closing event and simply hide it. Go into form2's code window. Bring up the closing event. then add this
sender As Object,
ByVal e As
e.Cancel = True
This will save your form and data entered into it, or any changes you made to it. It also now acts very much like VB6, the difference is you have a lot more control over it.
If I created form2 who created form1? As you can probably see when your project runs it creates the start-up object. Otherwise the program would have no where to start. You can also change your startup to a module named main and and it will call the sub main procedure just like in VB6.