Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ JAVA and C# _ C# Class With New And Class Without New

Posted by: access2009eg Oct 5 2019, 10:21 PM

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

Posted by: jleach Oct 5 2019, 10:44 PM


The new keyword creates an instance of the object. A static method (or entire class) does not need an instance.

If it's a static class (or if the class contains a static method), you do not need the new keyword.


public class Calculator {

   public int Add(int a, int b) {
     return a+b;
  public static int Multiply(int a, int b) {
    return a*b;


In this class, the Add method must be called on an instance (e.g., with new) of the class:

var x = new Calculator().Add(1, 2);

but the Multiply method is static, and can be called without an instance (and therefore without new):

var x = Calculator.Multiply(5, 10);

Additional notes:

* If the class itself is marked as static, all public members must also be static
* Static members (methods, properties) cannot reference non-static members within the class


Posted by: access2009eg Oct 5 2019, 11:04 PM


Thanks for your reply

Is there anything distinguish between static and normal class
when creating instance other than the error

How do I know this class is Static and this Class is normal

is there image for example appear when using autocomplete

of maybe first letter is capital

I'm Speaking about built in static and normal class
for all this

Posted by: PhilS Oct 6 2019, 03:46 AM

Is there anything distinguish between static and normal class
when creating instance other than the error

How do I know this class is Static and this Class is normal

First, you should generally look at the documentation, at least the object browser, before writing the code to use a class.
Additionally, the development environment should tell you by not offering static classes when using the New keyword with intellisense/code completion.
If you ignore the documentation and other hints and manually type in incorrect class usage, then the compiler will tell you by refusing to compile your code.

Posted by: jleach Oct 6 2019, 10:43 AM

Along with what Phil says, I think maybe there is a special icon/overlay for static members in intellisense dropdowns in Visual Studio, but I don't know offhand what it is.

You should get two different intellisense dropdowns depending on whether you're using the static caller or instance caller version:

var c = new Calculator().
c.[should give both instance and static methods in dropdown]

var x = Calculator.[should give only static in dropdown... I think]

Usually, we will know the method we need, and then we can look at the documentation (or hover over the method name with the mouse to see the method signature), which will tell us whether it's static or not. Even more usually, we will have an instance of the class readily available anyway, so we don't tend to use static members without specific reason.

The majority of classes in the .NET framework are not static. Static members are used in special cases only, and are not quite part of "normal" OOP.

C# and VB.NET follow the same rules in this. In VBA, static entities are something slightly different, and there is no class-level equivalent to .NET static members (VBA is not an OOP language).

Posted by: AlbertKallal Oct 6 2019, 05:53 PM

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 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

Posted by: jleach Oct 6 2019, 07:06 PM

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):