473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

error: An object reference is required for the nonstatic field......



Hi

When I run this code:

using System;

namespace DelegateProject
{
public delegate void MyDelegate(string s);

class Class1
{

public void SayHello(string s)
{
Console.Write(s);
}

static void Main(string[] args)
{
MyDelegate MyInstDel = new MyDelegate(SayHello);
MyInstDel("Hello");
}
}
}

I get the error:

An object reference is required for the nonstatic field, method, or
property 'DelegateProject.Class1.SayHello(string)'
??

What could be wrong?

thanks

Bert Effmans
*** Sent via Developersdex http://www.developersdex.com ***
Feb 16 '06 #1
2 11221
Hi.

This is because you calling non static method in a static method (main)

change the declaration of SayHello to

pubic static void SayHello(string s)
class Class1

{

public static void SayHello(string s)

{

Console.Write(s);

}

static void Main(string[] args)

{

MyDelegate MyInstDel = new MyDelegate(SayHello);

MyInstDel("Hello");

}
}

Regards
Vinu

<Beffmans> wrote in message news:uv**************@TK2MSFTNGP09.phx.gbl...


Hi

When I run this code:

using System;

namespace DelegateProject
{
public delegate void MyDelegate(string s);

class Class1
{

public void SayHello(string s)
{
Console.Write(s);
}

static void Main(string[] args)
{
MyDelegate MyInstDel = new MyDelegate(SayHello);
MyInstDel("Hello");
}
}
}

I get the error:

An object reference is required for the nonstatic field, method, or
property 'DelegateProject.Class1.SayHello(string)'
??

What could be wrong?

thanks

Bert Effmans
*** Sent via Developersdex http://www.developersdex.com ***

Feb 16 '06 #2
Hi,
main is a static method, it's not associated with any instance, SayHello is
an instace method, you need to create an instance before call it, change
your code to:

static void Main(string[] args)
{
Class1 instance = new Class1();
MyDelegate MyInstDel = new MyDelegate( instance.SayHello);
MyInstDel("Hello");
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Feb 16 '06 #3

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

Similar topics

8
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100;...
6
by: WayneD | last post by:
Hi All, Just got started in C#... Here's some C# code: public MyClass { private MyThingy m_Thingy;
1
by: Ray Cheung | last post by:
I am starting to realize that there are differences in vb.net and c#: in terms of syntax, object and keyword in small or big letter In the routine below, I got this error message in c An object...
5
by: Peter | last post by:
Hi, I have a COM object in a dll and it is registered on the system. The COM object was made with Borland C++ Builder and works nicely when used in BC++ projects. I'm TOTALLY new to .NET...
6
by: Laura Carr | last post by:
I have been getting the following error fro an event in an rpx.cs file. The error is as follows: An object reference is required for the nonstatic field, method, or property...
6
by: tshad | last post by:
I am getting the following error for: C:\VSProjects\ClassLibrary4\NewHire.cs(72): An object reference is required for the nonstatic field, method, or property 'MyFunctions.NewHire.firstName' ...
9
by: tshad | last post by:
Why does this not work? Can't I call a function directly from within a class? I also tried this with public in place of private. I get the following error: An object reference is required...
3
by: hazz | last post by:
Why do I get this error for this simple console app. It is complaining about the 'timer1_Elapsed' argument in System.Timers.ElapsedEventHandler(timer1_Elapsed); An object reference is required...
0
by: manicmax | last post by:
Hey I have a question for u I am taking connection string from IIS like this readonly string dataSource3 =ConfigurationManager.ConnectionStrings.ConnectionString; and using this dataSource3 ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.