Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2004, 12:32 AM
Authorized User
 
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default I request help understanding "static"

As I study my book concerning "static", I have not seen an explanation concerning the use of static in the following C# example:
********* S t a r t of e x a m p l e ******************

using System;

namespace PersonExample
{
    // ********************** Executable routine **********************
    class MyExecutableClass
    {
        [STAThread]
        static void Main(string[] args)
        {
            Student mike = new Student();
            mike.Age = 22;
            mike.Name = "Mike the Student";

            Instructor john = new Instructor();
            john.Age = 30;
            john.Name = "John the Instructor";

            DescribePerson (mike);
            DescribePerson (john);
        }

        public static void DescribePerson (Person person)
        {
            person.DescribeYourself();
        }
    }
}
******** here are the classes being used ********
using System;

namespace PersonExample
{
// ******************** Person Class *****************************
    public class Person
    {
        public int Age;
        public string Name;

        public Person()
        {
        }
        public void DescribeYourself(string somebody)
        {
            Console.WriteLine("Hello {0}, my name is {1}.", somebody, Name);
        }
        public void DescribeYourself()
        {
            Console.WriteLine("Hello dude, my name is {0}.", Name);
        }
    }
// ******************** Student Class *****************************
    public class Student : Person
    {
        public void Learn()
        {
            Console.WriteLine("OK, in a minute.");
        }
        public Student()
        {
        }
    }
// ******************** Instructor Class *****************************
    public class Instructor : Person
    {
        public int HowManyStudents;
        public void Teach()
        {
            Console.WriteLine("Hello dear students!");
        }
        public Instructor()
        {

        }
    }
}



********* e n d of e x a m p l e ******************
( The error I get when I remove "static" from the Method Header: public static void DescribePerson) is as follows: An object reference is required for the non static field, method, or propery "PersonExample"

I understand that using static on field members gives them a kind of projectwide scope, (static members do not produce instances after the first instance is created - is that right?) but I do not know what the purpose of static is here in my example.


Cmarek
__________________
Cmarek
 
Old October 12th, 2004, 12:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

Static means that you don't have to create an object to reference a property. Otherwise you need to create a new MyExecutableClass object to reference the DescribePerson() method.

Does that help?

Brian
 
Old January 19th, 2005, 04:43 PM
Authorized User
 
Join Date: Jan 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem is that although DescribePerson is within the same class as your Main method, DescribePerson is outside of scope of Main. For this reason if it is not declared static, Main can not access it without an object referrence.

If you want to make it a non-static method, you would need to create a MyExecutableClass object ( lets say you said MyExecutableClass myObject = new MyExecutableClass() ), then you could say myObject.DescribePerson(mike); .

Like bmains said, a static method is a method that behaves like a non-object oriented programming method in that it can be called from anywhere within the namespace without having an object referrence. An example of how this could be used is if you wanted to count the number of Objects in existance.

Let's say you had a static attribute called "people" (private static int people) declared in your Person class. Then each time a Person object is constructed you could add 1 to "people" (Put people++;, people+= 1;, or people = people+1; in the constructor(s)). Since "people" is static, it is only created once in memory and is not specific to just one Person Object but to the entire Person class. You could then create a static method ( public static int getPeople() ) that would return the static variable "people" and would represent the number of Person objects currently in existance (assuming none have been disposed of since being constructed).

//function call from any class in the same NameSpace as Person.
int numberOfPersons = getPeople();

//example of static method
public static int getPeople()
{
    return people;
}


Hope that all made sense :-/

Jesse





Similar Threads
Thread Thread Starter Forum Replies Last Post
request forwarging & request redirection hafizmuhammadmushtaq Servlets 2 April 24th, 2008 12:42 AM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
Request.Form / Request.QueryString Toran Classic ASP Databases 4 January 17th, 2007 02:23 PM
request.qurystring vs. request.form Durwood Edwards Classic ASP Databases 3 June 18th, 2004 12:09 AM
request.querystring() , request.form() alyeng2000 ASP.NET 1.0 and 1.1 Basics 1 December 30th, 2003 12:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.