Using A Collection Class to Raise Common Event (1 Viewer)

I *thought* that would be the name of the button?
It could be the name of the button but a compiler is very specific.

In the alphacommand class - the thing that stores the actual command button -

Property get Name() as string
Name = CmdButton.name '(or whatever the command button variable is called)
end property

This "exposes" the name of the command button to the outside world.
 
No, do not waste your time. Get on with what you need to do.
None of this is sinking in. If I had to use it, I would have to persevere, but I am retired and do not.
 
@Gasman you are doing a great job with this. I think you are almost there.

You are asking a class you create for some information about something. In this case you need the name of the control stored in the class header.

Property Get / let / set is the "interface" for a class to do what you want.

Property get Name() as string
Name = CmdButton.name '(or whatever the command button variable is called)
end property

This "exposes" the name of the command button to the outside world.

BTW Property Get is a one way street. It allows the programmer to see something but not change something. Property Let is used to save something. This is all about protecting us programmers from ourselves.
 
Last edited:
@Gasman, you are hitting a wall. Go fishing. You will be struggling to get a worm on the hook when the Aha moment arrives.

No not that aha moment, where the hook gouges your finger...
 
I am obviously too stupid to work this out for myself...
I feel your pain Paul. I KNOW you don't really think you're stupid, so I will spare you the pep talk.

Like you, I have an incredibly difficult time sorting Classes out. Everyone else here seems to grasp them with little to no effort and makes me feel like the slow kid in the back of the classroom.

I have found some use for them and have even managed to make one, but ONLY because there was a similar example handy that I was able to plagiarize. Even so, it wasn't without an incredible amount of difficulty
 
I feel your pain Paul. I KNOW you don't really think you're stupid, so I will spare you the pep talk.

Like you, I have an incredibly difficult time sorting Classes out. Everyone else here seems to grasp them with little to no effort and makes me feel like the slow kid in the back of the classroom.

I have found some use for them and have even managed to make one, but ONLY because there was a similar example handy that I was able to plagiarize. Even so, it wasn't without an incredible amount of difficulty
Well boy aren't you the ultimate encouragement! ;)

I spent a year trying and failing before I got it. I am trying to make it easier for the uneducated to become educated. And @Gasman has been a huge help in that regard.
 
@Gasman, you are hitting a wall. Go fishing. You will be struggling to get a worm on the hook when the Aha moment arrives.

No not that aha moment, where the hook gouges your finger...
And you realize that this really hurts the worm.
And you realize that this really hurts the fish.
And you realize this really hurts your finger
And you realize that fishing isn't what it is cracked up to be.

Not that I tell my daughter that. The excitement when she catches a "really big fish" makes it all worthwhile. Just not to the worm or the fish. ;)
 
I feel ya too Gasman. I've struggled with this since MajP's challenges a few years ago. Especially the pseudo controls one.
It's finally sinking in though so don't give up hope.
 
@Gasman,
Sorry I may have made this harder than necessary.
I prefer to do it this way and have it so that the Form just needs to know which command button was pressed and have the form then react to that. So all the code to locate a record remains in the form. The custom class just reports back to the form which button was clicked. This makes it more reusable and loosely coupled. Although that is a better design it probably much more confusing.

Instead if you have the class do all of the locating of the record (put the locating code in the class) then you do not have to report back to the form. You do not have to raise any custom events. This would make implementation easier. The clsAlphaCmd would trap the command button event and move the form. Then in the form all you need is a collection to hold all of the instances. The form does not have to react to any events since the location code resides in the class itself. This is more tightly coupled, but a lot easier to understand how to implement.

1. Where do you put Locate Record code?
In my approach I wanted to make it loosely coupled and kept that in the form. I had the form listen for the click event raised in my collection class
The simpler approach and what you started, it would need to be in the class.

2. Why did I convert iAlpha to string?
When you add anything to a collection you have the following parameters
object.Add item, key, before, after
The Key is a string. Now access is good at casting things so you probably could make the key a number but it will cast it to a string, anyways.

3. Why did it not compile with the .Name?
That was my fault. As I said I normally cut and paste the Collection Class and do some modification. Normally the thing I am adding to the collection class has a Name property and I use that as the key. Then you can return the object you need from the collection using the name.
In the collection class I usually have 2 methods to add to the collection. In the first you pass in the parameters and it builds and instance and adds to the class. The second method allows you to build an instance of some class and add to the collection.

So I will do a simpler example that does not require the collection class.
 
Please do not waste anymore time on this. I am not going to pursue this.
Anyway I would have thought due to all the buttons, a collection would be mandatory to keep them separate?

Thank you for trying anyway. :)
 
Sorry. Again I apologize, although that approach was more flexible but much harder to understand and I understand the frustration. However, you may want to keep it in your back pocket.

If you have the class do the record navigation then the following solution works fine. It is three lines of code to change. Also there is nothing wrong with doing it this way. You might want to modify it with other properties to be more flexible. For example you hard wire the field to search. You could instead pass that in as an argument.

The following is all you need.

Code:
'At top of form
Private MyAlphaCmds As New Collection  ' to hold your instances

Private Sub Form_Load()
  Call SetAlphaButtons ' Initialise the command buttons via a class
End Sub

Private Sub SetAlphaButtons()
 'You need to add all of your instances to MyAlphaCmds but need to create one first
  Dim i As Integer
  Dim NewAlphaCmd As ClsAlphaCmd  ' need to create an instance each time and add to your collection
  For i = 1 To 27
      Set NewAlphaCmd = New ClsAlphaCmd ' create a new alpha cmd
      NewAlphaCmd.fInit Me.Controls("AlphaTab" & i), i
     MyAlphaCmds.Add NewAlphaCmd 'add to collection
  Next
End Sub

In your design originally clsAlphaCmd is doing all the work so it does not need to raise any events.

You can review other techniques here for listening to common events, but if you have your class do the work then it is not necessay.
 

Users who are viewing this thread

Back
Top Bottom