|
-
Jan 4th, 2025, 02:31 AM
#1
Thread Starter
PowerPoster
Passing Variable Names with Values to a paramarray
I'm starting to have a lot of Flags in my application and it's hard to keep track of the status of them all.
Sometimes I want to temporarily change a flag if it's a certain value and then restore the value.
The simple way is.
Code:
Private ASub As Sub
Dim flg As Boolean
flg = Flags(0)
Flags(0)=False
' do sub stuff
Flags(0) = flg
Of course the problem with that is when you have sixteen flags and your whole sub turns into turning flags on and off and five lines of code that's actually doing anything.
So what I tried was this which failed immediately:
(and now looking back at it I can see that I was just doing a stupid bugassert and not passing a variable name at all. I know better. probably...)
Code:
' Flags in whatever modules. I like to group them all into one.
Private fAllowFlickering As Boolean
Private fShowDieRolls As Boolean
' Both Properties below have an associated Property Let of course.
Public Property Get AllowFlickering As Boolean
AllowFlickering = fAllowFlickering
End Property
Public Property Get ShowDieRolls As Boolean
ShowDieRolls = fShowDieRolls
End Property
Private Sub SetRestoreFlags(ParamArray Variables())
Dim n As Long
For n = LBound(Variables) To UBound(Variables)
MsgBox Variables(n)
Next n
End Sub
Private Sub Form_Load()
SetRestoreFlags AllowFlickering = False, ShowDieRolls = True
End Sub
SetRestoreFlags would actually be a class module that was initiated with whatever I want the flags to be.
Any time a Flag status is sent to the Class, it would save the current value in a collection or array.
This would mean that the Flags sent must be Public or Friend in scope.
On Class Terminate it would restore all flags to whatever value they were.
Example case:
You can turn Definitions on or off (Definitions of words used in the app). They appear in an auxilliary window.
So In the DisplayDefinition sub, it would check this flag and kick out if disabled or display the definition if enabled.
But maybe there's a definition I want the user to see and I want to override the setting.
So what I'm trying to achieve is doing this without having to send every possible flag.
I mean I still have to type out all the flags but it's one line of code plus the class declaration.
But that's still better than however many lines of code at the beginning and end of any sub it applies to.
Not to mention, much easier debugging because it's all in one place.
And I want to send the flags in any quantity and in any order.
If it recognizes the flag-property variable then it knows what to do with it.
I know there are a lot of ways to do this and I've been doing all of them.
But I'm looking for something more automated so I can just send off a list of flags and the status I want them to be temporarily and when it's done the class goes out of scope and the sub exits with flags set as they were when the sub was entered.
Last edited by cafeenman; Jan 4th, 2025 at 03:46 AM.
-
Jan 4th, 2025, 02:47 AM
#2
Thread Starter
PowerPoster
Re: Passing Variable Names with Values to a paramarray
Here's an example of one thing I'm doing now.
The entire app is Random stuff happening. Think software version of a Rube Goldberg machine.
But it's controlled randomness.
Some things do not want to be disturbed.
In this case, "Disturbed" means text from one thing showing up in the middle of something else that was already going on and then the original thing continues.
Some subs don't care if they're disturbed.
But they're not allowed to disturb other subs.
Example:
Code:
Sub CanBeDisturbedButNotDisturbOthers
If DoNotDisturb Then Exit Sub ' Public property
' Do stuff.
End Sub
Sub CanNotBeDisturbedOrDisturbOthers
Dim m_DoNotDisturb as cDoNotDisturb
If DoNotDisturb Then Exit Sub
Set m_DoNotDisturb = New cDoNotDisturb ' On Class Initialize sets DoNotDisturb = True
' On Terminate, sets DoNotDisturb = False
' Do stuff.
' m_DoNotDisturb goes out of scope upon sub exit.
End Sub
-
Jan 4th, 2025, 02:58 AM
#3
Thread Starter
PowerPoster
Re: Passing Variable Names with Values to a paramarray
OK, I think I might have just figured it out.
And that is to make a Flag class and send that to a collection class instead of sending property names.
Not sure how complicated that will be or how long the calls will be because I just now thought of it and that's as far as I've gotten.
' some thinking passes....
OK, I think the way it needs to work is to still pass all the flags individually.
So all this really saves me is the resetting them at the end of the sub.
But the beginning of the sub is just as messy.
So what gets sent to the collection class is this.
Code:
Sub DoThing
dim m_FlagCollector as New cFlagCollector
m_FlagCollector.Add AllowFlickering, False ' It gets the current flag and temporary value you're setting.
m_FlagCollector.Add ShowDefinitions, True ' Then it saves the original value internally before setting the new value.
' Do stuff.
End Sub
Not sure that's really worth the effort but maybe.
Because I really want to turn this into one line of code.
This could work but I don't like it.
It would be cool if there were a way to send two paramarrays. But I can't.
I keep thinking a Pipe character | could have been used to tell the compiler where
a parameter array ends so that anything else could come after it including another paramarray.
Last edited by cafeenman; Jan 4th, 2025 at 03:43 AM.
-
Jan 4th, 2025, 03:56 AM
#4
Thread Starter
PowerPoster
Re: Passing Variable Names with Values to a paramarray
Actually, now that I think about it some more, this whole idea is really stupid.
Is there a way to delete this thread?
The problem now is that the Sub that creates the FlagCollector still needs to know what the status of all those flags is to know what to do.
One flag might want you to not do the sub if it's set.
Another might want the sub to enter text into a different textbox.
Another might be deciding what sound to play.
So regardless I still need to check all those status after the collector gets everything and I probably end up with even more lines of code.
sorry to bother you all.
This was a dumb idea.
But I still have the same problem that I don't know if I should even do anything about it.
It's working but getting more unwieldy as the app grows.
-
Jan 7th, 2025, 08:36 AM
#5
Re: Passing Variable Names with Values to a paramarray
So, you have 16 Flags...... True or False..... 1 or 0
Funny, how an Integer-Type also has 16 Bits, which can be 1 or 0......
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jan 7th, 2025, 08:48 AM
#6
Lively Member
Re: Passing Variable Names with Values to a paramarray
How about something like this?
Code:
Const FLAG_1 = &H1 ' 00000001
Const FLAG_2 = &H2 ' 00000010
Const FLAG_3 = &H4 ' 00000100
Const FLAG_4 = &H8 ' 00001000
Public Function SetFlag(ByVal value As Long, ByVal flag As Long) As Long
' Function to set a flag
SetFlag = value Or flag
End Function
Public Function ClearFlag(ByVal value As Long, ByVal flag As Long) As Long
' Function to clear a flag
ClearFlag = value And Not flag
End Function
Public Function IsFlagSet(ByVal value As Long, ByVal flag As Long) As Boolean
' Function to check if a flag is set
IsFlagSet = (value And flag) <> 0
End Function
Private Sub Form_Load()
Dim value As Long
value = 0
' Set some flags
value = SetFlag(value, FLAG_1)
value = SetFlag(value, FLAG_3)
' Check flags
Debug.Print "FLAG_1 is set: " & IsFlagSet(value, FLAG_1)
Debug.Print "FLAG_2 is set: " & IsFlagSet(value, FLAG_2)
Debug.Print "FLAG_3 is set: " & IsFlagSet(value, FLAG_3)
' Clear a flag
value = ClearFlag(value, FLAG_1)
Debug.Print "FLAG_1 is set after clearing: " & IsFlagSet(value, FLAG_1)
End Sub
-
Jan 7th, 2025, 09:55 AM
#7
New Member
Re: Passing Variable Names with Values to a paramarray
I didn't understand the essence of the problem, but this immediately came to my mind.
Code:
Private Sub TestParamArray()
Dim param1, sName$
param1 = 1: sName = "param1"
GetMem4 ByVal VarPtr(sName), ByVal VarPtr(param1) + 2
SomeSub param1
End Sub
Private Sub SomeSub(ParamArray params())
Dim i&, sTmp$
For i = 0 To UBound(params)
GetMem4 ByVal VarPtr(params(i)) + 2, ByVal VarPtr(sTmp)
Debug.Print sTmp & ": "; params(i)
Next
GetMem4 0&, ByVal VarPtr(sTmp)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|