Click here to Skip to main content
15,910,981 members
Home / Discussions / C#
   

C#

 
GeneralRe: XML Serialization question Pin
Heath Stewart29-Feb-04 17:55
protectorHeath Stewart29-Feb-04 17:55 
GeneralBasics Pin
Matthew Hazlett29-Feb-04 13:14
Matthew Hazlett29-Feb-04 13:14 
GeneralRe: Basics Pin
Jonathan de Halleux29-Feb-04 13:32
Jonathan de Halleux29-Feb-04 13:32 
GeneralRe: Basics Pin
Colin Angus Mackay29-Feb-04 13:35
Colin Angus Mackay29-Feb-04 13:35 
GeneralRe: Basics Pin
Matthew Hazlett29-Feb-04 14:31
Matthew Hazlett29-Feb-04 14:31 
GeneralRe: Basics Pin
Colin Angus Mackay29-Feb-04 14:38
Colin Angus Mackay29-Feb-04 14:38 
GeneralRe: Basics Pin
Matthew Hazlett29-Feb-04 14:47
Matthew Hazlett29-Feb-04 14:47 
GeneralRe: Basics Pin
Colin Angus Mackay29-Feb-04 15:04
Colin Angus Mackay29-Feb-04 15:04 
You can mix static and non-static...

class Customer
{
    // This holds the number that the next newly created account
    // will be.
    private static int _nextAccountNumber = 0;

    // This holds the number of this instance's account.
    private int _accountNumber;


    // This is the constructor that is used when creating
    // a new customer account.
    public Customer()
    {
        _accountNumber = GenerateNextAccountNumber();
    }

    // This is the constructor that is used when pulling
    // an existing account from the database.
    public Customer(int accountNumber)
    {
        _accountNumber = accountNumber;
    }

    // This is a static method that generates the next account
    // number - It is static because regardless of the instance
    // of the class the number must be unique so it uses information
    // it knows about all customer accounts
    protected static int GenerateNextAccountNumber()
    {
        return _nextAccountNumber++;
    }

    // This is a non-static property that returns the account
    // number of this customer only.
    public int AccountNumber
    {
        get
        {
            return _accountNumber;
        }
    }
}


Do you see how it works? This is just one example of how you might use a static member variable and method mixed in a class with non-statics also.

From a static method you cannot access non-static members because you don't have any particular instance as a reference. From a non-static method you can access anything you like because you have access to the non-static member variables as you have a reference to an instance and you can access the static member variables because it doesn't matter one way or the other if you have a reference to a particular instance or not.

Does this help clear things up?


EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar


GeneralRe: Basics Pin
Matthew Hazlett29-Feb-04 19:16
Matthew Hazlett29-Feb-04 19:16 
GeneralDo not understand this (ListView) Pin
Snowjim29-Feb-04 13:01
Snowjim29-Feb-04 13:01 
QuestionSTRRET - Solved? Pin
Tristan Rhodes29-Feb-04 9:12
Tristan Rhodes29-Feb-04 9:12 
AnswerRe: STRRET - Solved? Pin
leppie29-Feb-04 10:41
leppie29-Feb-04 10:41 
AnswerRe: STRRET - Solved? Pin
Dave Kreskowiak29-Feb-04 11:15
mveDave Kreskowiak29-Feb-04 11:15 
AnswerRe: STRRET - Solved? Pin
RNEELY1-Mar-04 4:22
RNEELY1-Mar-04 4:22 
GeneralWindows Service Pin
dabuskol29-Feb-04 1:28
dabuskol29-Feb-04 1:28 
GeneralRe: Windows Service Pin
Mazdak29-Feb-04 2:03
Mazdak29-Feb-04 2:03 
GeneralRe: Windows Service Pin
kenokabe29-Feb-04 16:28
kenokabe29-Feb-04 16:28 
GeneralWierd error message... Pin
profoundwhispers29-Feb-04 0:10
profoundwhispers29-Feb-04 0:10 
Generalcatch Console input Pin
oOomen29-Feb-04 0:05
oOomen29-Feb-04 0:05 
GeneralRe: catch Console input Pin
Arjan Einbu29-Feb-04 6:53
Arjan Einbu29-Feb-04 6:53 
Generalsecurity Pin
mohammadQ28-Feb-04 23:23
mohammadQ28-Feb-04 23:23 
GeneralRe: security Pin
Dave Kreskowiak29-Feb-04 4:05
mveDave Kreskowiak29-Feb-04 4:05 
GeneralRe: security Pin
Verdant12329-Feb-04 4:50
Verdant12329-Feb-04 4:50 
GeneralDirectX Lighting Pin
SherKar28-Feb-04 23:14
SherKar28-Feb-04 23:14 
QuestionHow to determine if you are currently at designtime ? Pin
Andres Coder28-Feb-04 22:47
Andres Coder28-Feb-04 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.