|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
optional args to a class.Init methodgiven 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 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
Show quote
Hide quote
"Ivar" <Ivar.ekstromer***@ntlworld.com> wrote in message Hi Ivar,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 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
Show quote
Hide quote
"MP" <NoSpam@Thanks.com> wrote in message The only way to tell that nothing was passed is to make it a Variant and use 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 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.
Show quote
Hide quote
"Bob Butler" <noway@nospam.ever> wrote in message that's all i could come up with toonews: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. > The question is why would you care? I'm not sure I can see any time it just another of my dumb ideas :-)> would matter if the caller specified the default value or just let VB fill > it in. 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 "MP" <NoSpam@Thanks.com> wrote in message <cut>news:uQKrr%23wYJHA.3548@TK2MSFTNGP05.phx.gbl... > just another of my dumb ideas :-) That's not dumb, but I think you're confusing the value with the command > 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 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 "Bob Butler" <noway@nospam.ever> wrote in message I understand that that approach would work...but in my thinking i wanted to 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: > 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 "MP" <NoSpam@Thanks.com> wrote... VB also has a tristate enum built in that you could use, VbTriState> > 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 Values are, vbFalse = 0, vbTrue = -1 and vbUseDefault = -2 Private Sub Foo(value As VbTriState) debug.print value End Sub David
Show quote
Hide quote
"David Youngblood" <d**@flash.net> wrote in message Perfect! looks like just the thing.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 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
Show quote
Hide quote
"MP" <NoSpam@Thanks.com> wrote in message If you put the public enum in the class then you provide that to the callernews: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 "MP" <NoSpam@Thanks.com> wrote It could use a little work ;-)> 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? 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 "MP" <NoSpam@Thanks.com> wrote in message Sub main()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 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
Show quote
Hide quote
"Bob Butler" <noway@nospam.ever> wrote in message Hi Bob,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 > 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
Show quote
Hide quote
"MP" <NoSpam@Thanks.com> wrote in <SNIP> 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 > > 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. "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 Hi Dan,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. 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 offeredmark
Anyway to move Image control at runtime
Dim WithEvents * As HTMLDocument with an Iframe - access denied!? Recommended IDE for the PocketPC Automatic determine .Rows in MSHFLexgrid convert Unicode string Use ColumnClick and Click Event in MSHFLexGrid Ultima de aesthetica vb. user control asp How do you find ClassTypes Info..? |
|||||||||||||||||||||||