Object error

T

tshad

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
 
T

Tim Wilson

Make the "displayHeadline" method static.

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

Paul E Collins

tshad said:
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.
 
T

tshad

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","FeaturedService");
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
 
B

Bill Butler

tshad said:
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
 
M

Mike Schilling

tshad said:
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.
 
J

Jon Skeet [C# MVP]

tshad said:
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?
 
T

tshad

Jon Skeet said:
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
 
J

Jon Skeet [C# MVP]

tshad said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top