Home All Groups Group Topic Archive Search About

optional args to a class.Init method

Author
20 Dec 2008 10:38 PM
MP
vb6

given a class with a  boolean member variable mbSomeBool
given a method with an optional parameter for that value

Public Function DoSomething (Optional bSomeBool  As Boolean)
'if bSomeBool is passed in explicitly change the member variable...else
leave it as it is...
mbSomeBool =  bSomeBool

the question is this...

with an optional boolean variable how do i tell if it's just missing or if
it's being changed to False explicitly
If bSomeBool = True Then ' I know the caller passed in a value
If Not bSomeBool = True Then ' I don't know if the caller passed in False or
just left it in it's default current mode

since it's not a variant i cant use IsMissing, since it's not a string I
cant use Len(s)= 0

if mbSomeBool = True' because it was used previously, but now a method is
called that resets it explicitly, i want to change it...but if the optional
parameter is not used, leave it as it is....

the only way i can think of is use a variant(so i can use IsMissing) and
treat it as if it's a boolean but that doesn't seem right

is that question clear?
mark

Author
20 Dec 2008 11:19 PM
Ivar
A little code snippet below 'might be what UR lookin for :-)

Ivar

Option Explicit
Private Enum BoolVal
ItsTrue = 1
ItsFalse = 2
End Enum

Private Function AFunction(Optional SomeVal As BoolVal)
If SomeVal = ItsTrue Then
MsgBox "Val Passed = True"
ElseIf SomeVal = ItsFalse Then
MsgBox "Val Passed = False"
Else:
MsgBox "Val Passed = Nuffin"
End If
AFunction = SomeVal
End Function

Private Sub Command1_Click()
Dim X
X = AFunction
X = AFunction(ItsFalse)
X = AFunction(ItsTrue)
End Sub
Author
21 Dec 2008 12:32 AM
MP
Show quote Hide quote
"Ivar" <Ivar.ekstromer***@ntlworld.com> wrote in message
news:h4f3l.34061$I05.2598@newsfe04.ams2...
>A little code snippet below 'might be what UR lookin for :-)
>
> Ivar

> Private Enum BoolVal
>
> Private Function AFunction(Optional SomeVal As BoolVal)
> If SomeVal = ItsTrue Then

> AFunction = SomeVal
> End Function


Hi Ivar,
thanks for the response
I guess I didn't make it clear it's a public method to be used by a client
that wants an optional boolean arg
I'll try that but off the top it seems like a client isn't going to "know'
the enum sinced it's private

Sub UseObject()
dim o as myObj
Set o = New myObj
o.init
call o.Somemethod()
or
call o.Somemethod(True)'type mismatch
or
call o.Somemethod(False)'type mismatch
End Sub
??? am I missing something

mark
Author
21 Dec 2008 12:42 AM
Bob Butler
Show quote Hide quote
"MP" <NoSpam@Thanks.com> wrote in message
news:uLFp2OvYJHA.1532@TK2MSFTNGP03.phx.gbl...
> vb6
>
> given a class with a  boolean member variable mbSomeBool
> given a method with an optional parameter for that value
>
> Public Function DoSomething (Optional bSomeBool  As Boolean)
> 'if bSomeBool is passed in explicitly change the member variable...else
> leave it as it is...
> mbSomeBool =  bSomeBool
>
> the question is this...
>
> with an optional boolean variable how do i tell if it's just missing or if
> it's being changed to False explicitly

The only way to tell that nothing was passed is to make it a Variant and use
the IsMissing function.  You'd then need to do further testing to make sure
what was passed was a boolean value.

The question is why would you care?  I'm not sure I can see any time it
would matter if the caller specified the default value or just let VB fill
it in.
Author
21 Dec 2008 1:58 AM
MP
Show quote Hide quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:urLkFUwYJHA.3676@TK2MSFTNGP02.phx.gbl...
>
> "MP" <NoSpam@Thanks.com> wrote in message
> news:uLFp2OvYJHA.1532@TK2MSFTNGP03.phx.gbl...
>> vb6
>>
>> given a class with a  boolean member variable mbSomeBool
>> given a method with an optional parameter for that value
>>
>> Public Function DoSomething (Optional bSomeBool  As Boolean)
>> 'if bSomeBool is passed in explicitly change the member variable...else
>> leave it as it is...
>> mbSomeBool =  bSomeBool
>>
>> the question is this...
>>
>> with an optional boolean variable how do i tell if it's just missing or
>> if it's being changed to False explicitly
>
> The only way to tell that nothing was passed is to make it a Variant and
> use the IsMissing function.  You'd then need to do further testing to make
> sure what was passed was a boolean value.

that's all i could come up with too

> The question is why would you care?  I'm not sure I can see any time it
> would matter if the caller specified the default value or just let VB fill
> it in.

just another of my dumb ideas :-)
the idea was that obj.init (,,,, bool,,,) could set the 'initial state'
then obj.doSomething(,,,,bool,,,) could either accept the current state(by
leaving blank) or override it by entering either t or f
so leaving it blank would mean something different than filling it in with
False
anyway I'll do it a different way
thanks to all who responded
mark
Author
21 Dec 2008 2:16 AM
Bob Butler
"MP" <NoSpam@Thanks.com> wrote in message
news:uQKrr%23wYJHA.3548@TK2MSFTNGP05.phx.gbl...
<cut>
> just another of my dumb ideas :-)
> the idea was that obj.init (,,,, bool,,,) could set the 'initial state'
> then obj.doSomething(,,,,bool,,,) could either accept the current state(by
> leaving blank) or override it by entering either t or f
> so leaving it blank would mean something different than filling it in with
> False

That's not dumb, but I think you're confusing the value with the command
from the caller to set the value.  The class has a boolean value but the
caller needs 3 options.  Something like Ivar suggested would work:

Public Enum eSetState
  SetFalse = 0
  SetTrue = 1
  NoChange = -1
End Enum

Public Function DoSomething(Optional ByVal StateAction As eSetState =
NoChange) As String
Select Case StateAction
  Case SetFalse: DoSomething = "Set False"
  Case SetTrue: DoSomething = "Set True"
  Case Else: DoSomething = "No Change"
End Select
End Function

Sub Main()
MsgBox DoSomething
MsgBox DoSomething(SetFalse)
MsgBox DoSomething(SetTrue)
End Sub
Author
21 Dec 2008 2:51 AM
MP
"Bob Butler" <noway@nospam.ever> wrote in message
news:uidc0IxYJHA.1268@TK2MSFTNGP04.phx.gbl...
>
> "MP" <NoSpam@Thanks.com> wrote in message
> news:uQKrr%23wYJHA.3548@TK2MSFTNGP05.phx.gbl...
> <cut>
>> just another of my dumb ideas :-)

> That's not dumb, but I think you're confusing the value with the command
> from the caller to set the value.  The class has a boolean value but the
> caller needs 3 options.  Something like Ivar suggested would work:
>

I understand that that approach would work...but in my thinking i wanted to
keep it encapsulated in the class
using the public enum introduces extra something in to the calling client as
opposed to just
call obj.something(false)
call obj.something

where True and False are natively built in and dont' add any baggage outside
the class
Author
21 Dec 2008 4:28 AM
David Youngblood
"MP" <NoSpam@Thanks.com> wrote...
>
> I understand that that approach would work...but in my thinking i wanted
> to keep it encapsulated in the class
> using the public enum introduces extra something in to the calling client
> as opposed to just
> call obj.something(false)
> call obj.something
>
> where True and False are natively built in and dont' add any baggage
> outside the class

VB also has a tristate enum built in that you could use, VbTriState
Values are, vbFalse = 0, vbTrue = -1 and vbUseDefault = -2

Private Sub Foo(value As VbTriState)
    debug.print value
End Sub

David
Author
21 Dec 2008 11:29 PM
MP
Show quote Hide quote
"David Youngblood" <d**@flash.net> wrote in message
news:eXojgSyYJHA.684@TK2MSFTNGP04.phx.gbl...
>
> "MP" <NoSpam@Thanks.com> wrote...
>>
>> I understand that that approach would work...but in my thinking i wanted
>> to keep it encapsulated in the class
>> using the public enum introduces extra something in to the calling client
>> as opposed to just
>> call obj.something(false)
>> call obj.something
>>
>> where True and False are natively built in and dont' add any baggage
>> outside the class
>
> VB also has a tristate enum built in that you could use, VbTriState
> Values are, vbFalse = 0, vbTrue = -1 and vbUseDefault = -2
>
> Private Sub Foo(value As VbTriState)
>    debug.print value
> End Sub
>
> David

Perfect! looks like just the thing.
Public Sub Foo(Optional value As VbTriState)
if value = vbtrue then
     mbvar = True
ElseIf value = vbFalse then
    mbVar = False
Else
    'leave it alone
End if
End Sub

Thanks
mark
Author
21 Dec 2008 2:22 PM
Bob Butler
Show quote Hide quote
"MP" <NoSpam@Thanks.com> wrote in message
news:ebLZLcxYJHA.2068@TK2MSFTNGP03.phx.gbl...
>
> "Bob Butler" <noway@nospam.ever> wrote in message
> news:uidc0IxYJHA.1268@TK2MSFTNGP04.phx.gbl...
>>
>> "MP" <NoSpam@Thanks.com> wrote in message
>> news:uQKrr%23wYJHA.3548@TK2MSFTNGP05.phx.gbl...
>> <cut>
>>> just another of my dumb ideas :-)
>
>> That's not dumb, but I think you're confusing the value with the command
>> from the caller to set the value.  The class has a boolean value but the
>> caller needs 3 options.  Something like Ivar suggested would work:
>>
>
> I understand that that approach would work...but in my thinking i wanted
> to keep it encapsulated in the class
> using the public enum introduces extra something in to the calling client
> as opposed to just
> call obj.something(false)
> call obj.something
>
> where True and False are natively built in and dont' add any baggage
> outside the class

If you put the public enum in the class then you provide that to the caller
Author
21 Dec 2008 1:07 AM
Larry Serflaten
"MP" <NoSpam@Thanks.com> wrote

> if mbSomeBool = True' because it was used previously, but now a method is
> called that resets it explicitly, i want to change it...but if the optional
> parameter is not used, leave it as it is....

> is that question clear?

It could use a little work  ;-)

Booleans have only two values, you don't get a third state.  If you need
a third state, you will need a different data type, or a different design.

eg, Make SomeBool a property of the class and the user can set it
the way they want prior to making the call to DoSomething....

LFS
Author
21 Dec 2008 1:45 AM
Bob Butler
"MP" <NoSpam@Thanks.com> wrote in message
news:uLFp2OvYJHA.1532@TK2MSFTNGP03.phx.gbl...
> vb6
>
> given a class with a  boolean member variable mbSomeBool
> given a method with an optional parameter for that value

Sub main()
MsgBox DoSomething
MsgBox DoSomething(False)
MsgBox DoSomething(True)
MsgBox DoSomething(42)
End Sub

Public Function DoSomething(Optional SomeBool As Variant) As String
If IsMissing(SomeBool) Then
  DoSomething = "No value passed"
ElseIf VarType(SomeBool) <> vbBoolean Then
  Err.Raise 13, "DoSomething", "SomeBool must be True or False if specified"
ElseIf SomeBool Then
  DoSomething = "True"
Else
  DoSomething = "False"
End If
End Function
Author
21 Dec 2008 2:09 AM
MP
Show quote Hide quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:enrJs4wYJHA.6000@TK2MSFTNGP05.phx.gbl...
>
> "MP" <NoSpam@Thanks.com> wrote in message
> news:uLFp2OvYJHA.1532@TK2MSFTNGP03.phx.gbl...
>> vb6
>>
>> given a class with a  boolean member variable mbSomeBool
>> given a method with an optional parameter for that value
>
> Sub main()
> MsgBox DoSomething
> MsgBox DoSomething(False)
> MsgBox DoSomething(True)
> MsgBox DoSomething(42)
> End Sub
>
> Public Function DoSomething(Optional SomeBool As Variant) As String
> If IsMissing(SomeBool) Then
>  DoSomething = "No value passed"
> ElseIf VarType(SomeBool) <> vbBoolean Then
>  Err.Raise 13, "DoSomething", "SomeBool must be True or False if
> specified"
> ElseIf SomeBool Then
>  DoSomething = "True"
> Else
>  DoSomething = "False"
> End If
> End Function
>

Hi Bob,
right, that's what i meant by the only way i could see was using a variant
just seems like a 'dirty' way to do it <g> so i'll rethink if I really need
to do it.
maybe i'm just too brainwashed that variants are 'bad'...(yeah i know
everything has it's place...<g>)

Thanks again,
mark
Author
21 Dec 2008 5:07 PM
DanS
Show quote Hide quote
"MP" <NoSpam@Thanks.com> wrote in
news:uLFp2OvYJHA.1532@TK2MSFTNGP03.phx.gbl:

> vb6
>
> given a class with a  boolean member variable mbSomeBool
> given a method with an optional parameter for that value
>
> Public Function DoSomething (Optional bSomeBool  As Boolean)
> 'if bSomeBool is passed in explicitly change the member
> variable...else leave it as it is...
> mbSomeBool =  bSomeBool
>
> the question is this...
>
> with an optional boolean variable how do i tell if it's just missing
> or if it's being changed to False explicitly
> If bSomeBool = True Then ' I know the caller passed in a value
> If Not bSomeBool = True Then ' I don't know if the caller passed in
> False or just left it in it's default current mode
>

<SNIP>

> is that question clear?

Not really but..........well.....yes.

Maybe you are not using 'Optional' properly ? (If there is a proper way.)

From what I read, it seems that this param is NOT 'Optional'.

You want to know if it is implicity set to false, or is just defaulted to
false because nothing was passed.

*As long as there's not a compatibility problem*, just make it NOT
optional. Problem solved.
Author
22 Dec 2008 1:10 AM
MP
"DanS" <t.h.i.s.n.t.h.a.t@r.o.a.d.r.u.n.n.e.r.c.o.m> wrote in message
news:Xns9B7B7B6773A40thisnthatroadrunnern@85.214.105.209...
> "MP" <NoSpam@Thanks.com> wrote in
> news:uLFp2OvYJHA.1532@TK2MSFTNGP03.phx.gbl:
>
>> vb6
>>

> *As long as there's not a compatibility problem*, just make it NOT
> optional. Problem solved.

Hi Dan,
Thanks for the response

that would solve *a* problem <g>
just not the stated problem (ie if you *did* want it optional...how to tell)
David youngblood shows one solution
:-)

but thanks for responding, I appreciate all points of view offered
mark