By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,978 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,978 IT Pros & Developers. It's quick & easy.

Object error

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.