Alright, today we are going to talk about custom classes. This is, in my opinion, a really cool aspect of Object Orientated Programming. This allows you to create custom data types that can be used throughout your project. Not only does this save tons of code, but it makes keeping your program organized easier and makes everything look neater.
The difficulty is EASY!!!
Ok, let's say you are writing a program that will track gas mileage for cars. Now lets pretend that you are writing the piece of code that will deal with adding a new trip. Let's say you have a form that has places to put in the different pieces of data. Let's also assume that you have a database class that is used to read and write from the database. You could write:
Dim tripMileage as Int16
Dim tripDate as Date
Dim gasUsed as Double
DataBase.AddTrip(tripMileage, tripDate, gasUsed)
And that would work fine. But when you got to a point where you wanted to list all of the trips a user had put in, what are you going to do?
You could:
Dim tripMileage() as Int16
Dim tripDate() as Date
Dim gasUsed() as Double
This would be creating arrays of each type to use to store the data for each trip. The biggest problem with that is you must know beforehand how many trips you are dealing with. Also I personally never liked using arrays like this. What I would recommended doing is create a class called Trip.vb and using it to store trip information as its own data type. Ex:
Public Class Trip
Public tripMileage as Int16
Public tripDate as Date
Public gasUsed as Double
Public sub new()
tripMileage = 0
tripDate = new Date()
gasUsed = 0.0
End Sub
End Class
Then you could have the sub that gives you the list of every trip just give you an ArrayList of type Trip. You would use these trips as data types in your programming like this:
Dim t as new Trip()
t = DataBase.getNewestTrip()
labelTripMileage.Text = t.tripMileage
labelTripDate.Text = t.tripDate.ToShortDateString()
labelGasUsed.Text = t.gasUsed
labelGasMileage.Text = Math.Round(t.gasUsed / t.tripMileage, 2)
As you can see the variable t is of type Trip, this can make things a lot easier when programming to have a data type that is exactly what you are dealing with in your program.
I hope you had fun and learned something. If you have any good ideas for topics, feel free to email them to me.
Sunday, October 31, 2010
Wednesday, October 6, 2010
First Post
Alright, here we go...
This is my new blog. First a little about me, I am a student at Virginia Tech. I am currently a freshman. I plan to study Computer Engineering. All of the Visual Basic programming knowledge I have is self taught, I have never taken a VB class in my life, and I don't think I ever will. I am a very big fan of the .NET framework, and I absolutely love Visual Studio. I have never felt quite as comfortable with any other IDE (Integrated Development Environment) other than Visual Studio. I started programming on a TI-84 calculator and then moved on to VB.NET which I still use to this day. I can also program in JAVA and C#.NET and will soon be learning C++.
But enough about me, lets do something with programming...
For my fist example / lesson, I will be demonstrating the use of Message Boxes. I know that this is a very elementary topic, but you have to start somewhere, and I don't want to scare away any new programmers out there.
Difficulty: VERY EASY
To use a message box as a notification, you simply use the code:
MsgBox("Your message goes here!", ,"Message Box Title")
The reason I have that blank argument in there is because that is where the message box style would go. For this example, we just wanted the message box to be a notification, so it only needs an 'OK' button. Lucky for us that is the default style. If you don't have a specific title in mind for the message box, you can simply close end the argument list with the message and the title will be the name of your application.
If you want to ask a question with the message box, such as yes or no, you must set a style parameter. You must also be ready to receive the answer.
Dim ans As New MsgBoxResult
ans = MsgBox("Do you want to save before exiting?", MsgBoxStyle.YesNoCancel, "Save?")
If ans = MsgBoxResult.Yes Then
' Save and exit
End If
If ans = MsgBoxResult.No Then
' Don't save and exit
End If
If ans = MsgBoxResult.Cancel Then
' Don't save and don't exit
End If
As you can see here, I allowed there to be three possible answers to the question I asked: Yes, No, or Cancel. First I had to declare a variable to store the user's response in. This type of variable is aptly named 'MsgBoxResult'. Then I told the program to assign the value of the message box to the variable I declared. As you can see for the style parameter, I used 'MsgBoxStyle.YesNoCancel' this tells the computer that you want a message box with those three options. Now don't get confused, you can just put any old thing there, you must pick a style that exists.
I can't think of much else to say about message boxes, that is about the limit of their functionality, so I will wrap up with a few notes to remember about message boxes.
- While the message box is showing, the program does not run. What I mean is the code stops at the line of the message box and waits for the user to dismiss the message box before continuing. This can be useful if you want to check on variable values in the middle of a complex operation.
- Message boxes usually pop up on top of your application. One exception to his is in ASP.NET (web page programming) where the message box may not popup on top of the web browser. I have not figured out how to fix this and it is very annoying. Due to that flaw, message boxes are virtually useless in web programming.
I hope you enjoyed this little talk, and hopefully learned something. If you have a good idea for next time feel free to email them to me.
This is my new blog. First a little about me, I am a student at Virginia Tech. I am currently a freshman. I plan to study Computer Engineering. All of the Visual Basic programming knowledge I have is self taught, I have never taken a VB class in my life, and I don't think I ever will. I am a very big fan of the .NET framework, and I absolutely love Visual Studio. I have never felt quite as comfortable with any other IDE (Integrated Development Environment) other than Visual Studio. I started programming on a TI-84 calculator and then moved on to VB.NET which I still use to this day. I can also program in JAVA and C#.NET and will soon be learning C++.
But enough about me, lets do something with programming...
For my fist example / lesson, I will be demonstrating the use of Message Boxes. I know that this is a very elementary topic, but you have to start somewhere, and I don't want to scare away any new programmers out there.
Difficulty: VERY EASY
To use a message box as a notification, you simply use the code:
MsgBox("Your message goes here!", ,"Message Box Title")
The reason I have that blank argument in there is because that is where the message box style would go. For this example, we just wanted the message box to be a notification, so it only needs an 'OK' button. Lucky for us that is the default style. If you don't have a specific title in mind for the message box, you can simply close end the argument list with the message and the title will be the name of your application.
If you want to ask a question with the message box, such as yes or no, you must set a style parameter. You must also be ready to receive the answer.
Dim ans As New MsgBoxResult
ans = MsgBox("Do you want to save before exiting?", MsgBoxStyle.YesNoCancel, "Save?")
If ans = MsgBoxResult.Yes Then
' Save and exit
End If
If ans = MsgBoxResult.No Then
' Don't save and exit
End If
If ans = MsgBoxResult.Cancel Then
' Don't save and don't exit
End If
As you can see here, I allowed there to be three possible answers to the question I asked: Yes, No, or Cancel. First I had to declare a variable to store the user's response in. This type of variable is aptly named 'MsgBoxResult'. Then I told the program to assign the value of the message box to the variable I declared. As you can see for the style parameter, I used 'MsgBoxStyle.YesNoCancel' this tells the computer that you want a message box with those three options. Now don't get confused, you can just put any old thing there, you must pick a style that exists.
I can't think of much else to say about message boxes, that is about the limit of their functionality, so I will wrap up with a few notes to remember about message boxes.
- While the message box is showing, the program does not run. What I mean is the code stops at the line of the message box and waits for the user to dismiss the message box before continuing. This can be useful if you want to check on variable values in the middle of a complex operation.
- Message boxes usually pop up on top of your application. One exception to his is in ASP.NET (web page programming) where the message box may not popup on top of the web browser. I have not figured out how to fix this and it is very annoying. Due to that flaw, message boxes are virtually useless in web programming.
I hope you enjoyed this little talk, and hopefully learned something. If you have a good idea for next time feel free to email them to me.
Subscribe to:
Posts (Atom)