Introduction to VB: Objects & Controls, Properties, Events, Methods
Chapter #1: Objects & Controls
What is an object?
Letīs put as an example a Cup. A regular common coffee cup. There you have an object. You own a cup. You have it at home, and you use it whenever you want. You can notice its properties, or change them. You can get it to do certein things, and you can do things to it too.
Now, in the world there are millions of different coffee cups. But there are some things that every cup has. That would be the definition of a "class".
The class "CoffeeCup", defines the guidelines of the properties of a cup, but there are other properties that can particularly change how your own coffe cup looks, does things, or gets things done to it.
Letīs say that you have a table at home, and you have your cup on it. Your table will be the recipient of your cup. In your house, there are many cups, but thereīs a single one that has been placed on your table, and you will be working with that one only, so when you refer to it, you want things done with that one only, not on every cup of your home. In VB we would refer to that particular cup, by calling its parent first:
Table1.Cup1 Cup1 will have some properties predefined by the class "CoffeeCups". Those would be, as an example: Height Width Depth |
Every cup in this universe has at least three dimensions appreciatable by the human eye. Now, I donīt want to argue with you about strings, black holes, and six-dimensioned-reality. We will only consider those three here. These properties can accept decimal natural rational values:
Table1.Cup1.Height = 0,10 foot Table1.Cup1.Width = 0,10 foot Table1.Cup1.Depth = 0,10 foot Your cup may have one more property that can be changed to almost anything: Caption |
The caption will be a word, or a letter or whatever string of characters you may want to write on the side of your cup, to differenciate it to the user (or the coffee-drinker) from any other cups in the house:
Table1.Cup1.Caption = "Dadīs" |
What does "Table1" mean? It means that you have several tables (or you could have) at home, but we are referring to the particular table that holds "Cup1".
Now, letīs say that you have placed a coffepot on your table. and you want to pour some cofee on your cup. You would have to make this reference to invoke the 'method' (weīll be talking about these in a minute) "pour" in relation to cup1:
Table1.CoffeePot1.pour(Table1.Cup1) |
See, you are trying to pour coffee from your "CoffeePot1", that is contained in "Table1". But you canīt just pour coffee in the air, or it would fall on the table, or on the floor. You need to do this on an object that can accept the method "pour". That would be your "Cup1".
What happens when the user has poured enough coffee and the cup is full? If you let the user pour too much coffee, (enough to go over the top of your Cup1) the coffee will flood, so you must "control" that event. It would look something like this:
Do While Table1.Cup1.Full = False Table1.CoffeePot1.pour(Table1.Cup1) Loop |
In that way, you can only pour coffee until your "Cup1" is full. You could also add a message to the user informing that the cofee cup is full, and encouriging to stop pouring:
Do While Table1.Cup1.Full = False Table1.CoffeePot1.pour(Table1.Cup1) Loop If MsgBox("The Cup is full, do you want to keep pouring?", vbExclamation + vbYesNo, "Watch it!") = vbYes Then Call continue_pouring() Else Exit Sub End if |
Well, I hope that you got the idea of Objects and their relation between each other, and how controls affect the flow on the program on an "object-oriented programming language" like VB. If you are full of doubts about what youīve read, but think that you got the main idea, I suggest that instead of mailing me those questions, you continue to "Chapter #2: Properties" so you can clear it out a litle more.