Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Passing and Getting Variables ByVal
 

Passing and Getting Variables ByVal

Hi everyone I was wondering how does one go about passing variables using ByVal? For example:

Code Snippet

Function Get_Connections(ByVal AR_Connections As ArrayList)

Dim regKey As RegistryKey
regKey = Registry.LocalMachine
regKey = regKey.OpenSubKey("SOFTWARE")
regKey = regKey.OpenSubKey("GOD Service")

Dim Str_TempConnStr As String = "n/a"
Dim Str_Columns As String = ""
Dim x As Int16 = 1

Do Until Str_TempConnStr = ""

Try
Str_TempConnStr = regKey.GetValue("Conn_" & x).ToString

Catch ex As Exception
Str_TempConnStr = ""

End Try

AR_Connections.Add(Str_TempConnStr).ToString()
x += 1

Loop

Return AR_Connections

End Function


How do I get the value of AR_Connections from another Function or Sub? For example, if I have a Sub:

Code Snippet

Public Sub mySub(Get_Connections())


^^ Does it go something along those lines? Or am I wrong here?

Thanks for the help + assistance in advance. Regards, Onam

OnamC  Monday, January 28, 2008 2:38 PM

Hi,

As you are getting the Sub keys within the function then specify

the return type at the end of the FUNCTION first line like this.

The yellow highlighted bit is the Return TYPE

By the way if you pass an object ByRef then if the SUB or FUNCTION changes its value then the original value in the calling SUB or FUNCTION is also changed. Personally I do not like to use ByRef ( unless I can't help but use it ).

ByVal passes a copy of an object.

ByRef passes a reference to the object or "what it does" is to

change the original item if that item is changed within a SUB or FUNCTION.

Regards,

John

Code Snippet

Option Strict On

Imports Microsoft.Win32

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myArrayList As ArrayList

myArrayList = Get_Connections()

End Sub

Function Get_Connections() As ArrayList

Dim AR_Connections As New ArrayList

Dim regKey As RegistryKey

regKey = Registry.LocalMachine

regKey = regKey.OpenSubKey("SOFTWARE")

regKey = regKey.OpenSubKey("GOD Service")

Dim Str_TempConnStr As String = "n/a"

Dim Str_Columns As String = ""

Dim x As Int16 = 1

Do Until Str_TempConnStr = ""

Try

Str_TempConnStr = regKey.GetValue("Conn_" & x).ToString

Catch ex As Exception

Str_TempConnStr = ""

End Try

AR_Connections.Add(Str_TempConnStr).ToString()

x = Convert.ToInt16(x + 1)

Loop

Return AR_Connections

End Function

End Class

John Anthony Oliver  Monday, January 28, 2008 4:27 PM

Hi,

Try this to understand ByVal and ByRef please.

Add one button to a FORM to try this.

Regards,

John

Code Snippet

Option Strict On

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myInteger As Integer = 42

Call HalfMeByRef(myInteger)

'Shows 21 as the variable is passed ByRef

'so the original number gets changed.

MessageBox.Show(myInteger.ToString)

myInteger = 36

Call WontDoubleMe(myInteger)

'Still shows 36 as ByVal will mean a copy of the VALue

'gets passed with no reference so the value of 'myInteger'

'will not get changed.

MessageBox.Show(myInteger.ToString)

End Sub

Public Sub HalfMeByRef(ByRef aNumber As Integer)

aNumber = aNumber \ 2

End Sub

Public Sub WontDoubleMe(ByVal aNumber As Integer)

aNumber = aNumber * 2

End Sub

End Class

John Anthony Oliver  Monday, January 28, 2008 4:44 PM

There's no need for Get_Connections to return AR_Connections since the caller will already have a reference to it. I think what you want to do is something like

Dim connections As New ArrayList

Get_Connections(connections)

mySub(connections)

Mattias Sjögren  Monday, January 28, 2008 3:16 PM
What if for instance I wanted to do it that way. How would I go about doing it? I don't like using Global Variables as it gets messy.

Cheers for the replies Smile
OnamC  Monday, January 28, 2008 3:21 PM

Hi,

As you are getting the Sub keys within the function then specify

the return type at the end of the FUNCTION first line like this.

The yellow highlighted bit is the Return TYPE

By the way if you pass an object ByRef then if the SUB or FUNCTION changes its value then the original value in the calling SUB or FUNCTION is also changed. Personally I do not like to use ByRef ( unless I can't help but use it ).

ByVal passes a copy of an object.

ByRef passes a reference to the object or "what it does" is to

change the original item if that item is changed within a SUB or FUNCTION.

Regards,

John

Code Snippet

Option Strict On

Imports Microsoft.Win32

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myArrayList As ArrayList

myArrayList = Get_Connections()

End Sub

Function Get_Connections() As ArrayList

Dim AR_Connections As New ArrayList

Dim regKey As RegistryKey

regKey = Registry.LocalMachine

regKey = regKey.OpenSubKey("SOFTWARE")

regKey = regKey.OpenSubKey("GOD Service")

Dim Str_TempConnStr As String = "n/a"

Dim Str_Columns As String = ""

Dim x As Int16 = 1

Do Until Str_TempConnStr = ""

Try

Str_TempConnStr = regKey.GetValue("Conn_" & x).ToString

Catch ex As Exception

Str_TempConnStr = ""

End Try

AR_Connections.Add(Str_TempConnStr).ToString()

x = Convert.ToInt16(x + 1)

Loop

Return AR_Connections

End Function

End Class

John Anthony Oliver  Monday, January 28, 2008 4:27 PM

Hi,

Try this to understand ByVal and ByRef please.

Add one button to a FORM to try this.

Regards,

John

Code Snippet

Option Strict On

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myInteger As Integer = 42

Call HalfMeByRef(myInteger)

'Shows 21 as the variable is passed ByRef

'so the original number gets changed.

MessageBox.Show(myInteger.ToString)

myInteger = 36

Call WontDoubleMe(myInteger)

'Still shows 36 as ByVal will mean a copy of the VALue

'gets passed with no reference so the value of 'myInteger'

'will not get changed.

MessageBox.Show(myInteger.ToString)

End Sub

Public Sub HalfMeByRef(ByRef aNumber As Integer)

aNumber = aNumber \ 2

End Sub

Public Sub WontDoubleMe(ByVal aNumber As Integer)

aNumber = aNumber * 2

End Sub

End Class

John Anthony Oliver  Monday, January 28, 2008 4:44 PM

You can use google to search for other answers

Custom Search

More Threads

• OFD trying to get only the filename
• Remove My Namespace
• Prevent controls from refreshing or painting (eliminate flicker)
• Saving and Retrieving Rich text to and from sql server
• XMLdocument - sending node data to a text box
• Connecting to BTrieve
• i would like to ask why when open the openfiledialog .....
• VBA object added in a form
• Construct an object of a certain type from a string
• CRYSTAL REPORT IN MDI FORM (VB.NET)