190 likes | 275 Views
Java to Visual Basic.net. Basic VB.net concepts. Matt Presensky Brian Esham Booze Alan Partners mpresens@boozealan.com besham@boozealan.com. Assumptions. This tech seminar assumes that you have had some programming experience in A201 and A202.
E N D
Java to Visual Basic.net Basic VB.net concepts Matt Presensky Brian Esham Booze Alan Partners mpresens@boozealan.com besham@boozealan.com
Assumptions • This tech seminar assumes that you have had some programming experience in A201 and A202. • We also assume that you remember basic object oriented principles, like how classes work.
JAVA public class Test { public static void main(String []args) { Foo f = new Foo(12); f.doubler(); f.halver(); } } class Foo { private int num; public Foo(int input) { input = num; } public void doubler() { num = num + num; } public int halver() { num = num/2; return num; } } VISUAL BASIC .NET Module Test Sub Main() Dim f As New Foo(12) f.Doubler() f.Halver() End Sub Class Foo Dim num As Integer Sub New(ByVal input As Integer) input = num End Sub Sub Doubler() num = num + num End Sub Function Halver() num = num / 2 Return num End Function End Class End Test
Modules, Functions, and Subs, Oh my! • In VB there is all kinds of crazy stuff! Remember Java, where methods looked like this: • Void method (no returned value): public void foo(String input) { String blah = input; } • Visual Basic void methods are called subroutines, they are written like this: Public Sub foo(ByVal input As String) Dim blah As String = input End Sub
(Continued) • What about a method that has a return type? In VB.net they are called functions and are written like this: Private Function foo2(ByVal number As Integer) Dim blah As Integer = number Return blah End Function • Some Major Differences so far: • VB.net unlike Java is not case totally sensitive. • There are no brackets or semicolons! • The void keyword is replaced by two different method types called Sub and Function. - Visual Basic has a native IDE, Visual Studio.
(Continued) • ByVals – these are used when a single value is referenced instead of an address/name of a variable. This will become more logical when dealing with event handling. • Modules – A module amounts pretty much to a DLL file. It is a logical segment that is referenced by other segments as necessary. • As – This simply denotes and precedes variable type. • Dim – Dimensions, these are used to declare and allocate storage space for variables.
If statements and Loops • If statements in VB .net are like this: If Number < 10 Then Digits = 1 End If • Then we can add an else too. ElseIf Number < 100 Then Digits = 2 End If • While loop: While condition instructions End While
Instantiation • When we create a new object in VB.net we must add a “New” keyword subroutine into the class: SubNew(ByVal input As Integer) input = num End Sub • This method/sub must be included in an object that needs an instance. • Other than that the new subroutine acts just like a regular constructor in Java, it can either take an argument or not.
Instantiation Continued • Now when we want to instantiate the class we declare a new Dim and put the instance of the class in it like so: Dim InstanceName As New Classname(param) • Keep in mind that the Instance Name can be anything you want and that the Class must have a New subroutine.
Basic Inheritance • Inheritance is integral to OOP, and as such Visual Basic .net has been made to support it. • VB works it like this: Class DerivedClass Inherits BaseClass ‘this is how you declare that a class Inherits another from the base’ • To access inherited methods you can use the MyBase keyword like this: MyBase.derivedMethod()
Inheritance Continued • Overriding methods in a subclass works like this: • First you need to declare the method that might need to be overridden, like so: Public Overridable Function Foo() • Then you declare the override: Public Overrides Function Foo(ByVal stuff As String)
Option Explicit? • In Visual Basic .net this is automatically set to on. • You do not really need to mess with this as it is only there for interoperability reasons between Visual Basic .net and it’s predecessor Visual Basic 6. • Option Explicit’s main reason for life in VB.net is to enforce or ignore OOP concepts. What it does is give you the OPTION to create objects instead of forcing you to.
Visual Studio.net • Visual Studio Integrated Development Environment (IDE) is THE tool for rapid development with Visual Basic.net. • Here are a few basics of the IDE that will help you get started: Solution Explorer – this displays classes and resources used by the solution. Properties Window – This is where you set attributes of form components in a Visual way. i.e. change bgColor or set sizes. Object Browser – This displays the app’s object instances. Class View – This is what you will want to have open most of the time. The class view is an expandable display of the classes, and methods of your app. Toolbox – This list displays the components of your form that you can simply drag and drop into it.
Visual Studio Continued • To compile the program, go to build at the top and select build solution. After that there little play button, just like a VCR, hit that to run. • If you have errors they will appear on the bottom of the screen • VS also has intellicode which can save you typing time and list the methods applicable to an object or variable. • For a more advanced topic the debugging is great in VS, not only does it feature breakpoints but you can watch single variables line by line to see if they function correctly!
My First Windows App • Open up Visual Studio and create your first Visual Basic Application. • We will create a form that takes in a String from a textbox, calculates the number of characters in the String and then shows that info in the textbox once a button is clicked. • In addition, we will provide a button that exits the program
Event Handling • Handling events is easy with Visual Studio. Here is how we can get a button to save the input in a text box. 1) Add the button to the form. 2) Add the textbox to the form. 3) Double click the button and it adds the handler for you in the code automatically! 4) Now all that you have to do is fill in what you want to happen when the event is detected. All of this goes within the created method. 5) In the code view check out the box with the thunderclap icons. These list all possible events that can be handled, you can set this accordingly.
Finished Program Public Class Form1 Inherits System.Windows.Forms.Form Dim inString As String Dim length As Integer ‘VISUAL STUDIO GENERATED CODE GOES HERE’ Private Sub Calc_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc_btn.Click inString = TextBox1.Text length = inString.Length TextBox1.Text() = "Length of String is: " & length End Sub Private Sub Exit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_btn.Click Me.Close() End Sub End Class
References • Check out these handy references for help. • Microsoft’s MSDN (Go to Languages Section) this will tell you how to do EVERYTHING in VB. • http://msdn.microsoft.com/ • Books 24x7 Type in Visual Basic .net in the search engine, there are many books on the subject. • http://library.books24x7.com/home.asp?