Library tutorials & articles
The Quick & Dirty .NET Guide to C#/VB OOP
Quick Structs
Let's now examine C# structs, or structures as they're referred to in VB. Simply stated, these little bitties act and look just like classes, and utilize all the same kinds of modifiers and elements a class would. You could just think of them as light weight, scaled down classes.
Nevertheless, two caveats though: One, is that they are best suited for smaller data value types under 16 bytes - structures like numbers crunching. Thus, they are used with memory efficiency in mind, as their memory allocation is superior to classes. Two, another key difference in comparison to classes is that they cannot inherit from any other classes as any typical class could, nor could they be a base class either. However, they could implement interfaces though. Also, unlike classes, structs could be instantiated without the new keyword is you so choose. Hmm...
Additionally, classes when instantiated within .NET allocate memory on the heap or .NET's reserved memory space. Whereas structs yield more efficiency when instantiated due to allocation on the stack. Furthermore, it should be noted that passing parameters within structs are done so by value.
A brief word on field initialization and access. Typically, a class field could easily be initialized or preset with a value, as shown:
public class House {
public string HseColor = "gray"; //This is known as initializing a field
}
However, this won't work for structs unless the fields are static. Whenever we initialize any fields, they're typically known as instance fields/data, whereas the opposite holds true for fields declared with the static keyword. The main difference between the two is that a static field is accessible anywhere in your application without having to instantiate the class containing it. In VB the equivalent keyword is Shared .
[C#]
public class House or struct House {
public static HseColor = "gray"; //This is known as initializing a field
}
[VB]
Public Class House or Structure House
Public Shared HseColor As String = "gray"
End Structure
Accessing this kind of field is as simple as:
Response.Write (House.HseColor)
Now getting back to our structs. In the House class as shown earlier, changing the "public class House" to "struct House" make's it now a struct like so:
[C#]
struct House {
// ... Rest of code here
}
[VB]
Structure House
' ... Rest of code here
End Structure
Structs are instantiated in your page in the same manner as classes as we've shown. Again, although our listed struct example works fine as is, keep in mind all the aforementioned limitations and key purposes when implementing them in your programs.
Here we'll now quickly overview classes and other members they could contain and or independently behave as:
- Fields or Variables - A value that is associated with and within an object or class. Declared with modifiers such as public, private, static, shared, const (constants), etc.
- Constants - A consistent, unchanged value that is associated with an object or class.
- Properties - Advanced and more adaptable fields, with get and set accessors.
- Methods - Encapsulated code performing an action or storing some logic.
- Classes - A reference type that stores and encloses methods (functions or subroutines), fields, variables or properties, constructors, and even other classes that organize data and perform some kind of functionality.
- Structs - Light weight, scaled down memory efficient classes.
- Constructors - creates an instance of a class whenever the object is created.
- Destructors - destroys the instance of a class.
- Interfaces - Are a set of specifying arrangements with certain characteristics, that when implemented or inherited in a class must abide by. They could contain all mentioned here.
- Events - Are sent notifying something of actions going to or taking place.
- Delegates - Are useful for events and for passing one method or function to another as a parameter. Similar to function pointers in C++.
- Indexers - these allow you to index classes like you would arrays.
- Enums - Also known as enumerations , these are a series of constant and related values.
Sorry, if I blew your mind with all these terms. It's good to have an idea what they do although you may never use all of them. Nevertheless, I thought a good passing insight was in order.
I won't go into mind numbing detail on every single aspect of OOP, nor everything listed above, as this is not the article's intent. Rather the aim is, as aforementioned, to present the reader with a quick and dirty introduction and synopsis on all more important commonly used principles of OOP, so you can get a good overall idea and grasp on it.
As you now have a decent grasp on the methodology of objects, classes, and properties, we can now press on and claim our .NET inheritance. :-)
Related articles
Related discussion
-
hey developers out there
by pitsophera (0 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
-
Creating a Windows Service in VB.NET
by davidvanr (108 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
Related podcasts
-
A Practical Look at Silverlight 2 Part 1
Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...
Events coming up
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
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;}
}
This thread is for discussions of The Quick & Dirty .NET Guide to C#/VB OOP.