Passing variables from one procedure to another - vb.net

How can I use a vairable from Sub Main, in another sub.
Example
Sub Main()
Dim x As Long
End Sub
Sub Test() Dim y = x End Sub
Is this possible on vb.net

You could pass it to a function and return it again:
Sub Main()
Dim x As Long
' Give x a value
x = Test(x)
' Do something else with x
End Sub
Function Test(x As Long) As Long
' Do something with x
Return x
End Sub
Or declare it as a global variable:
Dim x As Long
Sub Main()
' Do something with x
End Sub
Sub Test()
Dim y = x ' Do something else
End Sub

There is a very easy way to do this.
Declare the value out of all the Subs on the top.
Public Class Form 1
Dim x As Long
Private Sub Main ()
End Sub
Private Sub Test ()
End Sub
End Class
And then write y = x on whichever Sub you want!

Related

Call module with multiple inputs

I've tried finding a solution to this problem but have been unable to.
In generic form:
Module1
Sub Source()
Call Module2.Run
End Sub
Module2
Sub Run()
Value = 10
Some code which uses Value as input
End Sub
What I want to be able to do is to be able to define multiple Values in Module1 and then run Module2.Run() with each value.
Module1
Sub Source()
Value = 10, 20, 30
Call Module2.Run (10)
Call Module2.Run (20)
Call Module2.Run (30)
End Sub
Module2
Sub Run()
Value = Input from Module1.Source()
Some code which uses Value as input
End Sub
Or something along these lines. Any input would be greatly appreciated.
You can pass parameters through arguments like so
Sub Sub1 ()
Dim myVal as Long
myVal = 1000
Sub2 (myVal) 'The "Call" is not necessary
End Sub
Sub Sub2 (myVal as Long) 'This sub requires an input to run (myVal)
MsgBox myVal
End Sub
You can create an array, fill it and pass as an argument.
Avoid using names like Source and Run which are already used by Excel;.
Option Explicit
Sub Sour()
Dim arr_1d() As Variant
arr_1d = Array("val1", "val2", "val3")
Dest arr_1d
End Sub
Sub Dest(arr_1d() As Variant)
Dim y As Long
For y = LBound(arr_1d) To UBound(arr_1d)
Debug.Print arr_1d(y)
Next
End Sub

Use data from one userform to formula in another

Quite new to vba and have been trying to figure out how to use data from my first userform to my second userform.
let's call them userform1 and userform2
So in userform1, user will enter data for a, b, c ,and d. Upon clicking OK, userform2 will open:
Private Sub OK_Click()
l = cdbl(a.value)+cdbl(b.value)
w = cdbl(c.value)+cdbl(d.value)
userform1.hide
userform2.show
End Sub
In userform2, i need the values of a and b (entered by user in userform1) to compute for x and y:
Private Sub OK_Click()
x = cdbl(a.value)+cdbl(d.value)
y = cdbl(b.value)+cdbl(c.value)
End Sub
tried placing a placing a b c d l w x and y in a module and setting in Public but code still doesn't work. Error "object required"
Thanks very much in advance.
A simple maybe not the best way is to declare variables a and b in the class modules. The better way might be to pass them via properties.
Code in Userform1
Option Explicit
Public a As Double
Public b As Double
Private Sub CommandButton1_Click()
a = TextBox1.Value
b = TextBox2.Value
Me.Hide
End Sub
Code in Userform 2
Option Explicit
Public x As Double
Public y As Double
Private Sub CommandButton1_Click()
TextBox1.Value = x + y
End Sub
And you can test it like that
Sub Demo()
Dim f1 As New UserForm1
Dim f2 As New UserForm2
f1.Show
f2.x = f1.a
f2.y = f1.b
f2.Show
End Sub
PS No checks or whatsover if the values entered in the textboxes are really valid.
a rough way
1) declare some Public variables of Double type
hence put this a the top of any module of your choice in the project
Public aValue As Double, bValue As Double, cValue As Double, dValue As Double
2) change your code as follows
UserForm1
Option Explicit
Private Sub OK_Click()
Dim l As Double, w As Double
aValue = CDbl(a.Value)
bValue = CDbl(b.Value)
cValue = CDbl(c.Value)
dValue = CDbl(d.Value)
l = aValue + bValue
w = cValue + dValue
Me.Hide
With UserForm2
.Show
End With
End Sub
UserForm2
Private Sub OK_Click()
Dim x As Double, y As Double
x = aValue + dValue
y = bValue + cValue
End Sub
and the likes ...

Override a Function

The following "aMacro" returns the error "Ambiguous name Detected" I understand why. Anybody know a way to override the first definition and and only use the definition inside of the function so that aFunction will return x - y ?
Besides changing the name.
Function aFunction(x As Integer, y As Integer) As Integer
aFunction = x + y
End Function
Sub aMacro()
Function aFunction(x As Integer, y As Integer) As Integer
aFunction = x - y
End Function
MsgBox aFunction(4, 3)
End Function
A function can be Private or Public, but the scope is always the whole module.
This can simulate "override function" with 4 class modules:
Functions, IFunction, FunctionAdd, FunctionSubtract.
class module Functions:
Function aFunction(x As Integer, y As Integer) As Integer
aFunction = x + y
End Function
interface IFunctions:
Function aFunction(x As Integer, y As Integer) As Integer
End Function
class module FunctionAdd:
Implements IFunctions
Private mFunctions As Functions
Private Sub Class_Initialize()
Set mFunctions = New Functions
End Sub
Private Sub Class_Terminate()
Set mFunctions = Nothing
End Sub
Private Function IFunctions_aFunction(x As Integer, y As Integer) As Integer
IFunctions_aFunction = mFunctions.aFunction(x, y) ' Uses the standard aFunction
End Function
Class module FunctionSubtract:
Implements IFunctions
Private mFunctions As Functions
Private Sub Class_Initialize()
Set mFunctions = New Functions
End Sub
Private Sub Class_Terminate()
Set mFunctions = Nothing
End Sub
Private Function IFunctions_aFunction(x As Integer, y As Integer) As Integer
IFunctions_aFunction = x - y ' Override aFunction, subtract values
End Function
You can test this with this:
Dim f As IFunctions
Set f = New FunctionAdd: Debug.Print f.aFunction(1, 2)
Set f = New FunctionSubtract: Debug.Print f.aFunction(1, 2)
Of course this is tedious for one function.
I could be useful is you have a lot of functions to override in many classes.
Try adding in the Optional value in the function. If the optional value isnt included in the call then it wont be references in the function.
Function aFunction(x As Integer, y As Integer, Optional override As Boolean) As Integer
If Not override Then
aFunction = x + y
Else
aFunction = x - y
End If
End Function
Sub aMacro()
MsgBox aFunction(4, 3)
MsgBox aFunction(4, 3, True)
End Sub

Using a sub var into other sub visual basic

How can i get a var value from a sub by calling it in other sub ? like
Sub test()
Dim a As Integer
a = 1
End Sub
Sub testshow()
MessageBox.Show(test.a)
End Sub
In VBA (which your tag states) you need to change Sub into Function which will be:
Function test()
Dim a As Integer
a = 1
test = a
End Function
Sub testshow()
MsgBox test
End Sub
EDIT after comment: If you using more then one variable then:
Function test(whichVar)
Dim a As Integer
If whichVar = 1 then
a = 100
ElseIf whichVar = 2 Then
a = 200
'etc...
End if
test = a
End Function
Sub testshow()
MsgBox test(2) 'will give you 200
End Sub

Return an array of a classes method names

I've got the following. How do I query the class myExample and return an array of the class's method names?
Sub Main()
Dim x As New myExample
'<<would like to return an array of x's method names i.e. {"hello","world","foo","bar"}
Console.WriteLine("press [enter] to exit")
Console.Read()
End Sub
Class myExample
Public Shared y As Integer
Public Sub hello()
y = 1
End Sub
Public Sub world()
y = 2
End Sub
Public Sub foo()
y = 3
End Sub
Public Sub bar()
y = 4
End Sub
End Class
All methods (incl. inherited):
Dim example As New myExample()
Dim t As Type = example.GetType()
Dim methods = t.GetMethods()
Dim allMethodNames() As String = methods.Select(Function(m) m.Name).ToArray()
Type.GetMethods