• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

VBA Class only let it be initialised once

Hi,

I have a class that I use in Excel. I only want this class to be initialised once is that possible? I know in C# you can use abstract - guessing there isn't something similar for VBA?

Thanks for any help,
M
0
mcs26
Asked:
mcs26
1 Solution
 
Martin LissRetired ProgrammerCommented:
Can you show us the code that initializes the class and how that code is called?
0
 
mcs26Author Commented:
Hi MatinLiss,

Thanks for the reply. I don't actually have any code yet just trying to plan how to best design it. I was just going to use myclass = new clsIMade.

Thanks,
M
0
 
Martin LissRetired ProgrammerCommented:
Well while not the most elegant way to prevent it, but the simplest would be to define a Public isInitialized As Boolean variable in a Module and when you create the class do this

If Not isInitialized Then
    myclass = new clsIMade
    isInitialized = True
End If

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aikimarkCommented:
Generally, you would have an Initialization() routine for your class that only runs once and should be private.  How else would any initialization code run more than once?
0
 
OrcbighterCommented:
VBA can create classes just the same as any other language that supports Object Oriented programming.

To create a class in VBA, you will select Insert / Class Module from the VBA menu of your project. Lets create a class called Hand.
Create a constructor and Destructor for the class, if the wizard did not already create it for you.
The Constructor must be named Class_Initialize and the Destructor must be named Class_Terminate. They are called automatically when a Class object is instantiated and destroyed, respectively.
eg
Private Sub Class_Initialize()
  ; place your initialisation code here
End Sub

Private Sub Class_Terminate()
  ; place any required cleanup code here
End Sub

You can give the class a method:
Public Sub Point( ByVal Direction as Integer )
   ; do pointy stuff here
End Function

In your main program you create an instance of this class thus:
Dim Finger as New Hand

; then to use it
Finger.Point( 10 );

The Constructor is only called once when the object is first instantiated, so your initialisation code is guaranteed to be executed only once FOR THAT OBJECT.
0
 
mcs26Author Commented:
Hi,

Thanks for the replies. I should I appreciate the intialise code will only run once. What I wanted to stop was having multiple objects of a class. So basically a static class would be an example in C#. I think MartinLiss has probably the simplest way of making sure that I do not have multiple objects.

Thanks,
Mark
0
 
Martin LissRetired ProgrammerCommented:
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now