473,462 Members | 1,055 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing varibles using "Byref"

I will excute the code below to get MethodName array. These codes are
edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives me
a varning message: The varible "sSourceMethodName" may throw a Null
reference exception because it passes using "Byref" before being assigned.

Although it's just a warning I still want to let ift fit the VS2005's
recommended mode.

Any suggestions will be appreciated,

Peter

'------------------------------------------
Dim sSourceSampleName() As String
Dim Count as integer
Count = GetMethodName( sSourceMethodName)

Public Function GetMethodName( ByRef sMethodName() As String) As Integer
Dim odr As OleDbDataReader
Dim i, j As Integer

j = GetMethodCount() 'Defined in other places
ReDim sMethodName(j - 1)

Dim oCmd As New OleDbCommand

'Connection ConnImport has been opened
With oCmd
.Connection = ConnImport
.CommandType = CommandType.StoredProcedure
.CommandText = "udpGetMethodName"
End With

odr = oCmd.ExecuteReader
Do While odr.Read()
sMethodName(i) = odr(0)
i += 1
Loop

If Not odr.IsClosed Then odr.Close()
Return j

End Function
'--------------------------------------------------------------
Jan 5 '07 #1
8 1453
Peter,

Get rid of that fixed array and use an arraylist, than you can pass byval
and your program becomes direct more efficient as well.

Cor

"Peter" <zl*****@sina.comschreef in bericht
news:OI*************@TK2MSFTNGP02.phx.gbl...
>I will excute the code below to get MethodName array. These codes are
edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives me
a varning message: The varible "sSourceMethodName" may throw a Null
reference exception because it passes using "Byref" before being assigned.

Although it's just a warning I still want to let ift fit the VS2005's
recommended mode.

Any suggestions will be appreciated,

Peter

'------------------------------------------
Dim sSourceSampleName() As String
Dim Count as integer
Count = GetMethodName( sSourceMethodName)

Public Function GetMethodName( ByRef sMethodName() As String) As Integer
Dim odr As OleDbDataReader
Dim i, j As Integer

j = GetMethodCount() 'Defined in other places
ReDim sMethodName(j - 1)

Dim oCmd As New OleDbCommand

'Connection ConnImport has been opened
With oCmd
.Connection = ConnImport
.CommandType = CommandType.StoredProcedure
.CommandText = "udpGetMethodName"
End With

odr = oCmd.ExecuteReader
Do While odr.Read()
sMethodName(i) = odr(0)
i += 1
Loop

If Not odr.IsClosed Then odr.Close()
Return j

End Function
'--------------------------------------------------------------


Jan 5 '07 #2
Thanks for your reply, Cor,

I will try to use arraylist to get rid of that fixed array problem.
But I also want to get the return values by calling a function. I used
"Byref" because the function can change the "Byref" parameter just like a
return value. Can I still use "Byref" under VS2005?

Peter
Jan 5 '07 #3
Peter:

I think you'll find the use of the array has little to do with it. You
should use a collection class, it's just easier but the problem is that you
don't need to declare the parameter as ByRef. I just replied to another
message along the same lines. An array "is" a reference, you pass it ByVal.
When the value gets to the function it is a reference to the array.

You _can_ pass it by reference but you open it up to be completely swapped
out. Create a new array in your function and assign that one to the ByRef
variable and you will see you destroyed the original one.

Picture what is happening in the computer when you pass variables to
functions. They are placed onto the "stack" if you had a 20,000 element
array would we imagine it would push 20,000 values onto the stack? It
always pushes a single value that acts as a pointer to where the array is.

Don't worry about keywords like ByRef... conceptually languages always have
the option to pass values or references, it isn't something they can
eliminate in VS2005 or even VS2009.

Tom
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Peter,

Get rid of that fixed array and use an arraylist, than you can pass byval
and your program becomes direct more efficient as well.

Cor

"Peter" <zl*****@sina.comschreef in bericht
news:OI*************@TK2MSFTNGP02.phx.gbl...
>>I will excute the code below to get MethodName array. These codes are
edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives
me a varning message: The varible "sSourceMethodName" may throw a Null
reference exception because it passes using "Byref" before being assigned.

Although it's just a warning I still want to let ift fit the VS2005's
recommended mode.

Any suggestions will be appreciated,

Peter

'------------------------------------------
Dim sSourceSampleName() As String
Dim Count as integer
Count = GetMethodName( sSourceMethodName)

Public Function GetMethodName( ByRef sMethodName() As String) As Integer
Dim odr As OleDbDataReader
Dim i, j As Integer

j = GetMethodCount() 'Defined in other places
ReDim sMethodName(j - 1)

Dim oCmd As New OleDbCommand

'Connection ConnImport has been opened
With oCmd
.Connection = ConnImport
.CommandType = CommandType.StoredProcedure
.CommandText = "udpGetMethodName"
End With

odr = oCmd.ExecuteReader
Do While odr.Read()
sMethodName(i) = odr(0)
i += 1
Loop

If Not odr.IsClosed Then odr.Close()
Return j

End Function
'--------------------------------------------------------------



Jan 5 '07 #4
Tom,

There is a redim in Peter's code, in my idea that is creating a complete new
array at a new point. I am not sure if you can than pass by value and get it
back on the same position. My idea was not.

Therefore my answer was as it was.

Cor

"Tom Leylan" <tl*****@nospam.netschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
Peter:

I think you'll find the use of the array has little to do with it. You
should use a collection class, it's just easier but the problem is that
you don't need to declare the parameter as ByRef. I just replied to
another message along the same lines. An array "is" a reference, you pass
it ByVal. When the value gets to the function it is a reference to the
array.

You _can_ pass it by reference but you open it up to be completely swapped
out. Create a new array in your function and assign that one to the ByRef
variable and you will see you destroyed the original one.

Picture what is happening in the computer when you pass variables to
functions. They are placed onto the "stack" if you had a 20,000 element
array would we imagine it would push 20,000 values onto the stack? It
always pushes a single value that acts as a pointer to where the array is.

Don't worry about keywords like ByRef... conceptually languages always
have the option to pass values or references, it isn't something they can
eliminate in VS2005 or even VS2009.

Tom
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Peter,

Get rid of that fixed array and use an arraylist, than you can pass byval
and your program becomes direct more efficient as well.

Cor

"Peter" <zl*****@sina.comschreef in bericht
news:OI*************@TK2MSFTNGP02.phx.gbl...
>>>I will excute the code below to get MethodName array. These codes are
edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives
me a varning message: The varible "sSourceMethodName" may throw a Null
reference exception because it passes using "Byref" before being
assigned.

Although it's just a warning I still want to let ift fit the VS2005's
recommended mode.

Any suggestions will be appreciated,

Peter

'------------------------------------------
Dim sSourceSampleName() As String
Dim Count as integer
Count = GetMethodName( sSourceMethodName)

Public Function GetMethodName( ByRef sMethodName() As String) As Integer
Dim odr As OleDbDataReader
Dim i, j As Integer

j = GetMethodCount() 'Defined in other places
ReDim sMethodName(j - 1)

Dim oCmd As New OleDbCommand

'Connection ConnImport has been opened
With oCmd
.Connection = ConnImport
.CommandType = CommandType.StoredProcedure
.CommandText = "udpGetMethodName"
End With

odr = oCmd.ExecuteReader
Do While odr.Read()
sMethodName(i) = odr(0)
i += 1
Loop

If Not odr.IsClosed Then odr.Close()
Return j

End Function
'--------------------------------------------------------------




Jan 5 '07 #5
dgk
On Fri, 5 Jan 2007 14:33:12 +0800, "Peter" <zl*****@sina.comwrote:
>Thanks for your reply, Cor,

I will try to use arraylist to get rid of that fixed array problem.
But I also want to get the return values by calling a function. I used
"Byref" because the function can change the "Byref" parameter just like a
return value. Can I still use "Byref" under VS2005?

Peter
ByVal often does allow the object's properties to be changed (value
type vs reference type objects). Simple things like integers and
strings are value types and a copy is actually made and passed to the
function so changes to them are indeed lost. But most things, and I'm
not sure about the arraylist that Cor suggested, can be changed within
a subroutine or function and persist when the sub exits.

Try it yourself. Create a simple object with a text property and pass
it either byval or byref to a subroutine and have that sub change the
property. When it gets back to the calling program, it won't matter
how you passed it, the property is still changed. Try it again, but
this time have the sub set the parameter to a new instance of the
object. That will make a difference.
Jan 5 '07 #6
Ah yes the redim might be a problem. Might be interesting to find out (I'm
not likely to redim an array) so I won't do it however :-) Clearly he would
be better of with a dynamic array.

It seems as I look the code over again that the return value "j" is just the
value of GetMethodCount() which likely could be called before
GetMethodName() and in that way the count wouldn't have to be returned, the
array could be resized prior to calling GetMethodName() and if it was zero
the call to GetMethodName() could be skipped entirely.

Tom

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:OD**************@TK2MSFTNGP02.phx.gbl...
Tom,

There is a redim in Peter's code, in my idea that is creating a complete
new array at a new point. I am not sure if you can than pass by value and
get it back on the same position. My idea was not.

Therefore my answer was as it was.

Cor

"Tom Leylan" <tl*****@nospam.netschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Peter:

I think you'll find the use of the array has little to do with it. You
should use a collection class, it's just easier but the problem is that
you don't need to declare the parameter as ByRef. I just replied to
another message along the same lines. An array "is" a reference, you
pass it ByVal. When the value gets to the function it is a reference to
the array.

You _can_ pass it by reference but you open it up to be completely
swapped out. Create a new array in your function and assign that one to
the ByRef variable and you will see you destroyed the original one.

Picture what is happening in the computer when you pass variables to
functions. They are placed onto the "stack" if you had a 20,000 element
array would we imagine it would push 20,000 values onto the stack? It
always pushes a single value that acts as a pointer to where the array
is.

Don't worry about keywords like ByRef... conceptually languages always
have the option to pass values or references, it isn't something they can
eliminate in VS2005 or even VS2009.

Tom
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>Peter,

Get rid of that fixed array and use an arraylist, than you can pass
byval and your program becomes direct more efficient as well.

Cor

"Peter" <zl*****@sina.comschreef in bericht
news:OI*************@TK2MSFTNGP02.phx.gbl...
I will excute the code below to get MethodName array. These codes are
edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives
me a varning message: The varible "sSourceMethodName" may throw a Null
reference exception because it passes using "Byref" before being
assigned.

Although it's just a warning I still want to let ift fit the VS2005's
recommended mode.

Any suggestions will be appreciated,

Peter

'------------------------------------------
Dim sSourceSampleName() As String
Dim Count as integer
Count = GetMethodName( sSourceMethodName)

Public Function GetMethodName( ByRef sMethodName() As String) As
Integer
Dim odr As OleDbDataReader
Dim i, j As Integer

j = GetMethodCount() 'Defined in other places
ReDim sMethodName(j - 1)

Dim oCmd As New OleDbCommand

'Connection ConnImport has been opened
With oCmd
.Connection = ConnImport
.CommandType = CommandType.StoredProcedure
.CommandText = "udpGetMethodName"
End With

odr = oCmd.ExecuteReader
Do While odr.Read()
sMethodName(i) = odr(0)
i += 1
Loop

If Not odr.IsClosed Then odr.Close()
Return j

End Function
'--------------------------------------------------------------

Jan 5 '07 #7
Well I just tried it and as we knew it would it failed when the array is
passed by value. Thank goodness :-) Clearly redimming the array creates a
new reference and the calling routine never gets it (unless it was returned
expressly of course).

The ArrayList is doubtlessly the better option.
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Ah yes the redim might be a problem. Might be interesting to find out
(I'm not likely to redim an array) so I won't do it however :-) Clearly
he would be better of with a dynamic array.

It seems as I look the code over again that the return value "j" is just
the value of GetMethodCount() which likely could be called before
GetMethodName() and in that way the count wouldn't have to be returned,
the array could be resized prior to calling GetMethodName() and if it was
zero the call to GetMethodName() could be skipped entirely.

Tom

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:OD**************@TK2MSFTNGP02.phx.gbl...
>Tom,

There is a redim in Peter's code, in my idea that is creating a complete
new array at a new point. I am not sure if you can than pass by value and
get it back on the same position. My idea was not.

Therefore my answer was as it was.

Cor

"Tom Leylan" <tl*****@nospam.netschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>Peter:

I think you'll find the use of the array has little to do with it. You
should use a collection class, it's just easier but the problem is that
you don't need to declare the parameter as ByRef. I just replied to
another message along the same lines. An array "is" a reference, you
pass it ByVal. When the value gets to the function it is a reference to
the array.

You _can_ pass it by reference but you open it up to be completely
swapped out. Create a new array in your function and assign that one to
the ByRef variable and you will see you destroyed the original one.

Picture what is happening in the computer when you pass variables to
functions. They are placed onto the "stack" if you had a 20,000 element
array would we imagine it would push 20,000 values onto the stack? It
always pushes a single value that acts as a pointer to where the array
is.

Don't worry about keywords like ByRef... conceptually languages always
have the option to pass values or references, it isn't something they
can eliminate in VS2005 or even VS2009.

Tom
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
Peter,

Get rid of that fixed array and use an arraylist, than you can pass
byval and your program becomes direct more efficient as well.

Cor

"Peter" <zl*****@sina.comschreef in bericht
news:OI*************@TK2MSFTNGP02.phx.gbl...
>I will excute the code below to get MethodName array. These codes are
>edited under VS2003. Now I want to upgrade it to VS2005,but VS2005
>gives me a varning message: The varible "sSourceMethodName" may throw a
>Null reference exception because it passes using "Byref" before being
>assigned.
>
Although it's just a warning I still want to let ift fit the VS2005's
recommended mode.
>
Any suggestions will be appreciated,
>
Peter
>
'------------------------------------------
Dim sSourceSampleName() As String
Dim Count as integer
Count = GetMethodName( sSourceMethodName)
>
Public Function GetMethodName( ByRef sMethodName() As String) As
Integer
Dim odr As OleDbDataReader
Dim i, j As Integer
>
j = GetMethodCount() 'Defined in other places
ReDim sMethodName(j - 1)
>
Dim oCmd As New OleDbCommand
>
'Connection ConnImport has been opened
With oCmd
.Connection = ConnImport
.CommandType = CommandType.StoredProcedure
.CommandText = "udpGetMethodName"
End With
>
odr = oCmd.ExecuteReader
Do While odr.Read()
sMethodName(i) = odr(0)
i += 1
Loop
>
If Not odr.IsClosed Then odr.Close()
Return j
>
End Function
'--------------------------------------------------------------


Jan 5 '07 #8
I read all your advice several times and understand I can use a object as a
parameter , pass it to a subroutine then change it and returne to the caller
no matter Byval or Byref.

I should separate GetMethodCount from GetMethodName.

I will create an arraylist object instead of an array.

Thanks for all of you, I think here is a warm family and I love this family,

Peter
Jan 6 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.