faqts : Computers : Programming : Languages : Visual basic : Visual basic .NET

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

23 of 28 people (82%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Visual Basic .net: Function: How to create a function in Microsoft Visual Basic .NET?

Jan 13th, 2004 18:05
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 22 December 2003 - 11:46 pm -------------------

Visual Basic .net: Function: How to create a function in Microsoft 
Visual Basic .NET?

---
---

a basic function looks like this:

Public Function ... As String
 ' do something
 return( <your result> )
End Function

---
---

similarly you can write:

Public Function ... As String
 ' do something
 ... = <your result>
End Function

---
---

e.g.

 Private Function MyTest() As String
  Return ("Today is Monday")
 End Function

then to call this function use e.g.

  Console.Write(MyTest())

---

e.g.

The same result can be received by using this:

 Public Function MyTest() As String
  MyTest = "Today is Monday"
 End Function

then to call this function use e.g.

  Console.Write(MyTest())

---

e.g.

 Public Function MyTest() As String
  Return ("Today is Monday")
 End Function

then to call this function use e.g.

  Console.Write(MyTest())

---

e.g.

 Private Function MyTest() As Integer
  Return ( 1 )
 End Function

then to call this function use e.g.

  Console.Write(MyTest())

---

e.g. when you need to supply 1 parameter, and return 1 result, you 
could use:

 Private Function MyTest( ByVal myday As String ) As String
  Return( "Today is " & myday )
 End Function

then to call this function use e.g.

 Console.Write( MyTest( "Monday" ) )

---

e.g. when you need to supply 2 parameters, and return 1 result, you 
could use:

 Private Function MyTest( ByVal myday As String, ByVal mynumber As 
Integer ) As String
  mynumber = mynumber + 1
  Return ("Today is " & myday)
 End Function

then to call this function use e.g.

 Console.Write( MyTest( "Monday", 1 ) )

---

e.g. when you need to supply 2 parameters, and return 1 result, and 
return another result via passing by reference, you could use:

 Private Function MyTest( ByVal myday As String, ByRef mynumber As 
Integer ) As String
  mynumber = mynumber + 1
  Return ("Today is " & myday)
 End Function

then to call this function use e.g.

 Dim mynumber As Integer = 0
 Console.Write( MyTest( "Monday", mynumber ) )
 Console.Write( mynumber ) ' this will show (0 + 1) or thus 1

---
---

more in general the format is:

[Private | Public | Friend | Protected | Protected Friend]  
Function <your function name> ( [ByVal | ByRef] <your parametername1> 
As <your parameter return type1>, [ByVal | ByRef] <your 
parametername2> <your parameter return type2>, ..., [ByVal | ByRef] 
<your parameternamelast> <your parameter return typelast> ) As <your 
function returntype>

 ' do something

 Return( <your result> )

 ' you can also use:
 ' <your function name> = <your result>

 ' do something

End Function

---
---

Internet: see also:

---

Visual Basic .net: Procedure: How to create a procedure in Microsoft 
Visual Basic .NET?
http://www.faqts.com/knowledge_base/view.phtml/aid/27632/fid/1610

---

Visual Basic: Class: Access: Modifier: Which access modifiers in 
Visual Basic.NET? [private/public]
http://www.faqts.com/knowledge_base/view.phtml/aid/27365/fid/1610

---

Procedures and Functions in Visual Basic
http://www.juicystudio.com/tutorial/vb/procedures.asp

----------------------------------------------------------------------