Go Back   Random Stuff Forums > Technical > Programming

Programming Discuss any type of programming here.

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 13-07-2009, 03:11 PM
Blupig's Avatar
Blupig (Offline)
wat i don't even
 
Join Date: Aug 2007
Posts: 159
Blupig is an unknown quantity at this point
Thumbs up [VB.net][TUT] Developing Your First Methods

Before starting, please make sure of have the VB.net IDE installed. You can get the express version 100% free directly from Microsoft off the following link:
http://www.microsoft.com/express/download/
There is also a trial version available of the complete Visual Studio set if you're more interested in that.

----

What is a method?
A method is an instruction separate from your control code. You can call upon them at any time you like.

What is the difference between a function and a sub?
A function returns a value as where a sub simply performs an action and returns nothing.

What are method properties/elements?
These vary depending on the method you're dealing with. They are extra values that you must enter to use the method properly. They are elements of which the method is dependant, unless they are optional (optional elements will be discussed later).

Subs
To start off your method, you'll need something that needs to be done. For your first one, we should start with a sub as they are more basic. This is the beginning of a sub:
Public Sub RunProgram(ByVal Path As String)
When you're coding a method, you'll probably use the "Public" class a lot. This allows the method to be used across your project, not only in the current class. This is different for class libraries since they are separate libraries that must be referenced and can contain several different class sections (I won't go into detail about libraries). If you want your method to only be accessible by the current class, you would put "Private" instead of "Public". Afterwards, there is the method name. You can name it whatever you like, as long as it doesn't interfere with any other methods (if you have the same name as another method, then you'd do something like this:
Public Shadows Sub RunProgram(ByVal Path As String)
The name cannot contain any spaces. Then, you have your method elements. Within the brackets, you have a choice of allowing the element to be directly manipulated, or by having a copy of it manipulated. To use a copy, you would use ByVal as shown above. For directly manipulating the variable within the sub, you would use ByRef (this isn't used very often and you will most likely not need it for quite some time; if you want to learn more about it, google it). Afterwards, you have your typical declaration. If your element will be called on as a string, then you declare it as a string, etc. This ends the first line of the sub. You can have as many elements as you like, provided that they are separated by commas (e.g. Private Sub Hello(ByVal Name As String, ByVal LastName As String).

Next, you have your code block. This can be whatever you like. In the first coded example above, we had made a "RunProgram" method. Let's finish it off:
Public Sub RunProgram(ByVal Path As String)
    Process.Start(Path)
End Sub
As you can see, this is how you would use your method elements. You simply call to them like normal declared variables, except that instead of only you being able to set the value, the user could do it too (through input in a textbox, etc.).

Functions
Functions are essentially the same as subs, except that they return a value. The following example will be a basic function that adds two numbers together and returns the result:
Public Function AddUp2Numbers(ByVal Num1 As Integer, ByVal Num2 As Integer) As Integer
    Dim Result As Integer = Num1 + Num2
    Return Result
End Function
As I'm sure you noticed, in a function there is an extra "declaration" in the first line. This defines the variable type that is being returned. To define what is returned within the function, you would use the Return method. In this function, the return is the two added elements as an integer.

In a function, you need to return something on all paths, or else VB will freak out and toss you an error. This means that if you add an if statement into your function, your function must always return a value in all possibilities of the code. If you don't want to return anything, you can simply use the Nothing method, so like this: "Return Nothing".

To set something to the returned value of a function, you would do something like the following:
TextBox1.Text = FunctionName()
With both functions and subs, you don't even need elements. Here are examples of non-element methods:
Public Sub RunGoogle()
    Process.Start("http://www.google.ca/")
End Sub
Public Function Add4And6() As Integer
    Return 4 + 6
End Function
Inheritance and overrides
Since this tutorial is about making your first methods, you don't need to know this stuff right now. If you're ambitious though, MSDN explains it well:
http://msdn.microsoft.com/en-us/library/aa290362(VS.71).aspx

Where to put my methods?
Methods can go anywhere you want. A common area for them are modules and class libraries. If you're unfamiliar with those though, you can just place them in your current form class and things will work the same as if they did in a library or in a module, except for the organizational bonuses and other features of class libraries and modules.

>> Tutorial brought to you by Blupig. <<






Last edited by Blupig; 13-07-2009 at 03:13 PM..
Reply With Quote
Reply


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 04:54 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright © 2006 - 2010, Rsforums.org