Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing to a new object in a static member

Status
Not open for further replies.

Stephen87

IS-IT--Management
Nov 18, 2006
4
US
Quick quesion all-
Why do you exactly have to create a new object in a static method?

Why wont this work?:
class Program
{
Document doc = new Document();
doc.Write();

static void Main(string[] args)
{

}
}

Thanks
 
It won’t work because on starting the program the 1st method to be executed is main()... but main is empty, a starting class is put in there which is your main program control object (class)does this answer the question

Age is a consequence of experience
 
Sorry, it doesn't answer my question ahah. Main doesn't need an object to run the overall program, if this is what you are pointing to. Im wondering, why excactly you have to create objects on the heap only through static methods.
 
A static member function can access only other static members. They can access non-static members only through an instance of the class. I think this answers your question.

I do not understand your code.
Marty
 
[tt]static void Main[/tt] is a special case, and is treated differently by the compiler. It just is being hosted by the enclosing class. Unless you create an instance of [tt]Program[/tt], your Document instance will never get created.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top