My Assistant
![]()
Custom Search
|
![]() ![]() |
![]() |
![]() Post#1 | |
Posts: 1,142 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 |
![]() Post#2 | |
![]() UtterAccess Administrator Posts: 10,287 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 -------------------- |
![]() Post#3 | |
Posts: 1,142 Joined: 19-February 08 ![]() | jleach 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 C# VB VBA -------------------- I would like to Thank you for your help |
![]() Post#4 | |
![]() Posts: 653 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 -------------------- |
![]() Post#5 | |
![]() UtterAccess Administrator Posts: 10,287 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). -------------------- |
![]() Post#6 | |
![]() UtterAccess VIP Posts: 2,903 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 |
![]() Post#7 | |
![]() UtterAccess Administrator Posts: 10,287 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, -------------------- |
![]()
Custom Search
|
![]() | Search Top Lo-Fi | 16th December 2019 - 03:07 AM |