Library articles and tutorials
The Quick & Dirty .NET Guide to C#/VB OOP
A Class in Objects
Simply put, a class is a created object . In more detail, a class is a reference type (that which stores some type of information) that encapsulates (hides or encloses) and or modularizes (making for flexible, varied use) any amount of methods (functions or subroutines), fields, variables or properties or constructors (object creating function) that classifies data and performs some kind of functionality. Thus, the focal point of OOP is to classify all common functionality and members within a main class that when utilized or instanced in your application, it becomes an object . Object creation is achieved by using the new keyword in your code, as we'll see.
The examples we'll mostly focus will revolve around the House class and its key traits/characteristics. Now if we had a house class and it contained its pertinent house characteristics ( fields or variables) it would look something like this. So go ahead and place your preferred language code section in a text file. Next save it as House.cs if using C# or House.vb for VB. This is your source file that you'll compile in the next section.
[C#]
public class House {
// Its members
// This is known as initializing a field - see the "Quick Structs" section further down
// public string HseColor = "gray";
public string HseBuilder;
public string HseSize;
public string HseColor;
public string Summary() {
return HseBuilder + " is building a new house that's " + HseSize + " and is painting it " + HseColor;
}
}
[VB]
Public Class House
Public HseBuilder As String
Public HseSize As String
Public HseColor As String
Public Function Summary() As String
Return HseBuilder &" is building a new house that's " & HseSize &" and is painting it " & HseColor
End Function
End Class
The bits of information we include above in our House blueprint is who the builder is, the house's size and its color. Further enhanced by a summary entailing overall what's included and taking place within our House class.
Therefore, even though this is one template describing one house, it could easily represent and be utilized for any number of houses since each of its public properties values or characteristics may be different each newly created instance of it. In the example above, our diverse information is contained in our public variables or fields that hold what we pass to them in memory. Our variables and method string declaration is known as a data type. In VB the As keyword specifies the data type, in C# the data type precedes the variable name and or method.
The term for using a class, as many times as you wish, each time with different characteristics, is called instantiation or creating an object for use. So the first time the house class is instantiated and assigned it can hold the properties for example, HseSize of "Big" and an HseColor of "Red", and when re-instanced could hold some other very different information. So out of our one House class, we've build two houses all with different characteristics; one class - many uses.
Variables as we've seen have the ability to hold one set value. Class properties unlike their close relatives variables, as we'll examined a little further down, extend the functionality and pretense by allowing you to carry out conditional checking within them preventing any faulty values passed in. Therefore, you can do much, in turn furthering the nature and objective of OOP, as we'll soon examine in the next section.
All we've discussed still needs to be demonstrated real world. Therefore, we'll now build two houses, each with its own particular qualities. So here goes.
Related articles
Related discussion
-
Creating a Windows Service in VB.NET
by codet (102 replies)
-
VB.NET or C#?
by siddeshwar (3 replies)
-
An Introduction to VB.NET and Database Programming
by yen (12 replies)
-
VB/VB.NET
by surath (7 replies)
-
Adding carriage returns to text file
by Andrew_B (4 replies)
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum)
Events coming up
-
Oct
14
What’s New in Visual Studio 2008 Service Pack 1?
Birmingham, United Kingdom
“Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.
hello,
I am very impressed by your tutorial as it finally allowed me to grasp the syntax behind OOP programming with .net.
Only what I did not understand is how and where do I complile the .cs to a dll? I cannot do it on the server. Do I do it on my local computer and then upload?
http://www.developerfusion.co.uk/show/4341/5/
Honestly I believe that the basic idea of the OOP was really great, but to be able to use it one really has to have the head as a water melon. There is too much theory, too many therms and the class theory really feels like puting each part of the program into a separate box and then figuring out how to drill a way between them. I started to learn C++ OOP at least 20 time and after a couple of weeks I did not even had an idea what it is about (note: I am not a proffesional programmer)...
I believe that: "The use of a programming language should be as simple as a pie and the algorithm should be the part where people spend the most of their time..."
When I do C# programming I do all public and it works great for me. Simply keep it simple!!!
Is there any web page or book where I could find how to do Non-OOP C# programming?
Sincerely,
Gabor Gorcsos
Though from personal experience I'd say 99.99% of the time, you'd want private member variables and public properties...
Hi Ehx,
That's true, and it's funny that in all my other articles I always write all private variables with public properties. i.e. - Building a Full-Featured Custom DataGrid Control. It's just one of those overlooked things. Oh well.
After reading your article, http://www.developerfusion.com/show/4341/6/
I got realy confused!!
from what I know from the book below, I declare private property, then declare public (get , set )
What you presented in your article is you declared public property,then made the (get,set) private)
I have put this simple comparison code , so please advice your point, What is the logic behind reversing the modifier(public to private and vis versa).
thanks
Ehx
// what is in the book (Begining Asp.Net Database using C# p 321)
private string Country;
public string Country
{
get{return _Country;}
set{Country= value;}
}
// what is in web article ( in your article)
public string Name;
private string _Name
{
get{return Name;}
set{Name= value;}
}