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
'MultiNamespace s.Class1.displa yHeadline()'
The code is:
using System;
namespace MultiNamespaces
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
displayHeadline (); <-- The error
Console.WriteLi ne("Press any key to exit!");
Console.ReadLin e();
}
private void displayHeadline ()
{
Console.WriteLi ne("a test");
}
}
}
Thanks,
Tom 9 1391
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.
Make the "displayHeadlin e" method static.
private static void displayHeadline ()
{
Console.WriteLi ne("a test");
}
--
Tim Wilson
..NET Compact Framework MVP
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Oy******** ******@TK2MSFTN GP02.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 'MultiNamespace s.Class1.displa yHeadline()'
The code is:
using System;
namespace MultiNamespaces { class Class1 { [STAThread] static void Main(string[] args) { displayHeadline (); <-- The error Console.WriteLi ne("Press any key to exit!"); Console.ReadLin e(); }
private void displayHeadline () { Console.WriteLi ne("a test"); } } }
Thanks,
Tom
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.useServ ice("FeaturedSe rvice.asmx?WSDL ","FeaturedServ ice");
intCallID = Service.Feature dService.callSe rvice( "GetFeature d" );
}
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.
The deal is you can't call non-static methods from static methods...
VJ
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Oy******** ******@TK2MSFTN GP02.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 'MultiNamespace s.Class1.displa yHeadline()'
The code is:
using System;
namespace MultiNamespaces { class Class1 { [STAThread] static void Main(string[] args) { displayHeadline (); <-- The error Console.WriteLi ne("Press any key to exit!"); Console.ReadLin e(); }
private void displayHeadline () { Console.WriteLi ne("a test"); } } }
Thanks,
Tom
"tshad" <ts**********@f tsolutions.com> wrote in message
news:O7******** ******@TK2MSFTN GP03.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(objec ts) 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.WriteLi ne("{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
"tshad" <ts**********@f tsolutions.com> wrote in message
news:O7******** ******@TK2MSFTN GP03.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.displa yHeadline(); <-- This is OK
now
Console.WriteLi ne("Press any key to exit!");
Console.ReadLin e();
}
private void displayHeadline ()
{
Console.WriteLi ne("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.displa yHeadline(); <-- This is OK
now
Console.WriteLi ne("Press any key to exit!");
Console.ReadLin e();
}
public Class1( string s)
{
headline = s;
}
private void displayHeadline ()
{
Console.WriteLi ne(headline);
}
, then creating the instance is pretty clearly what you need.
tshad <ts**********@f tsolutions.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... tshad <ts**********@f tsolutions.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
tshad <ts**********@f tsolutions.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Error in '/Cr_Dataset' Application.
-----------------------------------------------------------
---------------------
|
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 I wanted the system to catch the 500-100
error and redirect to the configured page in IIS. But nothing happens. I
don't have "on error resume next" in my COM object.
If I create ADO objects directly in my ASP code using Server.CreateObject it...
|
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
(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
On the aspx page I have the object tag as follows:
|
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
ID=MyID
|
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 to load a form object ( new
frmMain() ).
In debug mode, the "Load" form method is executed but it crashes when it
tries to set a DataTable from a DataSet in a local variable. Actually the
object value in debug mode is equal to nothing.
| |
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).
Since then, I found lots of the following in the postmaster output:
2003-11-29 15:19:54 ERROR: large object 4838779 does not exist
2003-11-29 15:20:11 ERROR: large object 4838779 does not exist
|
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 working when I include data entry
fields to the form: I click on Delete or Edit inside of
DataGrid and get this error: "Error: Object doesn't
support this property or method"
If I remove data entry fields from the form - DataGrid
allows to...
|
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 then tried to access 2nd, or 3rd, etc page. The problem is it
happens sometimes - sometimes when they clicked refresh button, then
everything is ok. Now they told me it happens more frequently. but I
have tried by myself many times and never got...
|
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 other properties...
bool error;
string lastError;
My whole class looks like this...
using System;
|
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 for it
indicator = document.getElementById('indicator');
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |