-
Mar 20th, 2023, 01:58 PM
#1
[RESOLVED] Hot to "trick" the bang specifier and square brackets
This is one that doesn't come to mind immediately.
I'm making a little utility code that's basically peeled out of a much larger application. I'm taking entire modules out of the larger application, and I'd prefer to not change them.
And those modules will have large chunks of unused code, but that's all fine.
Here's my problem. That larger application makes extensive use of the DAO and uses MS-Access databases. However, this little utility won't do any of that.
Ok, the problem. I've got this piece of code:
Code:
s = PatientData![Gender]
I need to build a dummy function that returns "not specified", but I don't want to change that above line to do it.
I'd also prefer NOT to create a reference to the DAO (or ADO). In fact, I want to keep references to a bare minimum.
Any ideas?
ADDED: I wouldn't mind creating a dummy class, if that's what it takes to get it done. If that's necessary, I'll just auto-instantiate it.
Last edited by Elroy; Mar 20th, 2023 at 02:01 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Mar 20th, 2023, 02:08 PM
#2
Re: Hot to "trick" the bang specifier and square brackets
Hmmm, ok, I can create a class that has the following:
Code:
Option Explicit
Public Property Get test() As String
test = "unknown"
End Property
And then do this:
Code:
Option Explicit
Dim o As New Class1
Private Sub Form_Load()
Debug.Print o.[test]
End Sub
But I can't use the bang symbol. Grrr. I get the following runtime error when using the bang:
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Mar 20th, 2023, 02:13 PM
#3
Re: Hot to "trick" the bang specifier and square brackets
Yayyy, I figured it out:
Code:
Option Explicit
Dim PatientData As New Collection
Private Sub Form_Load()
PatientData.Add "Unknown", "Gender"
Debug.Print PatientData![Gender]
End Sub
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Mar 20th, 2023, 10:57 PM
#4
Re: [RESOLVED] Hot to "trick" the bang specifier and square brackets
No need to spend and clutter up a Collection at run time, this is pretty basic stuff unless I have misinterpreted the question:
Startup module Module1.bas:
Code:
Option Explicit
Private Sub Main()
Dim PatientData As PatientData
Set PatientData = New PatientData
PatientData![Gender] = "male"
Debug.Print PatientData![Gender]
'Same thing, different syntax:
Debug.Print PatientData("Gender")
'Same thing, different syntax, Integer Index:
Debug.Print PatientData(3)
End Sub
PatientData.cls:
Code:
Option Explicit
'Accept Index to Default property using bang and brackets syntax. This is really just
'a Default property that accepts a property-array index String-subtype Variant value.
'
'Blackhole inputs, always output "not specified" result.
'DispID = DISPID_VALUE:
'
'Assigned via Tools|Procedure Attributes dialog ("Procedure ID" of (Default) there).
Public Property Get Default(ByVal Index As Variant) As String
Default = "not specified"
End Property
Public Property Let Default(ByVal Index As Variant, ByVal RHS As String)
'Discard RHS of assignment, this is a dummy property.
End Property
What did I miss?
-
Mar 21st, 2023, 08:58 AM
#5
Re: [RESOLVED] Hot to "trick" the bang specifier and square brackets
Hmmm, Dil, I tried a class, but I didn't set it up the way you did. I've sort of moved on, but that's good to know. I knew the bang separator could be used in several contexts, but I tend to limit my use of it for referencing DB fields. But thanks.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
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
|