Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Overclock.net Forum > FAQs

 
 
LinkBack Thread Tools
Old 10-12-06   #1 (permalink)
Networking Nut
 
money11465's Avatar
 
intel nvidia

Join Date: Apr 2006
Location: Albuquerque, NM
Posts: 1,454

Rep: 76 money11465 is acknowledged by some
Unique Rep: 70
FAQs Submitted: 1
Hardware Reviews: 1
Trader Rating: 4
Default How To: Code in Visual Basic (vB) - OOP (Objects) added

There are C++ and Java tutorials in the FAQ, so I thought I would just add a vB one. It is a fun language to learn and use, and is a little less acronym-happy than C++ and Java, so it's a good beginner's language. The best compiler is probably MS Visual Studio 2005 (Do not flame me for recommending MS, their vB compilers don't suck). Here is the page with a free download of Express Edition:
http://msdn.microsoft.com/vstudio/ex...d/default.aspx
The full version is much better, but this will do for now.

vB is considered Object Oriented Programming (OOP). All that means is that almost everything you do is calling an object of a class. For example, in Console.Writeline(), you are calling the object Writeline() of the Console class. You can also write these classes yourself.

When you open the program after installing, go to New Project>Console Application.

Some basics
The code module looks like this:
Module Module1
Sub Main()
'The apostrophe is used as the comment escape character
Dim value as Double 'Keyword "Dim" is used to declare variables,
'value is the variable's name, and it is of type double in this case.
'To run applications, go to Tools>Customize>Commands Tab>
'Debug on left, then drag "Start Without Debugging" to your toolbar.
'You will need to scroll down once you get to the Debug menu
'To run applications, you will need to hit the newly-created button

End Sub
End Module


Writing the famous "Hello World!"
Here is a complete vB Hello World application. Not that adding "line" to the end of Console.Write and Console.Read will move the output cursor to the next line, as I have done here.
Module Module1
Sub Main()
Console.Writeline("Hello World!")
End Sub
End Module

The output looks like this:
Hello World!
On the output examples, I will highlight my input as brown, as it will probably be different for you.

Using Console.Write()
Console.Write, as stated before, will keep the cursor the same line.
Module Module1
Sub Main()
Console.Write("Hello ")
Console.Writeline("World!")
End Sub
End Module

The output looks like this:
Hello World!

Concatenation with variables, strings and numbers
The & (ampersand) is used for concatenation. This is very simple:
Module Module1
Sub Main()
Dim variable as String
Dim var2 as Integer
Dim var3 as Integer
Dim var4 as Integer
variable = World
Console.Writeline("Hello " & variable & "!")
var2 = 5
var3 = 6
var4 = var2 & var3
Console.Writeline(var2 & " and " & var3 & " concatenated is: " & var4)
End Sub
End Module

The output looks like this:
Hello World!
5 and 6 concatenated is: 56


Accepting input
Console.Read will accept input. For asthetic reasons, I used .Write, not .Writeline so that the cursor stays on the same line for input. I then used .Readline so that the cursor would go to the next line and start the next output.
Module Module1
Sub Main()
Dim variable as String
Console.Write("Please enter anything: ")
variable = Console.Readline()
Console.Writeline("You entered " & variable)
End Sub
End Module

The output looks like this:
Please enter anything: 12345
You entered 12345


Writing Classes
This is an application that shows how you can pass parameters and use classes. The difference between ByVal and ByRef must be known. ByVal means passing the object (method) the value of a variable, but not altering it. Hence, "By Value". ByRef stands for By Reference, and that means give the method the memory address so that it can operate directly on the variable. When you open the "new console application", click Project>Add Class... You will be asked for a name of your class, but I just kept mine Class1.vb. To access the class you created, you will need to create an object of the class: "Dim arithmetic As New Class1" allows me to access this class by writing arithmetic. and then a method the class. A constructor is a method in the class that is called as soon as I assign an object to it. In vB, they are created as objects called "New"
Module Module1
Sub Main()
Dim arithmetic As New Class1
Dim int1 As Integer
Dim int2 As Integer
Dim sum As Integer
Console.Write("Please enter the 1st integer: ")
int1 = Console.ReadLine()
Console.Write("Please enter the 2nd integer: ")
int2 = Console.ReadLine()
arithmetic.add(int1, int2, sum)
Console.WriteLine("The sum of " & int1 & " and " & int2 & " is: " & sum)
End Sub
End Module


This is the Class:

Public Class Class1
Public Sub New()
Console.WriteLine("Adding Machine (C)2006 money11465")
Console.WriteLine("----------------------------------")
End Sub
Public Sub add(ByRef x As Integer, ByRef y As Integer, ByRef z As Integer)
z = x + y
End Sub
End Class


The output looks like this:
Adding Machine (C)2006 money11465
------------------------------------
Please enter the 1st integer: 7
Please enter the 2nd integer: 8
The sum of 7 and 8 is: 15


Temperature conversion (If/Then/Else)

Module Module1
Sub Main()
Dim choice as Integer
Dim tempf as Integer
Dim tempc as Integer
Console.Writeline("Would you like to:")
Console.Writeline("1. Convert *F to *C?")
Console.Writeline("2. Convert *C to *F?")
Console.Write("Please enter a choice: ")
choice = Console.Readline()
If (choice = 1) Then
Console.Write("What is the temperature in *F? ")
tempf = Console.Readline()
tempc = tempf - 32
tempc = tempc / 1.8
Console.Writeline(tempf & "*F is " & tempc & "*C.")
ElseIf (choice = 2) Then
Console.Write("What is the temperature in *C? ")
tempc = Console.Readline()
tempf = tempc * 1.8
tempf = tempf + 32
Console.Writeline(tempc & "*C is " & tempf & "*F.")
Else
Console.Writeline("You did not enter a valid choice.")
End If
End Sub
End Module

The output looks like this:
Would you like to:
1. Convert *F to *C?
2. Convert *C to *F?
Please enter a choice: 2
What is the temperature in *C? 25
25*C is 80*F.


Intro to GUI
A GUI is a Graphical User Interface (as most of you know). In vB, GUI elements are treated like objects.
Click New Project>Windows Application
Click on "Toolbox" in the upper left corner of your "window"
Drag two Labels, a Button, and a TextBox on to the form.
Arrange them however you like and click the first label, titled "Label1"
On the little window on the lower right corner of the screen, scroll down to Text and change to: "Enter Text to clone:"
Empty the Text field on Label2.
Change the text field on the button to "Clone!"
Double-click on the button.
You should see:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub
End Class

Add to the empty area of Private Sub Button1_Click:
Label2.Text = TextBox1.Text
Run the program and enter some text into your textbox and hit Clone!
Guess what happens? It clones your text!
__________________
System: Money11465's Conroe Build
CPU
Conroe E6600 L630B @ 3.15GHz 24/7
Motherboard
Gigabyte DS3 Rev. 2.0
Memory
2GB Patriot DDR2-800 D9GMH
Graphics Card
eVGA 8800GTS 320MB
Hard Drive
RAID0 250GB Seagate Perp. 16MB Cache
Sound Card
Integrated
Power Supply
700W OCZ GameXstream
Case
Antec P180B
CPU cooling
Thermalright Ultra-120 Extreme
GPU cooling
eVGA stock
OS
WinXP Pro
Monitor
20.1" Sceptre 1680x1050

Last edited by money11465 : 11-03-06 at 02:12 PM
money11465 is offline  
Old 10-25-06   #2 (permalink)
<= Humanaut
 
FrankenPC's Avatar
 
intel nvidia

Join Date: Jun 2006
Location: Bay Area
Posts: 5,548

Rep: 372 FrankenPC is a proven memberFrankenPC is a proven memberFrankenPC is a proven memberFrankenPC is a proven member
Unique Rep: 243
FAQs Submitted: 1
Trader Rating: 4
Default

DAMN good start!!!! Add some basic object references!
__________________
System: TITAN-2
CPU
Q6600 G0 @ 3.4GHZ
Motherboard
ASUS P5K-E
Memory
4GB GSkill DDR2-1000 CAS5
Graphics Card
BFG 8800GT OC
Hard Drive
340GB Raptor SATA II
Sound Card
SB X-FI
Power Supply
SilverStone 750W 4-rail
Case
Lian Li Mid-tower
CPU cooling
OCZ Vindicator
GPU cooling
Stock
OS
Vista Home Premium 32bit
Monitor
Sceptre 24" LCD
1 Million+ Folding at Home points
FrankenPC is offline Overclocked Account  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 03:37 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.11232 seconds with 8 queries