03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

these operate on the created data type:<br />

System.IO.StreamReader nn = new StreamReader();<br />

nn.Read();<br />

nn.Write();<br />

Static members thus operate on an instance of a class.<br />

Along with static member, it is possible <strong>to</strong> create static variables. These are often<br />

used <strong>to</strong> keep track of the number of instances of a class. An example is shown in<br />

Program 8.4 where a static member variable of numberProfile is created <strong>to</strong> count<br />

the number of instances of the class. This is part of the class, and not part of any instance<br />

of it.<br />

Program 8.4:<br />

using System;<br />

namespace ConsoleApplication5<br />

{<br />

class Profile<br />

{<br />

static int numberProfile=0;<br />

string code, name, telephone;<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

}<br />

// Construc<strong>to</strong>r<br />

public Profile(string c)<br />

{<br />

numberProfile++;<br />

code=c;<br />

}<br />

public string Name<br />

{<br />

<strong>set</strong> { name=value; }<br />

get { return name; }<br />

}<br />

public string Telephone<br />

{<br />

<strong>set</strong> { telephone=value; }<br />

get { return telephone; }<br />

}<br />

public void Display()<br />

{<br />

Console.WriteLine("Code: " + code + " Name: " + name +<br />

" Telephone: " + telephone);<br />

Console.ReadLine();<br />

}<br />

public static void HowMany()<br />

{<br />

Console.WriteLine("There are " + numberProfile + " profiles ");<br />

Console.ReadLine();<br />

}<br />

Agilent .NET Course: More Objects 11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!