Difference between Function and Sub procedures in VB6 - vb6

I cant understand about the difference between Function and Sub procedures in VB.NET.
The one with Function :
Private Function remainder (intno1 As Integer, intno2 As _ Integer) As Integer
Dim intresult As Integer
intresult = intno1 Mod intno2
remainder = intresult
End Function
and then by this way i call it :
Private Sub cmdrem _Click()
Dim intm As Integer, intn As Integer
Dim intmod As Integer
intm = Val (txtno1.Text)
intn = Val (txtno2.Text)
intmod = remainder (intm, intn)
lblres.text = "Answer Is = " + Str(intmod)
End Sub
would you please help me ?

some main differences between procedure and function in vb are that the function returns a value but sub procedure never returns value.
the return type must be defined in function declaration.
A function always is declared with keyword Function and a sub procedure is declared with keyword Sub.
Function ends with the keyword end function
and procedure ends with keyword end sub.

Related

How to pass an array with fixed length strings to a function

I am trying to pass an array of fixed length strings to a function. However, the function will not know the size of the strings. The parameter should take any size strings. How would I go about this? Here is my code:
Public Type myRecord
names(15) As String * 250
End Type
Function def:
Function HasQuotes(textArr() As String) As String
End Function
Usage:
HasQuotes(rowRec.names)
where rowRec is a MyRecord.
When I run this code I get the following error Type Mismatch: array or user defined type expected. This is because the parameter is not defined as textArr() As String * 250. How can I make the parameter accept any string length?
You Have to follow Proper syntax you can Try Following updated Code
Private Type myRecord
names(15) As String * 250
End Type
Private Sub Command1_Click()
Dim rowRec As myRecord
rowRec.names(0) = ("Vaibhav")
Dim v As String
v = HasQuotes(rowRec.names)
End Sub
Function HasQuotes(ByRef textArr() As String * 250) As String
MsgBox textArr(0)
End Function
You are going to have trouble passing an array of fixed-length strings of any length. One simple solution is to convert them to an array of variable-length strings:
Private Sub Test()
Dim rowRec As myRecord
Dim names(15) As String
Dim i As Integer
Dim s As String
rowRec.names(0) = "Brian"
rowRec.names(15) = "Stafford"
'move the data to an array of variable-length strings
For i = 0 To 15
names(i) = rowRec.names(i)
Next
s = HasQuotes(names)
'move the data back to the original array if needed
For i = 0 To 15
rowRec.names(i) = names(i)
Next
End Sub

VB6 Random Number Generation Seed Scope

What is the scope of the Randomize(seed) function? The way this code is written will the random number generator be seeded with a value of 20 when generate is called from myfunction? Does the randomize function change a global variable or some local variable?
Thanks for any help!
Function myfunction()
Call seed()
Call generate()
End Function
Function seed()
Randomize (20)
End Function
Function generate()
Dim X As Integer
X = CInt(100*Rnd)
End Function
If you call Randomize with explicit value or do not call Randomize at all you will get the same sequence by every start of the program. Try:
Dim i As Long
Dim sText As String
sText = ""
For i = 1 To 10
sText = sText & CInt(100 * Rnd(10)) & vbCrLf
Next i
MsgBox sText
But of course sometimes it could be wanted result...

How do I declare global array in VBScript

I'm trying to store an array value so that I can reuse when Sub is called more than once.
I would like to prevent from reassigning values to the array if value exist.
My code is something like this.
Dim views()
Sub runit()
For i=0 To 3
test()
Next
End Sub
Sub test()
ReDim Preserve views(0)= "test"
' - other codes that I want to run-
End Sub
I get " Type mismatch :'choseviews'" error.
If I move "Dim views()" inside "Sub test", I don't get the error.
How do I declare global array in VBScript?
If it's not possible, is there any ways to prevent reassigning array when Sub is called?
This following code does not work but you may get an idea what I'm trying to do .
Dim views()
Sub runit()
For i=0 To 3
test()
Next
End Sub
Function IsArrayDimmed(arr)
IsArrayDimmed = False
If IsArray(arr) Then
On Error Resume Next
Dim ub : ub = UBound(arr)
If (Err.Number = 0) And (ub >= 0) Then IsArrayDimmed = True
End If
End Function
Sub test()
If IsArrayDimmed(views) Then
Else
ReDim Preserve views(0)= "test"
End If
' - other codes that I want to run-
End Sub
Thank you for your help.
If I understand correctly, it seems like you want to declare a global array variable, and then add items to that array, without being limited to a static number of elements. In other words, you need to dynamically increase the size of the array by re-allocating it.
The global declaration is correct and belongs where you have it:
Dim views()
What you wrote here is incorrect syntax, you cannot assign a value and ReDim at the same time.:
ReDim Preserve views(0)= "test"
Additionally, that would ReDim the array to size 0, which is the opposite of what you want.
If you wish to "push" values on that array you should use a function like this which handles the redim to increase the size of the array before adding the value to the tail of the array:
Function Push(ByRef arrTarget, ByVal varValue)
Dim intCounter
Dim intElementCount
ReDim Preserve arrTarget(UBound(arrTarget) + 1)
If (isObject(varValue)) Then
Set arrTarget(UBound(arrTarget)) = varValue
Else
arrTarget(UBound(arrTarget)) = varValue
End If
Push = arrTarget
End Function
Use it like this:
Call Push(views,"test")
Any variable instantiated in the global scope will be a "global" variable. However, you should pass that variable explicitly into other scopes "by reference" if you want to have any changes persist in the original scope. You can do that using the ByRef keword in your Function or Sub declaration.
Sub test(ByRef viewsArray)
Now within test you will reference viewsArray which acts as a pointer to views.

How to get the value from method in Visual Basic 6

The code below returns error after the return statement
Private Sub Command1_Click()
Dim str As String
str = display("test")
MsgBox (str)
End Sub
Public Function display(s As String) As String
s = "updated"
Return s
End Function
Any ideas why?
Change display function. The difference is that in vb6 functions return a value not with return, but with it's name(in this case display), like below.
Public Function display(s As String) As String
s = "updated"
display = s
End Function

Visual Basic 6.0 Passing by Value Reference difference

In the following code, I get a compile time error because i is treated as a variant. The error is: "ByRef Argument type mismatch.".
But if I pass the parameters ByVal, there is no error why?
Private Sub Command2_Click()
Dim i, j As Integer
i = 5
j = 7
Call Swap(i, j)
End Sub
Public Sub Swap(ByRef X As Integer, ByRef Y As Integer)
Dim tmp As Integer
tmp = X
X = Y
Y = tmp
End Sub
When you Dim several variables on a single line ie Dim i, j as Integer j is dimmed as an integer, but i is a variant. You need to declare each variable type explicitly. I prefer to include only a single variable per line.
Dim i As Integer, j As Integer
or
Dim i As Integer
Dim j As Integer
This is something I learned when I inherited another programmer's code
ByVal autoconverts the variant into a integer because it is passing a value. While the ByRef is attempting to pass a variable that you can modify in the subroutines. In essence I is X in the ByRef scenario. VB6 doesn't allow you to modify a variant as a integer.

Resources