473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# classes - how do I refer to a method in a class without instatiating it?

Hi

I want to refer a class called LogEvent, and use one of its methods called
WriteMessage without actually having to create an instance of Logevent.

I have tried using the word sealed with the class and this works but I would
also like to know of other ways to do this.

Also are there any performance implacations of using sealed?

Thanks
Kuv Patel


Nov 15 '05 #1
5 14417
You have to make the method static, otherwise you cannot call the method
without creating an object.
Like this:

public static void WriteLogEvent()
{}

Greetz,
-- Rob.

kuvpatel wrote:
Hi

I want to refer a class called LogEvent, and use one of its methods
called WriteMessage without actually having to create an instance of
Logevent.

I have tried using the word sealed with the class and this works but
I would also like to know of other ways to do this.

Also are there any performance implacations of using sealed?

Thanks
Kuv Patel

Nov 15 '05 #2
Note: you can then call the class with: EventLog.WriteM essage().
In other words: ClassName.Metho dName()

Greetz,
-- Rob.
Nov 15 '05 #3
mk
check out the 'static' modifier, sealed simply marks a
class as not-inheritable.
-----Original Message-----
Hi

I want to refer a class called LogEvent, and use one of its methods calledWriteMessage without actually having to create an instance of Logevent.
I have tried using the word sealed with the class and this works but I wouldalso like to know of other ways to do this.

Also are there any performance implacations of using sealed?
Thanks
Kuv Patel


.

Nov 15 '05 #4
kuvpatel <kp@geuk.co.u k> wrote:
I want to refer a class called LogEvent, and use one of its methods called
WriteMessage without actually having to create an instance of Logevent.

I have tried using the word sealed with the class and this works but I would
also like to know of other ways to do this.

Also are there any performance implacations of using sealed?


Sealed is completely separate to what you want. I believe what you
actually mean is static, which exactly means "this method doesn't run
in the context of an instance, so you can run it without creating an
instance".

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
http://www.csharphelp.com/archives/archive148.html
A C# class can contain both static and non-static members. When we declare a
member with the help of the keyword static, it becomes a static member. A
static member belongs to the class rather than to the objects of the class.
Hence static members are also known as class members and non-static members
are known as instance members.

In C#, data fields, member functions, properties and events can be declared
either as static or non-static. Remember that indexers in C# can't declared
as static.

Static Fields

Static fields can be declared as follows by using the keyword static.

class MyClass
{
public static int x;
public static int y = 20;
}
When we declare a static field inside a class, it can be initialized with a
value as shown above. All un-initialized static fields automatically get
initialized to their default values when the class is loaded first time.
For example
// C#:static & non-static
// Author: ra******@msn.co m
using System;
class MyClass
{
public static int x = 20;
public static int y;
public static int z = 25;
public MyClass(int i)
{
x = i;
y = i;
z = i;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLi ne("{0},{1},{2} ",MyClass.x,MyC lass.y,MyClass. z);
MyClass mc = new MyClass(25);

Console.WriteLi ne("{0},{1},{2} ",MyClass.x,MyC lass.y,MyClass. z);
}
}
The C# provides a special type of constructor known as static constructor to
initialize the static data members when the class is loaded at first.
Remember that, just like any other static member functions, static
constructors can't access non-static data members directly.
The name of a static constructor must be the name of the class and even they
don't have any return type. The keyword static is used to differentiate the
static constructor from the normal constructors. The static constructor
can't take any arguments. That means there is only one form of static
constructor, without any arguments. In other way it is not possible to
overload a static constructor.

We can't use any access modifiers along with a static constructor.

// C# static constructor
// Author: ra******@msn.co m
using System;
class MyClass
{
public static int x;
public static int y;
static MyClass ()
{
x = 100;
Y = 200;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLi ne("{0},{1},{2} ",MyClass.x,MyC lass.y);
}
}
Note that static constructor is called when the class is loaded at the first
time. However we can't predict the exact time and order of static
constructor execution. They are called before an instance of the class is
created, before a static member is called and before the static constructor
of the derived class is called.
Static Member Functions

Inside a C# class, member functions can also be declared as static. But a
static member function can access only other static members. They can access
non-static members only through an instance of the class.

We can invoke a static member only through the name of the class. In C#,
static members can't invoked through an object of the class as like in C++
or JAVA.

// C#:static & non-static
// Author: ra******@msn.co m

using System;
class MyClass
{
private static int x = 20;
private static int y = 40;
public static void Method()
{
Console.WriteLi ne("{0},{1}",x, y);
}
}
class MyClient
{
public static void Main()
{
MyClass.Method( );

}
}
Static Properties

The properties also in C# can be declared as static. The static properties
are accessing using the class name. A concrete example is shown below.

// C#:static & non-static
// Author: ra******@msn.co m

using System;
class MyClass
{
public static int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}

}
class MyClient
{
public static void Main()
{
MyClass.X = 20; // calls setter displays SET
int val = MyClass.X;// calls getter displays GET
}
}
Static Indexers
In C# there is no concept of static indexers, even though static properties
are there.

Static Members & Inheritance

A derived class can inherit a static member. The example is shown below.

// C#:static
// Author: ra******@msn.co m
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLi ne("Base static method");
}

}
class MyClass : MyBase
{
}
class MyClient
{
public static void Main()
{
MyClass.Method( ); // Displays 'Base static method'
Console.WriteLi ne(MyClass.x);// Displays 25
}
}
But a static member in C# can't be marked as override, virtual or abstract.
However it is possible to hide a base class static method in a derived class
by using the keyword new.
An example is shown below.

// C#:static & non-static
// Author: ra******@msn.co m

using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLi ne("Base static method");
}

}
class MyClass : MyBase
{
public new static int x = 50;
public new static void Method()
{
Console.WriteLi ne("Derived static method");
}
}
class MyClient
{
public static void Main()
{
MyClass.Method( ); // Displays 'Derived static method'
Console.WriteLi ne(MyClass.x);// Displays 50
}
}
Finally remember that it is not possible to use this to reference static
methods.
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
kuvpatel <kp@geuk.co.u k> wrote:
I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent.

I have tried using the word sealed with the class and this works but I would also like to know of other ways to do this.

Also are there any performance implacations of using sealed?


Sealed is completely separate to what you want. I believe what you
actually mean is static, which exactly means "this method doesn't run
in the context of an instance, so you can run it without creating an
instance".

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2126
by: Michael | last post by:
I've written a simple class that puts together a MySQL SELECT query and allows you to extend the query, but I'm unsure as to when to use $this - > var_name and when I can just use $varname, and when it's necessary to use 'var $varname' and when this can be left out. Here's the class - could it be made simpler? Michael class...
45
3580
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any...
0
3332
by: Ewart MacLucas | last post by:
generated some WMI managed classes using the downloadable extensions for vs2003 from mircrosoft downloads; wrote some test code to enumerate the physicall processors and it works a treat, but a question.. The code fails with the error that: "Additional information: COM object that has been separated from its underlying RCW can not be used."...
3
2416
by: JAPSTER | last post by:
An application has a startup form (Form1). A second form (frm2), created from within Form1. Each form has a textbox. To access the text in TextBox1 in frm2 from Form1, you use: frm2.TextBox1.Text = "XXXXXX" How do I refer to the TextBox1 controls Text property in Form1 from frm2? Form1.TextBox1.Text doesn't work and generates a...
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
11
2484
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
12
3420
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to...
26
2520
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
12
11048
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8176
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5699
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3816
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.