X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
> C# Class With New And Class Without New    
post Oct 5 2019, 10:21 PM

Posts: 1,198
Joined: 19-February 08

How do I now classes that not need to create instance form it using new
and class you must use a new to create instance of this class
for example
with C#

Thank you so much

I would like to Thank you for your help
Go to the top of the page
Start new topic
post Oct 6 2019, 05:53 PM

UtterAccess VIP
Posts: 3,055
Joined: 12-April 07
From: Edmonton, Alberta Canada

Actually, access does support (in a way) the idea of not having to create an instance of a class.

In fact, prior to the new release of VBA for access 2000, pre-2000 versions of VBA allowed use of classes without having to create an instance of them.

So then how did all that code work when people upgraded from Access 97 to Access 2000 and beyond?

Well, they placed a flag in the module definition, and while not exposed, you CAN modify this flag, and the result is a static or so called base class that you don’t have to declare an instance of.

So, take this in Access VBA.

Option Compare Database
Option Explicit

Dim m_Date  As Date

Public Property Get MyDate() As Date
   MyDate = m_Date

End Property

Public Property Let MyDate(v As Date)

  m_Date = v
End Property

Public Sub MyHello()
   MsgBox "Hello"
End Sub

Now, to use above in code we go:

Sub test5553434()

   Dim z    As New clsTest
End Sub

To make this work like access 97 days, then open up the class in the VBA editor.

Then file->Export file. You can now open this up with say notepad.
When you export the above, you get this:

  MultiUse = -1  'True
Attribute VB_Name = "clsTest"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit

Dim m_Date  As Date

Public Property Get MyDate() As Date
   MyDate = m_Date
Etc. etc.

And,note the attribute settings. If you change:

Attribute VB_PredeclaredId = False


Attribute VB_PredeclaredId = True

Now, delete the class module, and from the VBA ide, go file->import.

After you import – make sure you hit ctrl-s in the IDE – the imported module will not be saved.

Now, in code, you can use the “base” class name anytime, and do so without having to create a insance of the class.

Eg this:

Sub test5553434()

End Sub

I don’t recommend doing the above, and as noted, this ability and feature is to allow/keep code compatibility with pre-access 2000 databases in which you did not have to declare an instance of the class before using it.

In .net, you can use the static declare, but often, due to my VBA habits, I just add plane jane code modules to my vb.net projects like I do in Access for the “general” code routines that are not written as a class.

For the most part, how classes work in VBA and .net are much the same. About the only real major difference is .net allows overloading, but I don’t use that much in .net anyway.

And this ability or feature of .net or as per above example of doing this in VBA has little to do with one or the other before more OO then the other. Some systems allow static classes, and some don’t.

Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada

Go to the top of the page
post Oct 6 2019, 07:06 PM

UtterAccess Administrator
Posts: 10,495
Joined: 7-December 09
From: St. Augustine, FL

Oh yea, forgot about that.

But then, this is pretty much the same thing as a Standard Module as this point.

It may be worth noting that the Static keyword in VBA - slightly different than the main idea of this topic - is used for variables which retain their values between calls:

Public Function CountMyself() As Integer
   Static MyCount As Integer
   MyCount = MyCount + 1
   CountMyself = MyCount
End Function

'anywhere else, whenever, until the application is shut down (or project is reset):


Jack D. Leach
Founder & CEO
Dymeng Services Inc.
Business Software Solutions
Go to the top of the page

Posts in this topic

Custom Search

RSSSearch   Top   Lo-Fi    25th May 2020 - 03:58 PM