473,405 Members | 2,185 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,405 software developers and data experts.

ByRef Argument - Option Strict ON

Hello: I have a problem using ByRef arguments with Option Strict ON. I have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...To
accommodate all variable types I made the arguments in ChangeValue() of type
object...I then check the typecode and do the correct comparison etc...

With Option Strict ON I have to cast the arguments to the generic object
type in the call to ChangeValue()

The problem is that the value of a ByRef argument does not change even
though ChangeValue modifies the argument??? Does CObj(lsValue) actually
return a new object reference?

What's happening here???

Any insight will be greatly appreciated!!!

- Shannon

'*********************
'* Example Code:
'*********************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ls_Value As String = "1"

Dim lo_Object As Object = "1"

Console.WriteLine(ls_Value)

ChangeValue(CObj(ls_Value), "2")

Console.WriteLine(ls_Value)

Console.WriteLine("***")

Console.WriteLine(lo_Object)

ChangeValue(lo_Object, "2")

Console.WriteLine(lo_Object)

End Sub

Private Sub ChangeValue(ByRef ao_Value As Object, ByVal ao_NewValue As
Object)

Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)

Select Case le_TypeCode

Case TypeCode.String

If (CStr(ao_Value) <> CStr(ao_NewValue)) Then

ao_Value = ao_NewValue

End If

End Select

End Sub
Nov 21 '05 #1
13 2479

"Shannon Richards" <sr**********@hotmail.com> wrote
Hello: I have a problem using ByRef arguments with Option Strict ON. I have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...
That is a bit odd, why would you need a routine to do that? Wherever you
call that routine, you could simply do the assignment, instead....
ChangeValue(CObj(ls_Value), "2")
ls_Value = "2"

You know what to change it to, why bother calling out to a routine?

To accommodate all variable types I made the arguments in ChangeValue() of type
object...I then check the typecode and do the correct comparison etc...


If it weren't already possible to do inline, this would be a case for method overloading:

Private Sub ChangeValue(ByVal Source As String, ByVal Value As String)
Source = Value
End Sub
Private Sub ChangeValue(ByVal Source As Integer, ByVal Value As Integer)
Source = Value
End Sub
Take note that I've removed the If <> test because it is not needed, if they are different
then Source is assigned the new value. If they are the same, then Source is assigned
an identical value, the result is the same. Likewise, the routine is not needed, you could
just do the assignment inline, rather than using a routine....

HTH
LFS
Nov 21 '05 #2
Thanks for your feedback...

This code is actually part of a project in which I'm using the "Unit of
Work" pattern for managing changes to objects etc...The procedure does a lot
of additional processing which I have not shown to keep things simple...Each
time a property in an object is changed this routine is called and if the
new value actually differs from the original many additional steps take
place...example: registering the object as dirty in my work
controller...notifying a client of changes via events...etc

Right now I have reverted to an overloaded method scenario where there are
overloaded methods for handling all datatypes...string, integer, date,
boolean etc etc etc...

I'm just curious why I'm experiencing the behavior I'm experiencing...using
Object arguments and ByRef with Option Strict ON...

Why does this work with VB.Net implicit datatype casting (Option Strict OFF)
and behave differently with explicit datatype casting (Option Strict ON).
What does VB do under the hood that's different that explicitly casting to
type Object?

Thanks
- Shannon
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...

"Shannon Richards" <sr**********@hotmail.com> wrote
Hello: I have a problem using ByRef arguments with Option Strict ON. I
have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...


That is a bit odd, why would you need a routine to do that? Wherever you
call that routine, you could simply do the assignment, instead....
ChangeValue(CObj(ls_Value), "2")


ls_Value = "2"

You know what to change it to, why bother calling out to a routine?

To accommodate all variable types I made the arguments in ChangeValue()
of type
object...I then check the typecode and do the correct comparison etc...


If it weren't already possible to do inline, this would be a case for
method overloading:

Private Sub ChangeValue(ByVal Source As String, ByVal Value As String)
Source = Value
End Sub
Private Sub ChangeValue(ByVal Source As Integer, ByVal Value As
Integer)
Source = Value
End Sub
Take note that I've removed the If <> test because it is not needed, if
they are different
then Source is assigned the new value. If they are the same, then Source
is assigned
an identical value, the result is the same. Likewise, the routine is not
needed, you could
just do the assignment inline, rather than using a routine....

HTH
LFS

Nov 21 '05 #3
Shannon,
You need to overload ChangeValue for each object type that it supports.
Private Sub ChangeValue(ByRef ao_Value As String, ByVal ao_NewValue As
String) ... possible common logic ... If (ao_Value <> ao_NewValue) Then
ao_Value = ao_NewValue
End If ... possible common logic ... End Sub Private Sub ChangeValue(ByRef ao_Value As Integer, ByVal ao_NewValue As
Integer) ... possible common logic ... If (ao_Value <> ao_NewValue) Then
ao_Value = ao_NewValue
End If ... possible common logic ... End Sub
Within each ChangeValue, if there is common logic (other then the
comparison) I would have each specific ChangeValue call a generalized
ChangeValue.

The reason that ByRef Object will not work with Option Strict On is that the
routine requires that you pass it an object variable, you are attempting to
pass it a more specific variable. If you pass it an Integer variable and the
routine attempts to assign a String to the parameter you get a type mismatch
as a String is not assignment compatible with an Integer, a conversion needs
to occur, with Option Strict On this conversion needs to be explicit. Ergo
the error you are seeing...

Hope this helps
Jay
"Shannon Richards" <sr**********@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl... Hello: I have a problem using ByRef arguments with Option Strict ON. I
have built a generic sub procedure "ChangeValue()" to change the value of
an argument if the new value is not the same as the original value...To
accommodate all variable types I made the arguments in ChangeValue() of
type object...I then check the typecode and do the correct comparison
etc...

With Option Strict ON I have to cast the arguments to the generic object
type in the call to ChangeValue()

The problem is that the value of a ByRef argument does not change even
though ChangeValue modifies the argument??? Does CObj(lsValue) actually
return a new object reference?

What's happening here???

Any insight will be greatly appreciated!!!

- Shannon

'*********************
'* Example Code:
'*********************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ls_Value As String = "1"

Dim lo_Object As Object = "1"

Console.WriteLine(ls_Value)

ChangeValue(CObj(ls_Value), "2")

Console.WriteLine(ls_Value)

Console.WriteLine("***")

Console.WriteLine(lo_Object)

ChangeValue(lo_Object, "2")

Console.WriteLine(lo_Object)

End Sub

Private Sub ChangeValue(ByRef ao_Value As Object, ByVal ao_NewValue As
Object)

Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)

Select Case le_TypeCode

Case TypeCode.String

If (CStr(ao_Value) <> CStr(ao_NewValue)) Then

ao_Value = ao_NewValue

End If

End Select

End Sub

Nov 21 '05 #4
Shannon,

Not adding so much to the others, however Option Strict On prevents that the
program will search in runtime to the appropriate type. (Better it demands
you set it in design time).

Exactly as you doing what I saw in a glimp. (The others have answers you so
in detail that I did not really look at the problem, just the part of the
Option Strict question of you).

(I have even not checked why Jay did not point you on the ref by Value
instead by Ref in your question what he (and I) normally do, so there will
be a reason for that).

However I hope that my first sentence helps?

Cor
Nov 21 '05 #5
"Shannon Richards" <sr**********@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl...
Hello: I have a problem using ByRef arguments with Option Strict ON.
I have built a generic sub procedure "ChangeValue()" to change the
value of an argument if the new value is not the same as the original
value...To accommodate all variable types I made the arguments in
ChangeValue() of type object...I then check the typecode and do the
correct comparison etc...


Better to have a ChangeValue method on each class and overload it
for each Type. I think that's what's called PolyMorphism - same
method, does something different.

I fell over the same problem a while ago and it took me a while to figure
out what was going on. The problem is the ByRef bit.
Passing the value /in/ (going from a Specific-Type to a more generic one
e.g. Object) VB is perfectly happy to do - it's a Widening conversion -
but when you try to "pass" the value back the other way, (from Object
to Specific-Type), VB get's all upset because it doesn't know how to do
that. Hence your problem.

HTH,
Phill W.
Nov 21 '05 #6

"Shannon Richards" <sr**********@hotmail.com> wrote
Why does this work with VB.Net implicit datatype casting (Option Strict OFF)
and behave differently with explicit datatype casting (Option Strict ON).
It was designed to behave differently, based on the Option Strict setting.
That is why it behaves differently. You get no late binding with Strict ON.

What does VB do under the hood that's different that explicitly casting to
type Object?


Setting that option disables late binding. Turning Strict off allows late binding,
(plus others) that is what the design was. Since you are attempting to use late
binding, you will find that option has significant impact on your code....

http://msdn.microsoft.com/library/de...tionstrict.asp
http://msdn.microsoft.com/library/de...bspec5_1_2.asp

See if reading the specs help....
LFS
Nov 21 '05 #7
Cor,
(I have even not checked why Jay did not point you on the ref by Value
instead by Ref in your question what he (and I) normally do, so there will
be a reason for that). She wants to set the caller's variable, she needs ByRef, why would I offer
her something different? In stead of ByRef, she could have used a Function &
ByVal, however the calling syntax then gets more "elaborate".

Personally I don't see why you would need to "check" why I did or did not do
something! Unless of course you are reviewing it for your own edification.

Just a thought
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Shannon,

Not adding so much to the others, however Option Strict On prevents that
the program will search in runtime to the appropriate type. (Better it
demands you set it in design time).

Exactly as you doing what I saw in a glimp. (The others have answers you
so in detail that I did not really look at the problem, just the part of
the Option Strict question of you).

(I have even not checked why Jay did not point you on the ref by Value
instead by Ref in your question what he (and I) normally do, so there will
be a reason for that).

However I hope that my first sentence helps?

Cor

Nov 21 '05 #8
>>>(I have even not checked why Jay did not point you on the ref by Value
instead by Ref in your question what he (and I) normally do, so there
will be a reason for that).
Personally I don't see why you would need to "check" why I did or did not
do something! Unless of course you are reviewing it for your own
edification.


I am free to "check" things I hope when it real interest me and as a lot of
things can something throw an event, and that you did not tell about "the
value by ref" did that? However, I did not found it interesting enough. I
have the idea that I know now enough from that and found it because of the
more general part of the subject not interesting enough to see why you did
not do that.

When I really "check" it than that it is not for yours, however for my own
education as you said or when I really think direct in first sight, that you
are wrong. In that case, I would write it in a very different way, trying to
do it in a way that you can correct your answer. Alternatively, as it can
have more aspects trying to do it in a way that we can discuss it.

I assume you was meaning education because I cannot imagine that I have to
do an edification (Or is should have a meaning I do not know). However I do
not here play a game who knows it the best. I wrote a short while ago that I
respect your opinion, you answered on that with "Do we" I am not a "we",
that is here only the Queen, when you mean if you do respect me, than that
is not up to me.

That is all,

Cor


Nov 21 '05 #9

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uy*************@TK2MSFTNGP12.phx.gbl...
(I have even not checked why Jay did not point you on the ref by Value
instead by Ref in your question what he (and I) normally do, so there
will be a reason for that).

Personally I don't see why you would need to "check" why I did or did not
do something! Unless of course you are reviewing it for your own
edification.


I am free to "check" things I hope when it real interest me and as a lot
of things can something throw an event, and that you did not tell about
"the value by ref" did that? However, I did not found it interesting
enough. I have the idea that I know now enough from that and found it
because of the more general part of the subject not interesting enough to
see why you did not do that.

When I really "check" it than that it is not for yours, however for my own
education as you said or when I really think direct in first sight, that
you are wrong. In that case, I would write it in a very different way,
trying to do it in a way that you can correct your answer. Alternatively,
as it can have more aspects trying to do it in a way that we can discuss
it.

I assume you was meaning education because I cannot imagine that I have to
do an edification (Or is should have a meaning I do not know). However I
do not here play a game who knows it the best. I wrote a short while ago
that I respect your opinion, you answered on that with "Do we" I am not a
"we", that is here only the Queen, when you mean if you do respect me,
than that is not up to me.

That is all,

Cor

Nov 21 '05 #10
Cor,
Generally when I hear "checked" it implies (to me at least) that you are
checking ones homework, grading his paper, making sure I am doing it
right...

I am all for people reviewing others work for their own edification. In fact
I would all but insist as that is how IMHO we truly learn.

Edification:

http://encarta.msn.com/dictionary_18...ification.html

<definition>
enlightenment: instruction or enlightenment, especially when it is
morally or spiritually uplifting
</definition>

So I am all for others reviewing/recreating my work (or your work, or
Herfried's work, or Jon Skeet's work, or especially Microsoft's work) for
their own edification or enlightenment. Enlightenment as in that quest for
the "Proverbial Aha!" to become better programmers...

Don't get me wrong, If I make a mistake or have not explained something
well, I want others to let me know! Which is part of the reason you find me
correcting my own mistakes...

Just a thought
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uy*************@TK2MSFTNGP12.phx.gbl...
(I have even not checked why Jay did not point you on the ref by Value
instead by Ref in your question what he (and I) normally do, so there
will be a reason for that).

Personally I don't see why you would need to "check" why I did or did not
do something! Unless of course you are reviewing it for your own
edification.


I am free to "check" things I hope when it real interest me and as a lot
of things can something throw an event, and that you did not tell about
"the value by ref" did that? However, I did not found it interesting
enough. I have the idea that I know now enough from that and found it
because of the more general part of the subject not interesting enough to
see why you did not do that.

When I really "check" it than that it is not for yours, however for my own
education as you said or when I really think direct in first sight, that
you are wrong. In that case, I would write it in a very different way,
trying to do it in a way that you can correct your answer. Alternatively,
as it can have more aspects trying to do it in a way that we can discuss
it.

I assume you was meaning education because I cannot imagine that I have to
do an edification (Or is should have a meaning I do not know). However I
do not here play a game who knows it the best. I wrote a short while ago
that I respect your opinion, you answered on that with "Do we" I am not a
"we", that is here only the Queen, when you mean if you do respect me,
than that is not up to me.

That is all,

Cor

Nov 21 '05 #11
Jay,

The "2" in this link is as in my English Dutch dictonary the word
"edification" is, a word I had never seen before.

http://www2.merriam-webster.com/cgi-bin/mwdictsn

Trying to make it (I don't know if it interest you or you knew already) a
little bit clear for you what happens. English is a language, which has
because it has from 1066 a lot of French (Normandic) words and from before
that words used in more dialects/languages around the North See.

While there are a lot of equivalents. I have noticed that people who talk
native "Latin" European languages take often the "French" equivalent of an
English word, while people from my region "North Sea" take those "North Sea"
words. Although I think I never would do that in this context, would I when
I did, use "enlighten" = in Dutch "verlichten".

The best translation for the Dutch word "checken" is probably examine
however especially not in the context from a teacher, where we use explicit
"examineren" or "controleren". I hope you don't mind that I don't "check"
every word if it has even more meanings (while I know check has a lot in
English) than I think there are and surely don't know what they all mean in
the different dialects. However, I will next time use examine and hope you
will than not take the not meant meaning of the word.

For the rest nothing than you wrote.

Cor
Nov 21 '05 #12
It seems that the link does not work.

Main Entry: ed·i·fy
Pronunciation: 'e-d&-"fI
Function: transitive verb
Inflected Form(s): -fied; -fy·ing
Etymology: Middle English, from Middle French edifier, from Late Latin &
Latin; Late Latin aedificare to instruct or improve spiritually, from Latin,
to erect a house, from aedes temple, house; akin to Old English Ad funeral
pyre, Latin aestas summer
Date: 14th century
1 archaic a : BUILD b : ESTABLISH
2 : to instruct and improve especially in moral and religious knowledge;
also : ENLIGHTEN, INFORM
Nov 21 '05 #13
Thank you all for the feedback...even thought this post took a strange
twist...

I researched data widening and now understand the basics of what is going
on...Upon mush reconsideration I think I like the tight integration of using
overloaded methods for each specific data type I expect to handle...

Thank you!
- Shannon
"Shannon Richards" <sr**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Thanks for your feedback...

This code is actually part of a project in which I'm using the "Unit of
Work" pattern for managing changes to objects etc...The procedure does a
lot of additional processing which I have not shown to keep things
simple...Each time a property in an object is changed this routine is
called and if the new value actually differs from the original many
additional steps take place...example: registering the object as dirty in
my work controller...notifying a client of changes via events...etc

Right now I have reverted to an overloaded method scenario where there are
overloaded methods for handling all datatypes...string, integer, date,
boolean etc etc etc...

I'm just curious why I'm experiencing the behavior I'm
experiencing...using Object arguments and ByRef with Option Strict ON...

Why does this work with VB.Net implicit datatype casting (Option Strict
OFF) and behave differently with explicit datatype casting (Option Strict
ON). What does VB do under the hood that's different that explicitly
casting to type Object?

Thanks
- Shannon
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...

"Shannon Richards" <sr**********@hotmail.com> wrote
Hello: I have a problem using ByRef arguments with Option Strict ON. I
have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...


That is a bit odd, why would you need a routine to do that? Wherever you
call that routine, you could simply do the assignment, instead....
ChangeValue(CObj(ls_Value), "2")


ls_Value = "2"

You know what to change it to, why bother calling out to a routine?

To accommodate all variable types I made the arguments in ChangeValue()
of type
object...I then check the typecode and do the correct comparison etc...


If it weren't already possible to do inline, this would be a case for
method overloading:

Private Sub ChangeValue(ByVal Source As String, ByVal Value As String)
Source = Value
End Sub
Private Sub ChangeValue(ByVal Source As Integer, ByVal Value As
Integer)
Source = Value
End Sub
Take note that I've removed the If <> test because it is not needed, if
they are different
then Source is assigned the new value. If they are the same, then Source
is assigned
an identical value, the result is the same. Likewise, the routine is not
needed, you could
just do the assignment inline, rather than using a routine....

HTH
LFS


Nov 21 '05 #14

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
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
3
by: Earthlink | last post by:
This is best explained by looking at the comments in the sample code below. Is this a VB.NET bug? Option Strict On Public Class Class1 End Class Public Class Class2 : Inherits Class1
0
by: VaBa | last post by:
Hi, Need some help.. I have a VBA class module in MS ACCESS. In the same MS ACCESS app, I am calling a .NET DLL (which i have exposed as COM-visible) and passing BYREF an instance of the VBA class...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.