473,395 Members | 1,869 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,395 software developers and data experts.

Object error

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 for the nonstatic field, method, or property
'MultiNamespaces.Class1.displayHeadline()'

The code is:

using System;

namespace MultiNamespaces
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
displayHeadline(); <-- The error
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}

private void displayHeadline()
{
Console.WriteLine("a test");
}
}
}

Thanks,

Tom
Apr 4 '06 #1
9 1380
tshad wrote:
Why does this not work?
Can't I call a function directly from within a class?


You can't call a non-static method unless a specific object instance
is involved.

I'm guessing you want to declare your second method as static.

Eq.
Apr 5 '06 #2
Make the "displayHeadline" method static.

private static void displayHeadline()
{
Console.WriteLine("a test");
}

--
Tim Wilson
..NET Compact Framework MVP

"tshad" <ts**********@ftsolutions.com> wrote in message
news:Oy**************@TK2MSFTNGP02.phx.gbl...
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 for the nonstatic field, method, or property 'MultiNamespaces.Class1.displayHeadline()'

The code is:

using System;

namespace MultiNamespaces
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
displayHeadline(); <-- The error
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}

private void displayHeadline()
{
Console.WriteLine("a test");
}
}
}

Thanks,

Tom

Apr 5 '06 #3
That worked.

But I guess I am a little confused.

I thought I could call any public or private function inside of a class
because it is part of the class.

In my asp.net pages, I don't set my functions as static. For example:

var intCallID = 0;

function Init()
{
GetNewFeatured();
setInterval( "GetNewFeatured()", 5000 )
}
function GetNewFeatured()
{
Service.useService("FeaturedService.asmx?WSDL","Fe aturedService");
intCallID = Service.FeaturedService.callService( "GetFeatured" );
}

GetNewFeatured() is not not a static function, is it?

Do all functions/methods in a class have to be defined as static?

Thanks,

Tom

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:II******************************@bt.com...
tshad wrote:
Why does this not work?
Can't I call a function directly from within a class?


You can't call a non-static method unless a specific object instance is
involved.

I'm guessing you want to declare your second method as static.

Eq.

Apr 5 '06 #4
The deal is you can't call non-static methods from static methods...

VJ

"tshad" <ts**********@ftsolutions.com> wrote in message
news:Oy**************@TK2MSFTNGP02.phx.gbl...
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 for the nonstatic field, method, or
property 'MultiNamespaces.Class1.displayHeadline()'

The code is:

using System;

namespace MultiNamespaces
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
displayHeadline(); <-- The error
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}

private void displayHeadline()
{
Console.WriteLine("a test");
}
}
}

Thanks,

Tom

Apr 5 '06 #5

"tshad" <ts**********@ftsolutions.com> wrote in message
news:O7**************@TK2MSFTNGP03.phx.gbl...
That worked.

But I guess I am a little confused.

I thought I could call any public or private function inside of a class because it is part of the
class.

static methods are associated with the class
non-static methods are associated with instances(objects) of that class.

Here is a sample program demonstrating this
---------------------------------------------------------
using System;
public class Foo
{
private string name;
private static string greeting = "Hello";

public Foo(string name)
{
this.name = name;
}
static void Main(string[] args)
{
Foo foo1 = new Foo("Caitlin");
Foo foo2 = new Foo("Alison");
Foo foo3 = new Foo("Joe");

foo1.Speak();
foo2.Speak();
foo3.Speak();
ChangeGreeting("Goodbye");
foo1.Speak();
foo2.Speak();
foo3.Speak();
}

public void Speak()
{
Console.WriteLine("{0}, {1}",greeting, name);
}

public static void ChangeGreeting(string greet)
{
greeting = greet;
}
}
-----------------------------------------------------
Output:
Hello, Caitlin
Hello, Alison
Hello, Joe
Goodbye, Caitlin
Goodbye, Alison
Goodbye, Joe

Hope this Helps
Bill
Apr 5 '06 #6

"tshad" <ts**********@ftsolutions.com> wrote in message
news:O7**************@TK2MSFTNGP03.phx.gbl...
That worked.

But I guess I am a little confused.

I thought I could call any public or private function inside of a class
because it is part of the class.


It's visible, yes, but you haven't called it correctly. An instance method
needs to be called on an instance, and you haven't supplied one. You could
say, for example
static void Main(string[] args)
{
Class1 instance = new Class1();
instance.displayHeadline(); <-- This is OK
now
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}

private void displayHeadline()
{
Console.WriteLine("a test");
}

But since displayHeadline() doesn't use any of the class's state, it's more
logical to make it static. If the code looked like

private string headline;

static void Main(string[] args)
{
Class1 instance = new Class1("This is a test");
instance.displayHeadline(); <-- This is OK
now
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}

public Class1( string s)
{
headline = s;
}

private void displayHeadline()
{
Console.WriteLine(headline);
}

, then creating the instance is pretty clearly what you need.
Apr 5 '06 #7
tshad <ts**********@ftsolutions.com> wrote:
But I guess I am a little confused.

I thought I could call any public or private function inside of a class
because it is part of the class.


Yes, you can - if you have an appropriate reference to call the method
on, if it's an instance method. If you're already within an instance
method, then you can call another instance method and you're implicitly
calling it on "this". If you're in a static method, however, you're not
currently in the context of any particular object, so you need to say
what object to run the instance method on.

When Main runs, no instances of your class have been created. Suppose
your displayHeadline method used an instance variable - what would you
have expected it to do?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 5 '06 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
tshad <ts**********@ftsolutions.com> wrote:
But I guess I am a little confused.

I thought I could call any public or private function inside of a class
because it is part of the class.
Yes, you can - if you have an appropriate reference to call the method
on, if it's an instance method. If you're already within an instance
method, then you can call another instance method and you're implicitly
calling it on "this". If you're in a static method, however, you're not
currently in the context of any particular object, so you need to say
what object to run the instance method on.


So if I called dispalyHeadline as static instead of private, then it would
have worked?

Tom

When Main runs, no instances of your class have been created. Suppose
your displayHeadline method used an instance variable - what would you
have expected it to do?

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

Apr 5 '06 #9
tshad <ts**********@ftsolutions.com> wrote:
Yes, you can - if you have an appropriate reference to call the method
on, if it's an instance method. If you're already within an instance
method, then you can call another instance method and you're implicitly
calling it on "this". If you're in a static method, however, you're not
currently in the context of any particular object, so you need to say
what object to run the instance method on.


So if I called dispalyHeadline as static instead of private, then it would
have worked?


It could have been static as well as private - they're orthogonal
concepts. But yes, it would have worked.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 5 '06 #10

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

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
2
by: Nithi Gurusamy | last post by:
Dear Group: I have a COM object developed in VB. It makes ADODB calls. When it fails it Raise Error. I am using the COM object in my ASP using Server.CreateObject. Whenever a function call fails...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
8
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
2
by: Roby Eisenbraun Martins | last post by:
Hi, My name is Roby Eisenbraun Martins, I am a C++, VB and NET developer. I am working with a NET 2002 project right now and I am receiving this uncommon "OutOfMemory" error message when I try...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
2
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.