473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to pass by const reference?

Why does VB.NET V2 force me to pass by value for my set function? When I try
to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it). In
my case, would the byval for the set function cause a superfluous copy?
Thanks,
Siegfried

class person
dim m_name as string
public sub new (n as string)
m_name = n
end sub
public property name () as string
Get
return m_name
end get
set ( const byref value as string)
m_name = value
end set
end Property
public overloads overrides function ToString() as string
return m_name
end function
protected overloads overrides sub Finalize()
outp.WriteLine("~" & name )
MyBase.Finalize()
end sub
End Class
Jan 16 '08 #1
14 4789
"Siegfried Heintze" <si*******@heintze.comwrote in message
news:OH****************@TK2MSFTNGP06.phx.gbl...
Why does VB.NET V2 force me to pass by value for my set function? When I
try to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it).
In my case, would the byval for the set function cause a superfluous copy?
It doesn't create a second copy of the string, just the pointer to it. So
you have 2 pointers pointing to the 1 string. If you change the string from
1 pointer it will create a copy then I believe.

Michael
Jan 16 '08 #2
Why does VB.NET V2 force me to pass by value for my set function?
set ( const byref value as string)
I have never heard of the const keyword being used here.

Try skipping it.

--
Rory
Jan 16 '08 #3

"Michael C" <mi**@nospam.comwrote in message
news:u2*************@TK2MSFTNGP04.phx.gbl...
"Siegfried Heintze" <si*******@heintze.comwrote in message
news:OH****************@TK2MSFTNGP06.phx.gbl...
>Why does VB.NET V2 force me to pass by value for my set function? When I
try to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it).
In my case, would the byval for the set function cause a superfluous
copy?

It doesn't create a second copy of the string, just the pointer to it. So
you have 2 pointers pointing to the 1 string. If you change the string
from 1 pointer it will create a copy then I believe.
That is correct. By default all arguments in VB .NET are passes ByVal and
passing an argument ByVal means you get a copy - - but not necessarially a
copy of the data. If you pass a reference type (which a String is) ByVal,
you get a copy of the pointer to the reference type, thus having two ways to
access the one memory address. If you pass a value type (like an Integer)
ByValue, then you get a copy of the actual data stored on the stack.

If you were to pass a reference type (String) ByRef, then you'd get a
pointer to the original pointer, which points to the memory. A pointer to a
pointer is not as effiicient as a copy of the first pointer.
>
Michael

Jan 16 '08 #4
You can't do this.

Const is used to declare a constant and when declaring a constant, you must
assign it a value. Const can't be used to force an argument to become
constant.

-Scott

"Rory Becker" <Ro********@newsgroup.nospamwrote in message
news:b5**************************@msnews.microsoft .com...
>Why does VB.NET V2 force me to pass by value for my set function?
>set ( const byref value as string)

I have never heard of the const keyword being used here.

Try skipping it.

--
Rory


Jan 16 '08 #5
My understanding is that in .NET, all strings with the same contents will be
the same instance anyway.

So for example try the following code
Dim strA As String = "ABCDEF"
Dim strB As String = "ABCDEF"
MsgBox(String.ReferenceEquals(a, b))

It returns "True"!

Strings are now reference types, so whether you pass ByRef or ByVal, you are
passing (or copying) a pointer, not the contents of the string itself.

I think the reason is as old as the hills. Strings in BASIC have always been
variable length, and Microsoft has always tried to hide Pointers, memory
addresses etc from BASIC programmers. Being variable length, you need either
a terminator or a length field in memory so the computer knows where the end
of the string is. But in the BASIC paradigm, the "whole variable" only
consists of the characters, so the language must hide the terminator/length
field. The way VB does this is by making strings Reference types, then making
strings immutable and non-repeating. So if you do the comparison
If strA = strB Then ...
technically you are comparing REFERENCES (ie. that they point to the same
part of memory) rather than comparing the contents of the variables.
--
David Streeter
Synchrotech Software
Sydney Australia
"Siegfried Heintze" wrote:
Why does VB.NET V2 force me to pass by value for my set function? When I try
to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it). In
my case, would the byval for the set function cause a superfluous copy?
Thanks,
Siegfried

class person
dim m_name as string
public sub new (n as string)
m_name = n
end sub
public property name () as string
Get
return m_name
end get
set ( const byref value as string)
m_name = value
end set
end Property
public overloads overrides function ToString() as string
return m_name
end function
protected overloads overrides sub Finalize()
outp.WriteLine("~" & name )
MyBase.Finalize()
end sub
End Class
Jan 16 '08 #6
Addendum: In some ways, it is quite elegant, since comparisons will take the
same amount of time irrespective of the size of the strings being compared
(i.e. there is no character-by-character comparison). Also, additional copies
of the string take up an insignificant amount of memory.

Downside is that code like:
for i as integer = 0 to 100: strA &= "A": Next i
would be much slower, since you are creating a new string at each iteration
rather than extending an existing one. (I think there is a StringBuilder
class which is designed for this sort of work).

One major change from VB6 is that Strings are no longer collections of
Bytes, but collections of Characters. In other words, Strings are no longer a
good choice for storing binary data. Byte arrays are fairly good, but I get
sick of type CByte whenever I want to use a particular byte value as an
operand. Oh for a type character for byte!!

At least they got rid of BSTRs!!


--
David Streeter
Synchrotech Software
Sydney Australia
"SurturZ" wrote:
My understanding is that in .NET, all strings with the same contents will be
the same instance anyway.

So for example try the following code
Dim strA As String = "ABCDEF"
Dim strB As String = "ABCDEF"
MsgBox(String.ReferenceEquals(a, b))

It returns "True"!

Strings are now reference types, so whether you pass ByRef or ByVal, you are
passing (or copying) a pointer, not the contents of the string itself.

I think the reason is as old as the hills. Strings in BASIC have always been
variable length, and Microsoft has always tried to hide Pointers, memory
addresses etc from BASIC programmers. Being variable length, you need either
a terminator or a length field in memory so the computer knows where the end
of the string is. But in the BASIC paradigm, the "whole variable" only
consists of the characters, so the language must hide the terminator/length
field. The way VB does this is by making strings Reference types, then making
strings immutable and non-repeating. So if you do the comparison
If strA = strB Then ...
technically you are comparing REFERENCES (ie. that they point to the same
part of memory) rather than comparing the contents of the variables.
--
David Streeter
Synchrotech Software
Sydney Australia
"Siegfried Heintze" wrote:
Why does VB.NET V2 force me to pass by value for my set function? When I try
to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it). In
my case, would the byval for the set function cause a superfluous copy?
Thanks,
Siegfried

class person
dim m_name as string
public sub new (n as string)
m_name = n
end sub
public property name () as string
Get
return m_name
end get
set ( const byref value as string)
m_name = value
end set
end Property
public overloads overrides function ToString() as string
return m_name
end function
protected overloads overrides sub Finalize()
outp.WriteLine("~" & name )
MyBase.Finalize()
end sub
End Class

Jan 16 '08 #7

"SurturZ" <su*****@newsgroup.nospamwrote in message
news:0F**********************************@microsof t.com...
My understanding is that in .NET, all strings with the same contents will
be
the same instance anyway.

So for example try the following code
Dim strA As String = "ABCDEF"
Dim strB As String = "ABCDEF"
MsgBox(String.ReferenceEquals(a, b))

It returns "True"!
This is true. Before a new string is made on the heap, the "intern pool" is
checked to see if it already is in the pool. If so, then the same string
object is used for the new variable, if not, a new string object is created
in memory. While this increases performance, it doesn't change the basic
ByVal/ByRef point.
Strings are now reference types, so whether you pass ByRef or ByVal, you
are
passing (or copying) a pointer, not the contents of the string itself.
Yes, but in one case you are making a copy of a pointer that points directly
at the object on the heap and in the other you are making a pointer that
points to the first pointer (which is slightly less efficient).
>
I think the reason is as old as the hills. Strings in BASIC have always
been
variable length, and Microsoft has always tried to hide Pointers, memory
addresses etc from BASIC programmers. Being variable length, you need
either
a terminator or a length field in memory so the computer knows where the
end
of the string is. But in the BASIC paradigm, the "whole variable" only
consists of the characters, so the language must hide the
terminator/length
field. The way VB does this is by making strings Reference types, then
making
strings immutable and non-repeating. So if you do the comparison
If strA = strB Then ...
technically you are comparing REFERENCES (ie. that they point to the same
part of memory) rather than comparing the contents of the variables.
--
David Streeter
Synchrotech Software
Sydney Australia
"Siegfried Heintze" wrote:
>Why does VB.NET V2 force me to pass by value for my set function? When I
try
to change it to const byref it gives me a syntax error. It seems very
inefficient to be passing strings around by value when a reference to a
constant string object will do fine (all we are going to do is copy it).
In
my case, would the byval for the set function cause a superfluous copy?
Thanks,
Siegfried

class person
dim m_name as string
public sub new (n as string)
m_name = n
end sub
public property name () as string
Get
return m_name
end get
set ( const byref value as string)
m_name = value
end set
end Property
public overloads overrides function ToString() as string
return m_name
end function
protected overloads overrides sub Finalize()
outp.WriteLine("~" & name )
MyBase.Finalize()
end sub
End Class

Jan 16 '08 #8
Siegried,

It is always more efficient (in a matter of processing) to pass something by
value then by reference.
By Reference means nothing more than that the By value reference is placed
in a box.

However the behaviour of a type (by instance a string is immutable) can make
that you need sometimes to pass by reference.

Cor
Jan 16 '08 #9
Scott M. wrote:
"SurturZ" <su*****@newsgroup.nospamwrote in message
news:0F**********************************@microsof t.com...
>My understanding is that in .NET, all strings with the same contents
will be the same instance anyway.

So for example try the following code
Dim strA As String = "ABCDEF"
Dim strB As String = "ABCDEF"
MsgBox(String.ReferenceEquals(a, b))

It returns "True"!

This is true. Before a new string is made on the heap, the "intern
pool" is checked to see if it already is in the pool. If so, then
the same string object is used for the new variable, if not, a new
string object is created in memory.
Are you entirely sure about that? As I understand it, the COMPILER does that
at compilation time, but at run-time no such thing occurs.

For example:

\\\
Dim a As String
Dim b As String

a = "ABCDEF"
b = "ABC"
b &= "DEF"

MsgBox(String.ReferenceEquals(a, b))
///

Unlike SurturZ's example, this displays False. The compiler doesn't see the
string assignment to variable "b" as being the same as to "a" at compile
time, and so the two strings exist separately in memory despite having the
exact same content.

--

(O)enone
Jan 16 '08 #10
Now if you were to use String.Intern on b, you'd get back to a reference to
a. The intern table is used for constant strings, not dynamically created
strings. to use it for dynamically created strings, you need to do the
interning manually
"(O)enone" <oe****@nowhere.comwrote in message
news:fm**********@aioe.org...
Scott M. wrote:
>"SurturZ" <su*****@newsgroup.nospamwrote in message
news:0F**********************************@microso ft.com...
>>My understanding is that in .NET, all strings with the same contents
will be the same instance anyway.

So for example try the following code
Dim strA As String = "ABCDEF"
Dim strB As String = "ABCDEF"
MsgBox(String.ReferenceEquals(a, b))

It returns "True"!

This is true. Before a new string is made on the heap, the "intern
pool" is checked to see if it already is in the pool. If so, then
the same string object is used for the new variable, if not, a new
string object is created in memory.

Are you entirely sure about that? As I understand it, the COMPILER does
that at compilation time, but at run-time no such thing occurs.

For example:

\\\
Dim a As String
Dim b As String

a = "ABCDEF"
b = "ABC"
b &= "DEF"

MsgBox(String.ReferenceEquals(a, b))
///

Unlike SurturZ's example, this displays False. The compiler doesn't see
the string assignment to variable "b" as being the same as to "a" at
compile time, and so the two strings exist separately in memory despite
having the exact same content.

--

(O)enone
Jan 16 '08 #11
Oh, and I forgot to mention, technically the interning of constant strings
is not done by the compiler, rather it is done by the JIT/runtime. You can
turn off string interning of constants by ngen'ing the assembly when you
have a no interning attribute in the assembly.
<Assembly:
CompilationRelaxationsAttribute(CompilationRelaxat ions.NoStringInterning)>

So yes it is the compiler, but the JIT or ngen compiler not the vbc or csc
compilation ;)

"Bill McCarthy" <Bi**@NOSPAM.comwrote in message
news:4D**********************************@microsof t.com...
Now if you were to use String.Intern on b, you'd get back to a reference
to a. The intern table is used for constant strings, not dynamically
created strings. to use it for dynamically created strings, you need to do
the interning manually
"(O)enone" <oe****@nowhere.comwrote in message
news:fm**********@aioe.org...
>Scott M. wrote:
>>"SurturZ" <su*****@newsgroup.nospamwrote in message
news:0F**********************************@micros oft.com...
My understanding is that in .NET, all strings with the same contents
will be the same instance anyway.

So for example try the following code
Dim strA As String = "ABCDEF"
Dim strB As String = "ABCDEF"
MsgBox(String.ReferenceEquals(a, b))

It returns "True"!

This is true. Before a new string is made on the heap, the "intern
pool" is checked to see if it already is in the pool. If so, then
the same string object is used for the new variable, if not, a new
string object is created in memory.

Are you entirely sure about that? As I understand it, the COMPILER does
that at compilation time, but at run-time no such thing occurs.

For example:

\\\
Dim a As String
Dim b As String

a = "ABCDEF"
b = "ABC"
b &= "DEF"

MsgBox(String.ReferenceEquals(a, b))
///

Unlike SurturZ's example, this displays False. The compiler doesn't see
the string assignment to variable "b" as being the same as to "a" at
compile time, and so the two strings exist separately in memory despite
having the exact same content.

--

(O)enone
Jan 16 '08 #12
In article <0F**********************************@microsoft.co m>,
SurturZ <su*****@newsgroup.nospamwrote:
So if you do the comparison
If strA = strB Then ...
technically you are comparing REFERENCES (ie. that they point to the same
part of memory) rather than comparing the contents of the variables.
This is incorrect.

Microsoft.VisualBasic.CompilerServices.StringType. StrCmp is used for
the = < and operators.

--
J.B. Moreno
Jan 17 '08 #13

"J.B. Moreno" <pl***@newsreaders.comwrote in message
news:16**********************@newsreaders.com...
In article <0F**********************************@microsoft.co m>,
SurturZ <su*****@newsgroup.nospamwrote:
> So if you do the comparison
If strA = strB Then ...
technically you are comparing REFERENCES (ie. that they point to the same
part of memory) rather than comparing the contents of the variables.

This is incorrect.

Microsoft.VisualBasic.CompilerServices.StringType. StrCmp is used for
the = < and operators.
That's true. If you wan to compare the two variables to see if they point
to the same object in memor, use the "Is" operator.

>
--
J.B. Moreno

Jan 18 '08 #14
In article <##**************@TK2MSFTNGP05.phx.gbl>,
Scott M. <sm**@nospam.nospamwrote:
"J.B. Moreno" <pl***@newsreaders.comwrote in message
SurturZ <su*****@newsgroup.nospamwrote:
So if you do the comparison
If strA = strB Then ...
technically you are comparing REFERENCES (ie. that they point to the same
part of memory) rather than comparing the contents of the variables.
This is incorrect.

Microsoft.VisualBasic.CompilerServices.StringType. StrCmp is used for
the = < and operators.

That's true. If you wan to compare the two variables to see if they point
to the same object in memor, use the "Is" operator.

While that's true, it's less important than the fact that you can have
more than one instance of a string with the same value.
IF "1234".SubString(1,2) IS "1234".SubString(1,2) THEN
' never going to happen
END IF

SurturZ was confused by the fact that there will only be one instance
of a string CONSTANT in a namespace.

So that if you say

Dim str1 as String = "1234"
Dim str2 as String = "1234"

IF str1 IS str2 THEN
'this happens
END IF
But that's because what's really happening under the hood is:

DIM cStr1234 AS String = "1234"

Dim str1 as String = cStr1234
Dim str2 as String = cStr1234

IF str1 IS str2 THEN
'this happens
END IF

On a somewhat related note, if you use Option Compare Text and then
compare "a" to "A" the answer could be true for you and false for
someone else, without a single change in the program.

--
J.B. Moreno
Jan 20 '08 #15

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

Similar topics

4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
5
by: James Gregory | last post by:
#include <cstdlib> #include <cctype> #include <string> template <class Input_Iter> inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) { char tempArray = {0}; for (int i = 0;...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
10
by: Robert | last post by:
Hello all, I am a C programmer learning C++. And I am confused with function Pass by Pointer * or Pass by Reference &. Function pass by pointer like: void func(int * x) And function pass by...
6
by: LuB | last post by:
Hi, I wanted to use the most efficient argument passing method. I was always taught that its best to pass (const SomeObject& obj) if possible .... but in this case, I can't pass a const param...
8
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
3
by: Giff | last post by:
Hi, I am trying to change my way of programming (I am still learning) , in particular I am putting an effort in passing const ref to functions, when possible. When possible means (to me) when...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.