PredeclaredID

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Simply looking at this code, it doesn't make sense:

' Standard module

Code:
Sub Start()

    Something.Test 1,2

End Sub

'Class called Something

Code:
Public Sub Test(Val1, Val2)

End Sub

I didn't expect it to compile because my limited understanding of classes is that one must instantiate before using it.

Then I remembered about some additional settings that classes have, so I exported it, then opened it in notepad.

Code:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public Sub Test(Val1, Val2)
    
End Sub

I think what made the code compile and run is this line:

Code:
Attribute VB_PredeclaredId = True

Can someone please explain what it's doing and if setting it to be true means you don't have to instantiate, then why not set it to be true all the time?

Thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Typically you don't need a default instance of a class; the point of them is to be reusable templates.
 
Upvote 0
If you change the value of VB_PredeclaredId to true in a class file, that class will be given a named default instance when your code starts running.
In VBA a Userform is a named default instance class by default, class modules aren't. Why? It's by design, ask MS.

As @RoryA pointed out, you want/need to instantiate a class. You always should avoid working directly with the default instance of a class, but one exception could be a factory method built into the class itself. That way the necessary initialization of the object represented by that class can take place immediately with the benefit of using whatever parameters you want.
 
Upvote 0
If you change the value of VB_PredeclaredId to true in a class file, that class will be given a named default instance when your code starts running.
In VBA a Userform is a named default instance class by default, class modules aren't. Why? It's by design, ask MS.

As @RoryA pointed out, you want/need to instantiate a class. You always should avoid working directly with the default instance of a class, but one exception could be a factory method built into the class itself. That way the necessary initialization of the object represented by that class can take place immediately with the benefit of using whatever parameters you want.
Thanks for the detailed explanation.

I only came across this by accident. Clearly whoever wrote it wanted to demonstrate their detailed understanding of classes.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top