Why a data after the parameter list?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

When creating a function or sub procedure what purpose does the data type
declaration outside of the parameter list after the parenthesis on the end
serve?
for example Public Sub mypgm(Byval fld1 as integer) as string
 
When creating a function or sub procedure what purpose does the data
type declaration outside of the parameter list after the parenthesis
on the end serve?
for example Public Sub mypgm(Byval fld1 as integer) as string

The line above is invalid because sub procedures don't return anything.

Functions on the otherhand return data, and it's used to declare the type
of data being returned.
 
It's the return value of the function. Your example is illegal
syntax, since a Sub does not return a value.

Correct syntax would be:
Public Function MyPgm(ByVal fld1 As Integer) As String

The function MyPgm returns a String.
 
Hello

When creating a function or sub procedure what purpose does the data type
declaration outside of the parameter list after the parenthesis on the end
serve?
for example Public Sub mypgm(Byval fld1 as integer) as string

It's purpose is to allow a method to return a value after it is
finished processing.

For Example:

////////////////////
Public Function IsUserValid(ByVal userName As String) As Boolean
If (the user is valid) Then
Return True
Else
Return False
End If
End Function

Public Sub Foo()
Dim isValid As Boolean = IsUserValid("Seth Rowe")
If isValid Then
MsgBox("Seth Rowe is a Valid User")
Else
MsgBox("Seth Rowe is not a Valid User")
End If
End Sub
///////////////////

The code to check whether a given name is a valid user has been
encapsulated into the IsUserValid method. Any other method can now
pass in a string to this function and will receive a boolean value
from the method that will indicate whether the user is, or isn't,
valid. Other examples are methods that might construct an object, such
as a DataTable. In general, the constructed DataTable will be created
in the method and returned as a return value (Public Function
BuildDT() As DataTable).

Thanks,

Seth Rowe
 
Winlin,

Looking at the other answers shows that it has a lot of possibilities.
Forget VB6 that was time consuming finding itself the correct type.

However beside the answers you got already, don't forget overloading. By
telling te types you can create more methods, with almost the same code.

Cor
 
However beside the answers you got already, don't forget overloading. By
telling te types you can create more methods, with almost the same code.

I don't think overloading applies here as you can't overload based on
return type.

Thanks,

Seth Rowe
 
Back
Top