Class Modules

User avatar
DocAElstein
5StarLounger
Posts: 643
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Class Modules

Post by DocAElstein »

Part 1 (reply to Mike, (Speakeasy) )


>3) An object itself does not have a module; it is your IDE that has modules.
>That is a technicality again open to perception I think

Ok, so let's talk about traditional compiled software. It has source code which is compiled to object code . And it is the object code that gets distributed. The object code does not include the source code, and the end user thus does not have access to that source code. They only have access to functionality defined by that source code. And back in VBA OOP, the source code for a class is the class module. part of the problem, I suspect, is that you are conflating two concepts, and using the word 'module' to mean two things (VBA modules, which contain source code, and the compiled code that provides the functionality of the program - whether in an object or not. But that compiled code is NOT a module;' hence my comment, by the way, that "There is no module for you to see")


I am not sure if I was conflating, maybe I was doing it, and didn’t notice – I got a ton of great tasting Veggie meat balls for free recently, and I could literally get a van full more if I wanted, so I am trying to eat through them before they go bad, maybe that caused me to conflate, apologies if I did. I got plenty of windows open as usual, so that should limit the damage.
But I jest, :) , - more seriously now…
I didn’t meant to give the impression I was conflating two different things. I think I was not intending to be talking about compiled coding as that scares me. I am thinking just that
_ a copy of the text in the class module inside some thing not too dissimilar (in terms of a square window and icons) in the VB Editor might be a nice thing to appear , or for you to imagine it to appear, after you instantiate an object from the class
,_ and that the name of the thing appearing might be the name of the variable used in the insanitation

I think that I was meaning the VBA modules, and meaning that in a similar vain of allowing myself to perceive that a spreadsheet exists, I was being so bold as to take the liberty to perceive that an object module looking not too dissimilar to the VBA worksheet modules existed after an instantiation of my custom class object.
I am very grateful that you qualified your "There is no module for you to see" , it makes sense what you are saying, I think I take your point.

I am at the level of high up end user that can reasonably suggest/ perceive that a spreadsheet, for example , exists, even if you might correctly convince me that it doesn’t. ( To be honest I long since had my suspicions myself, Lol – I figured Excel is more like a single cell, and what I perceive as a spreadsheet is some set of offsets synchronised to the voltage levels on the scan coils of my screen tube that goes across, then down then across, building up this picture to deceive me into thinking I have a spreadsheet,….. but that’s another Alan Theory for another day , perhaps )

I guess as a laymen I am saying that the source code that is the class can be thought of, in layman terms, as, just text on paper, whereas the code for example in a VBA worksheets code module that we physically see in the VB Editor and can run in step mode, is, as I perceive , a bit more substantial in some way, which way exactly I don't care to try to define just yet, but as it is more substantial, as such, it is perhaps, or has in it, slightly more "real" coding in a layman sense. But I am taking your point, - if I understand it correctly, that it is still source code.
I want a term that allows something to be a bit more substantial, in a perception sense, as a piece of paper with text on it, and an instantiated object’s (source) code was kind of making me think I could tie it into, and regard it as, part of, the thing we refer to as the worksheet (object)

_.___

To explain again a bit better, If I may, referring to my ramblings from here , which I am busy revisiting and reviewing in light or our discussions…. (Please note to only look from/ beginning at that link which should take you to page #3 , starting at post #21)

So, This I can see with my eyes already , - it’s the custom code module, that I added, and named FileOpenWatcher
my custom class module that I have got.JPG

I got this next coding somewhere else, (less important where), and that coding, amongst other things, instantiates from that class, using a variable, MeWatcher. ( That variable, is then referring to my new instance of the class).
Do an instanciate and fill a property.JPG

I think it would be nice to have had this sort of thing pop up after that Instanciation, and that popped up thing would had the name MeWatcher , and I would like if it’s icon was in the VB Editor left hand explorer window at some similar looking level as the worksheet code module icons. (So this next screen shot is not something I can see – it is what i would like to see, and what I currently like to perceive when trying to understand these things
What I would like to apppear after my instantiating.JPG

I would not want to be able to edit it that last thing, and perhaps after it popped up I should either be banned from editing my class module, or, if I do edit that coding, then those changes should be immediately reflected in identical changes in my MeWatcher module, and or visa versa. – One is what I describe as text on a piece of paper, and the other is "made" from it, so they need to look the same,and so changes in one would somehow automatically be reflected in the other. Or, alternatively, after the first instantiated object is made , then editing is banned, and if I wanted to change something I would have to delete the two things and start again.

If I had that thing that I would like to have, it would then be nice that a step debug mode run of coding , if appropriate, went through that MeWatcher module, rather than me having the option to "break into class module", FileOpenWatcher, - since I can’t run coding in my added class module, (which I think is good and correct), so I should not be allowed ever to step through it either. I admit that the option is a useful tool to have, but I would prefer it to be possible to do it through this alternative way I have suggested.
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 19 Jun 2024, 18:44, edited 5 times in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
DocAElstein
5StarLounger
Posts: 643
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Class Modules

Post by DocAElstein »

Part 2 (reply to Mike, (Speakeasy) ) ( Part 1 in previous post here )
_.____________

I think we are agreeing on things to do with the New for a worksheet declaration not working. But you have made some useful references for me to come back to , at a later date if I am able to , in order to get a better low level understanding. Thanks
Perhaps the .Add is a factory method?

_._________________________-


( >met OOP (which VBA isn’t, but tries to act like – according to other smarter people here …..
It doesn't meet the purist definition of OOP, since it does not support inheritance or polymorphism (although you can do some polymorphism thanks to Implements)
As smarter people argue the toss on this one a lot, I won’t look further into it, but just link another Post I passed on it here https://eileenslounge.com/viewtopic.php ... 96#p197596 for future cross reference )


_.__________________-

I still have a tendency to call what I can see in the VB Editor as a worksheet code module, and which I perceive as a worksheets class object module, a worksheets class object module (or object module, for short), just as a way to distinguish it from a class module ( the thing we can add).
But you are making me want to think of calling it something else. What exactly I am not sure yet.

I am not yet convinced I want to be calling it a class module yet.
I am nervous, for example, to justify calling it a class module because it has source code in it, or else I might have to call a normal code module, ( the thing we see in the VB editor with default name Modulex), a class module. I suspect that won’t do at all, and I have a gut feeling it’s wrong to say that. Why I am not too sure, as I am still not too sure how to describe accurately what that thing we see as a normal code module is. To be honest, I am more happy about coming up with theories as to what the Worksheets, ThisWorkbook, and my imaginary code modules are !

I am still thinking that I want to call a worksheet code module and my perceived imaginary code module, ( the one representing my variable, MeWatcher , that is referring to my New instanceiated instance of the class named FileOpenWatcher ) something similar in terms of what it is, when describing what sort of thing it is

_._________________


Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
DocAElstein
5StarLounger
Posts: 643
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Class Modules

Post by DocAElstein »

Hello :)
This post is mainly a bit of a correction to a previous post of mine in the thread. The correction is a bit more substantial than appropriate for a quick edit, so I thought better do another post.

In that post of mine , I responded to the previous post from some one post OP, Bot, or whatever it/ he was, giving an example of a … Class Module for check the name of workbook when is open..
I sanitised that offering a bit, and supplemented it a bit, with some explanations.
I was under the impression, and probably gave the impression therefore, that it was an example of a good use of a class module.
I think now that I was wrong.

Parallel to all that, I picked up some good info from the discussions on class things thereafter, and with that improved knowledge, and quite a bit of other researching and experiments on and off in the meantime, I am a bit wiser…. Well a least, I think so, Lol. Maybe in a day or two I might like to follow up those discussions. ( Perhaps as this thread is a bit cluttered now, so I might do that in a fresh Thread or two, …. so that's something for us all to look forward to :evilgrin: :smile: )

One last thing before I start, which perhaps in a day or two I might post a new Thread to discuss … The following improved solution / corrections, I tripped over, as obvious without even thinking about it at all, just like falling off a log when you don’t even think about making any attempt to keep balance. This came about as I pursued my attempts at understanding, as I see it, of class things, and, I am being honest, (even if it gets me later a lot of hate mail, and death threats, Lol), .. - if I was thinking that the ThisWorkbook module and worksheet modules were any forms of class modules, then I would not have got these solutions even by trying for a few weeks or months. (I probably would have got there after a year or ten though – I get anywhere and everywhere eventually, Lol). But maybe that is all for a later discussion, Here , I just wanted to correct my previous bad in this Thread….

_._____________________________________________-

So…

_1) I already gave my supplemented class / class module solution, here, Post #21 and here, Post #22 , (which in the meantime I only made some small improvements based on my improved knowledge)

I went on to do a couple of solutions without a class module.
Here, Post #24 is a bit of background to my thinking,


_2) Here, Post #25 is a solution with no class module, using the ThisWorkbook module instead

_3) Here, Post #26 is a solution with no class module, using a worksheet module instead.

_.______

The point of me doing that and making this post is…..
In an earlier posting in this thread, I suggested, in answer to the original post asking about uses of class modules….
DocAElstein wrote:
08 May 2024, 20:14
..... very simplified, we can say maybe two main areas they might be used.
_ Where you don’t really need them, but they might help you organise things. Along the same vein of using functions. ............
_ The area where I have mostly seen them doing something that might be difficult without them. This usually involves tackling the area of WithEvents - You can see that straight away in the first two links from Hans ..........
Well, with hindsight, now, neither of those apply to the example, to my current way of thinking. -. The second solution I present here _2) , seems the best, neatest, … and their 'aint no class module in sight … well …. JIMHO. … :flee:

Perhaps, if I may, any discussions on this I would like to defer until I ask for some more help, to pick the brains of the smarter people here, on the general class and class module subject in a day or two. Perhaps if anyone interested in that might find this and the referenced musings of mine an interesting prelude, to help build up a case to sentence me to death, as an idiot, Lol, :) … :flee:

Alan
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 19 Jun 2024, 19:14, edited 1 time in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
HansV
Administrator
Posts: 78850
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Class Modules

Post by HansV »

ThisWorkbook is - as has been mentioned before in this thread - a special case of a class module...
Best wishes,
Hans

User avatar
DocAElstein
5StarLounger
Posts: 643
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Class Modules

Post by DocAElstein »

I am inclined to think it might be, as smarter people keep saying it. But I am struggling to find any explanation as to why.

My thinking, the TLDR version, that got me the new solutions, was, that there is a parallel to
what they, (ThisWorkbook module and Worksheet modules), are
and
an instantiated object from a class.

But I will try to explain that more fully when I have all my thoughts clear in a day or two
Last edited by DocAElstein on 20 Jun 2024, 06:58, edited 1 time in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
SpeakEasy
4StarLounger
Posts: 598
Joined: 27 Jun 2021, 10:46

Re: Class Modules

Post by SpeakEasy »

Your examples use WithEvents. WithEvents can ONLY be used in a class module. Ipso facto, whether you like it or not, Sheet1 and ThisWorkbook are class modules

User avatar
DocAElstein
5StarLounger
Posts: 643
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Class Modules

Post by DocAElstein »

I have heard that one before. There must be more to it, that I am missing information on. If the class module and the ThisWorkbook and worksheet modules were coloured blue, I would not think that my old blue VW bus , or the Blue Bier accessories in it, was a class, Ipso defacto or whatever. But I am always open to persuasion. German Dunkel Bier is of a class, that after getting used to it, you would never want to go near English wee wee Beer ever again

My thinking, which I accept could be wrong is, maybe… WithEvents can ONLY be used in a class module, or an object module part of an object, or related to, an object instantiated from a class using an WithEvents declaration or wirings thereof


I don’t have any preferences of liking it or not, one way or the other. I am just trying to understand if/ why. It's just a personal preference of not liking to useing things I do not understand or can’t explain

What is wrong with this .. An object instantiated from a class which has a WithEvent declaration, has that declaration, so must be wired to accept it.
In other words, the class used to instantiate the ThisWorkbook or a Worksheet, ( which we don’t have direct access to ), has that sort of declaration in order to accept / have the default event coding , and so is generally “wired” to accept that sort of declaration.

We have no way to modify the class of a Worksheet or ThisWorkbook. (They are not exposed for automation. No interface has been provided for us). I am thinking perhaps that, that contradicts that ThisWorkbook and a Worksheet module are class modules. My argument currently, which I accept might be wrong, is that we can modify the instantiated object, and that the ThisWorkbook module or a worksheet module is one of the tools to allow us to do that.

Microsoft Office is a very old program. I doubt anyone has a clear grip of it all. Microsoft documentation is accepted by most people as often being not so good, and changeable, so not very reliable. Microsoft documentation is just worth a quick look, that’s all, especially if it is much older original documentation, as there may be something in them based on some original true facts.


To a large extent we are left to guess. Although debatable, some very smart and experienced people are of the opinion that VBA only attempts to be an Object Orientated Programming language. My theories and musings tie up with the results and lead to solutions otherwise that would not be allowed or tolerated. I am a final home user, not a theoretician conforming to something I don’t know what.


But I am wiser for all the comments and criticisms, and very grateful for them. I always have them in my head to try and influence my theories for the better. I am way far off having any good track record of being right to defend. That gives me a certain extra freedom and ability that many don’t have: I am very happy to be wrong all the time, if I get the right answer with a plausible explanation and working theory in the end.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
SpeakEasy
4StarLounger
Posts: 598
Joined: 27 Jun 2021, 10:46

Re: Class Modules

Post by SpeakEasy »

>ThisWorkbook and worksheet modules were coloured blue, I would not think that my old blue VW bus was a class

Appreciate you are being somewhat tongue-in-cheek here, but that's not even close to the same thing.

If there was some sort of global automotive industry rule that blue paint can only be used on VW busses, then if you saw a blue vehicle it could only, ipso facto, be a VW bus

>We have no way to modify the class of a Worksheet or ThisWorkbook. (They are not exposed for automation. No interface has been provided for us). That contradicts that ThisWorkbook and a Worksheet module are class modules

Oh, but you can, they are, and consequently it doesn't

Here's an example of working with their interfaces, and modifying them

ThisWorkbook code:

Code: Select all

Option Explicit

Public Event ok(strWorkbook As String)
Private mValue As String

Public Property Let NewProperty(newValue As String)
    mValue = newValue
End Property

Public Property Get NewProperty() As String
    NewProperty = mValue
End Property

Public Sub ForceAnEvent()
    RaiseEvent ok(Me.Name)
End Sub

' To illustrate that ThisWorkbook also supports all existing events (and properties and methods) of the Workbook class
' (basically ThisWorkbook has a hidden Dim WithEvents Workbook as Workbook; similalry Sheet<x> has a hidden Dim WithEvents
' WorkSheet as WorkSheet, and a userform has a hidden Dim WithEvents UserForm as UserForm
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    MsgBox "A sheet is activating"
End Sub
Sheet1 code

Code: Select all

Option Explicit

Public WithEvents myworkbook As ThisWorkbook

' Define a public method for Sheet1 class
Public Sub Example()
    Set myworkbook = ThisWorkbook
    MsgBox "I am sheet " & Me.Name
    
    MsgBox "I am in " & myworkbook.Name
    myworkbook.NewProperty = "Yes"
    MsgBox "I have added a new property to the ThisWorkbook class, and it currently holds " & myworkbook.NewProperty
    myworkbook.ForceAnEvent
End Sub

Private Sub myworkbook_ok(strWorkbook As String)
    MsgBox "ooh, an event from my customised ThisWorkbook class: " & strWorkbook
End Sub
And using it from a normal code module:

Code: Select all

Option Explicit

Public Sub main()
    Dim mysheet As Sheet1
    
    Set mysheet = Sheet1
    mysheet.Example ' call the new method we added to the Sheet1 class
    mysheet.Activate ' and what is happening here?
    MsgBox "And finally, did this mean we modified the existing objects? " & ThisWorkbook.NewProperty & "and" & ActiveWorkbook.NewProperty 'and here?
End Sub

User avatar
DocAElstein
5StarLounger
Posts: 643
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Class Modules

Post by DocAElstein »

Thanks, appreciate the response.
I won’t respond too quickly. I want to go through all my musings on this, yet again, when I can really concentrate on it, maybe in a dedicated Thread, or two, to hammer this one out, so as to get specific issues less clouded by trying to do too much at once .
We seem to keep getting 90% there, and so start again and again.
I am not afraid of accepting that Sheet1 and ThisWorkbook are class modules, I have nothing to lose one way or the other. I just want to understand why.
Any other ideas or comments are always very welcome. You can be sure I will go through them all very carefully, I always do.

Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
HansV
Administrator
Posts: 78850
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Class Modules

Post by HansV »

I'll pass.
Best wishes,
Hans