UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> C# Class With New And Class Without New    
 
   
access2009eg
post Oct 5 2019, 10:21 PM
Post#1



Posts: 1,130
Joined: 19-February 08



friends
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#
Vb.net
VBA

Thank you so much

--------------------
I would like to Thank you for your help
Go to the top of the page
 
jleach
post Oct 5 2019, 10:44 PM
Post#2


UtterAccess Editor
Posts: 10,210
Joined: 7-December 09
From: St Augustine, FL


Hi,

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.

Example:

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

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


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

CODE
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

hth

--------------------
Go to the top of the page
 
access2009eg
post Oct 5 2019, 11:04 PM
Post#3



Posts: 1,130
Joined: 19-February 08



jleach

Thanks for your reply
hat_tip.gif

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
C#
VB
VBA

--------------------
I would like to Thank you for your help
Go to the top of the page
 
PhilS
post Oct 6 2019, 03:46 AM
Post#4



Posts: 634
Joined: 26-May 15
From: The middle of Germany


QUOTE
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.
This post has been edited by PhilS: Oct 6 2019, 03:47 AM

--------------------
Go to the top of the page
 
jleach
post Oct 6 2019, 10:43 AM
Post#5


UtterAccess Editor
Posts: 10,210
Joined: 7-December 09
From: St Augustine, FL


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:

CODE
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).

--------------------
Go to the top of the page
 
AlbertKallal
post Oct 6 2019, 05:53 PM
Post#6


UtterAccess VIP
Posts: 2,874
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.

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

CODE
Sub test5553434()

   Dim z    As New clsTest
  
   z.MyHello
  
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:

CODE
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
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

To

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:

CODE
Sub test5553434()

   clsTest.MyHello
  
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.

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

Go to the top of the page
 
jleach
post Oct 6 2019, 07:06 PM
Post#7


UtterAccess Editor
Posts: 10,210
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:

CODE
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):
?CountMyself()
1
?CountMyself()
2
?CountMyself()
3


Cheers,

--------------------
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    14th November 2019 - 04:34 AM