Thursday, December 11, 2008

Friday December 12: Coding with Class, PART ONE

Greetings, chattering code monkeys!



Today we are less than a month's worth of classes away from programming in Java. In celebration of this upcoming event --and just because its Friday-- we are going to look at a concept that Visual Basic has more or less copied from Java in the first place: the Class.



In Java, everything is a class; the whole darned language is Class-based. And, ever since VB went to .NET framework, Visual Basic has classes too. A Class is kind of like a blueprint: you use them to create actual objects. You've been using them all along: if you create a new Project and get into the code for your form, the very first line of code is

"Public Class Form1"


Today we will create our own classes. You will start out simply, then branch off into your own creations. Here's how we will start:


A) Create a new Project named after yourself plus the phrase "Legos"


B) on the Form1 that you get, create a TextBox


C) We are going to create a new type of control that can go on your form, so the first step will be to create a Class. You do this by Clicking on the Project menu, then clicking on the menuitem "Add Class". I named mine redButton, since our new class will be based on the Button class that already exists in VB


D) You will notice that you will get an empty set of code that looks like this:
Public Class redButton


End Class


Since your new class will create a button, you need to tell the program that. So, the first line of code is as follows:


Inherits Windows.Forms.Button


Classes can inherit properties and methods and other cool stuff from other classes, kind of like how I inherited my father's blue eyes and low tolerance for liars. Our class will be able to use all the stuff that buttons have, including this:



Private Sub redButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click 'this stuff is all on one line


All buttons can be clicked. So will yours


The next step is to add something simple to that button click. How's about we start with its caption and color? Add this inside that click method thingamabob:


Text = "Whoo Hoo!""
BackColor = Color.Red


The final step is to add this new type of button to your form. I did mine at Form Load:



'create a new button from your class
Dim myNewButton As New redButton( )
'what text does this button have?
myNewButton.Text = "Deep Thought"
'add the Button that is created from the class to the form
Me.Controls.Add(myNewButton)


This code adds the new kind of button to your form when you run the program, and not until then. You can also set its size and location like so:


'where on the form do we create this new button?

myNewButton.Location = New Point(100, 50)

'setting the size of the Button

myNewButton.Size = New Size(100, 55)


OK, let's run that thing and see if it works. Then I'm going to show you some other different stuff I've done with my button, and I will ask you to come up with some new stuff of your own.


Cheers,


Mr. L

No comments: