Am straining to determine where var is being reassigned/reset ???

Am straining to determine where var is being reassigned/reset ???

am 23.10.2007 16:49:22 von MLH

In the snippet below are lines 1 - 3. The snippet is taken from
FN SetSaleTimeNdate2 in AppSpecific global module. Somehow,
the value of PubDays is zero upon return from the Bits2Weekdays
FN in line #3. I do not understand why the value is changing???

1 PubDays = DLookup("[NPPubDay]", "tblNewsPaper",
"[NewsPprName]=GetCurrentNewsPaper()") 'a value 1 to 127
representing the weekdays on which the related paper publishes
Debug.Print "PubDays: "; PubDays, "<==========="
2 SDS.GV_PubDays = PubDays
Debug.Print "PubDays: "; PubDays, "<==========="
3 SDS.GV_txtPubDays = Bits2Weekdays(PubDays)
Debug.Print "PubDays: "; PubDays, "<==========="

PubDays is locally declared in FN SetSaleTimeNdate2 this way...
Dim PubDays As Integer

The debugging statement after line #3 proves the value is changed.

Not that it should matter, but the functional code in Bits2Weekdays
function is ...
20 For i = vbSaturday To vbSunday Step -1
30 If MyBitValue >= 2 ^ (i - 1) Then
40 PString = Chr$(44) & Format$(i, "dddd") & PString
50 MyBitValue = MyBitValue Mod 2 ^ (i - 1)
60 End If
70 Next i
80 PString = right$(PString, Len(PString) - 1) ' get rid of the extra
comma
90 Bits2Weekdays = PString

Re: Am straining to determine where var is being reassigned/reset ???

am 23.10.2007 20:41:34 von OldPro

On Oct 23, 9:49 am, MLH wrote:
> In the snippet below are lines 1 - 3. The snippet is taken from
> FN SetSaleTimeNdate2 in AppSpecific global module. Somehow,
> the value of PubDays is zero upon return from the Bits2Weekdays
> FN in line #3. I do not understand why the value is changing???
>
> 1 PubDays = DLookup("[NPPubDay]", "tblNewsPaper",
> "[NewsPprName]=GetCurrentNewsPaper()") 'a value 1 to 127
> representing the weekdays on which the related paper publishes
> Debug.Print "PubDays: "; PubDays, "<==========="
> 2 SDS.GV_PubDays = PubDays
> Debug.Print "PubDays: "; PubDays, "<==========="
> 3 SDS.GV_txtPubDays = Bits2Weekdays(PubDays)
> Debug.Print "PubDays: "; PubDays, "<==========="
>
> PubDays is locally declared in FN SetSaleTimeNdate2 this way...
> Dim PubDays As Integer
>
> The debugging statement after line #3 proves the value is changed.
>
> Not that it should matter, but the functional code in Bits2Weekdays
> function is ...
> 20 For i = vbSaturday To vbSunday Step -1
> 30 If MyBitValue >= 2 ^ (i - 1) Then
> 40 PString = Chr$(44) & Format$(i, "dddd") & PString
> 50 MyBitValue = MyBitValue Mod 2 ^ (i - 1)
> 60 End If
> 70 Next i
> 80 PString = right$(PString, Len(PString) - 1) ' get rid of the extra
> comma
> 90 Bits2Weekdays = PString

If you are passing PubDays as a variable to a sub or function, and not
using the ByVal keyword, then the value of PubDays can be changed by
the sub or function. ByRef is the default in VB.

Re: Am straining to determine where var is being reassigned/reset ???

am 24.10.2007 21:19:24 von MLH

Is it possible that I might have reset some global option modifying
default VB setting from ByRef to ByVal? This is the first time I've
ever noticed this behavior. I will trying using ByVal keyword now
to see if it prevents the value from PubDays from being changed.



>
>If you are passing PubDays as a variable to a sub or function, and not
>using the ByVal keyword, then the value of PubDays can be changed by
>the sub or function. ByRef is the default in VB.

Re: Am straining to determine where var is being reassigned/reset ???

am 24.10.2007 22:29:36 von MLH

Addingthe ByVal Keyword in the first line DID the trick. I can't
believe I haven't encountered this issue before. Why do you
suppose that is? Seems as though I would have experienced
problems before now.

Function Bits2Weekdays(ByVal MyBitValue As Integer) As String
On Error GoTo ErrorBits2Weekdays
Dim PString As String, i As Integer, ThisFN As String
ThisFN = "Bits2Weekdays"

10 If MyBitValue < 1 Or MyBitValue > 127 Then Exit Function
20 For i = vbSaturday To vbSunday Step -1
30 If MyBitValue >= 2 ^ (i - 1) Then
40 PString = Chr$(44) & Format$(i, "dddd") & PString
50 MyBitValue = MyBitValue Mod 2 ^ (i - 1)
60 End If
70 Next i
80 PString = right$(PString, Len(PString) - 1) ' get rid of the extra
comma
90 Bits2Weekdays = PString

ExitBits2Weekdays:
Exit Function

ErrorBits2Weekdays:
Dim r As String, k As String, Message3 As String
r = "The following unexpected error occurred in Function
Bits2Weekdays, line #" & Trim$(CStr(Erl)) & ", CBF on " & ThisFN & "."
k = CRLF & CRLF & str$(Err) & ": " & Quote & Error$ & Quote
Message3 = r & k
MsgBox Message3, vbExclamation, "Unexpected Error - " & MyApp$ &
", rev. " & MY_VERSION$
Resume ExitBits2Weekdays

End Function

Re: Am straining to determine where var is being reassigned/reset ???

am 25.10.2007 00:12:54 von OldPro

On Oct 24, 3:29 pm, MLH wrote:
> Addingthe ByVal Keyword in the first line DID the trick. I can't
> believe I haven't encountered this issue before. Why do you
> suppose that is? Seems as though I would have experienced
> problems before now.
>
> Function Bits2Weekdays(ByVal MyBitValue As Integer) As String
> On Error GoTo ErrorBits2Weekdays
> Dim PString As String, i As Integer, ThisFN As String
> ThisFN = "Bits2Weekdays"
>
> 10 If MyBitValue < 1 Or MyBitValue > 127 Then Exit Function
> 20 For i = vbSaturday To vbSunday Step -1
> 30 If MyBitValue >= 2 ^ (i - 1) Then
> 40 PString = Chr$(44) & Format$(i, "dddd") & PString
> 50 MyBitValue = MyBitValue Mod 2 ^ (i - 1)
> 60 End If
> 70 Next i
> 80 PString = right$(PString, Len(PString) - 1) ' get rid of the extra
> comma
> 90 Bits2Weekdays = PString
>
> ExitBits2Weekdays:
> Exit Function
>
> ErrorBits2Weekdays:
> Dim r As String, k As String, Message3 As String
> r = "The following unexpected error occurred in Function
> Bits2Weekdays, line #" & Trim$(CStr(Erl)) & ", CBF on " & ThisFN & "."
> k = CRLF & CRLF & str$(Err) & ": " & Quote & Error$ & Quote
> Message3 = r & k
> MsgBox Message3, vbExclamation, "Unexpected Error - " & MyApp$ &
> ", rev. " & MY_VERSION$
> Resume ExitBits2Weekdays
>
> End Function

I don't know of any way to turn the ByRef default off on a global
level. Personally, I think having ByRef as the default in a function
is counter intuitive, since a function is only supposed to return one
value. I like this behavior in a sub though, as it is very useful.

Re: Am straining to determine where var is being reassigned/reset ???

am 25.10.2007 07:51:16 von MLH

Likewise. Who are we to question the infinite wisdom of the ADT
on the subject? And I agree about the behavior in a sub. A sub
is a place for doing thnigs and the reason one creates a sub is
so the same things can be done again and again throughout
the program by sprinkling Calls to the sub all over the place.

I still haven't a clue as to what functional line in my Bits2Weekdays
FN modified the value of PubDays. Perhaps A97 HELP explains
somewhere how and when VB decides whether to modify the value
of variables passed to FN's by reference.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

>
>I don't know of any way to turn the ByRef default off on a global
>level. Personally, I think having ByRef as the default in a function
>is counter intuitive, since a function is only supposed to return one
>value. I like this behavior in a sub though, as it is very useful.

Re: Am straining to determine where var is being reassigned/reset ???

am 29.10.2007 14:30:10 von OldPro

> I still haven't a clue as to what functional line in my Bits2Weekdays
> FN modified the value of PubDays. Perhaps A97 HELP explains
> somewhere how and when VB decides whether to modify the value
> of variables passed to FN's by reference.

This is the line where it is modified:
MyBitValue = MyBitValue Mod 2 ^ (i - 1)