Entry
C#: Class: Create: Simple: How to create a class in C#?
Jan 10th, 2004 10:47
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 11 September 2003 - 16:03 pm ------------------
C#: Class: Create: Simple: How to create a class in C#?
---
A class is zero or more functions, procedures or variables,
grouped together, with a common name (e.g. addressC).
---
Inside you tell about their access (e.g. internal, private, public,
protected).
http://www.faqts.com/knowledge_base/view.phtml/aid/24765/fid/791
---
There are also zero or more special procedures to initialize
(called 'constructors').
---
This class information will be stored in 1 specific location in memory.
---
To use your class, you first assign it to a variable.
---
Note:
This process of creating you can repeat zero or more times, and each
time a new location in memory with the same memory structure as that of
the given class (in that single memory location) will be assigned to
the variable associated with that class.
So all this variables can be seen as initially having the same
structure (=variables, and a reference to the 1 memory location where
that functions and procedures can be found) as that class, but later
possibly with different data (=variables) inside it, and this variables
are also rather independent of each other (as each variable has its own
separate location in memory).
---
To access the procedures, functions and variables, after
initializing, you can use the classname followed by a dot
followed by the name of that variable, function or procedure.
---
---
Steps: Overview:
1. Possibly download and install the
free Microsoft .net framework SDK,
http://www.microsoft.com/downloads/details.aspx?familyid=4fe5bdb5-
c7a7-4505-9927-2213868a325b&languageid=f49e8428-7071-4979-8a67-
3cffcb0c2524&displaylang=en
or
use Visual Studio .net
When installed, the C# compiler
csc.exe
can usually be found in a directory
similar to:
C:\WINDOWS\Microsoft.NET\Framework\v1.0
2. Type the program in your favorite text editor
--- cut here ---------------------------------------------------------
using System;
class addressC {
public string nameS;
public string addressS;
public string cityS;
public string countryS;
public string phonenumberS;
public string emailS;
}
class Testaddress {
public static void Main() {
addressC knudO = new addressC();
addressC johnO = new addressC();
//
knudO.nameS = "Knud";
knudO.addressS = "my street 123";
knudO.cityS = "my city";
knudO.phonenumberS = "01-2345678";
knudO.countryS = "my country";
knudO.emailS = "myemail@adress";
//
Console.WriteLine( knudO.nameS );
Console.WriteLine( knudO.addressS );
Console.WriteLine( knudO.cityS );
Console.WriteLine( knudO.countryS );
Console.WriteLine( knudO.phonenumberS );
}
}
--- cut here ---------------------------------------------------------
3. Your program should so contain the 'main' method
4. Save this file with extension .cs
e.g.
TestAddress.cs
5. Compile with
csc.exe TestAddress.cs
6. If compiled successfully this will create
(in the same directory) a file:
TestAddress.exe
7. Run with
TestAddress
8. That will show on the console command line:
Knud
my street 123
my city
01-2345678
myemail@adress
---
Here a typical screen of your console:
+--------------------------------------------------------------------+
|> csc.exe TestAddress.cs |
| |
|Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0|
|Copyright (C) Microsoft Corp 2000-2001. All rights reserved. |
| |
|> TestAddress.exe |
| |
|Knud |
|my street 123 |
|my city |
|01-2345678 |
|myemail@adress |
| |
|> Press any key to continue . . . |
+--------------------------------------------------------------------+
---
Internet: see also:
---
C#: How to create a class: tutorial
http://www.joegrip.com/seeit.html
---
C++: Class: Create: Simple: How to create a class in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/24828/fid/163
---
Delphi: Class: Create: Simple: How to create in a class in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/24789/fid/175
---
Java: Class: Create: Simple: How to create a class in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/24842/fid/165
----------------------------------------------------------------------