By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

ByRef Argument - Option Strict ON

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
>>>(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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.