26 March 2012

Introduction to OOP #Part 3

In this part, I'll continue this post , so our class has started to have its own structure and to take his own form (mask) and it usable now, let's see our class:

class Person
{
  string FirstName {get; set;}
  string LastName {get; set;}


    void Sleep(int hours)
    {
     // body

    }
}


This class can be used in our first example with first and last names in more flexibility way, let's add one additional 'function', a function differs from a method while a method doesn't return any result of any dataType because it is marked with "void", the function returns any type of result (like 'int' in this example):



int CalculateAge(int BirthYear)

{
  return DateTime.Now.Year - BirthYear;
}



Our function does one simple operation, it return our age by passing just the year of birth, it does a simple calculation - the DateTime class comes from .Net classes and has also his own properties, methods and functions defined.

Modifiers - Before we can use our class we need to know about modifiers, modifiers are something like access point keys (or protection levels) of methods, properties, functions..etc. I'll for now just shortly explain two very common modifiers: "private" and "public", those two keywords are placed before the property, method, function..etc.. if it isn't added it is marked internally as "private", Let's see by just adding "private" keyword before the 'string':


private string myPrivateString = "hello";


With "private" keyword or modifier you can access for instance this variable only within the declaration scope, I'll write more about scopes and modifiers in coming posts.
With "public" keyword we can have access to the variable, method, function..etc. through our instance of the object, Let's see one example:

public string myPublicString = "hello there";



What's next?
Because of my time limit I'm not able to continue this post for now, maybe in the coming posts - and for next I'll try to cover more about classes and their common usages. Thanks for your time comments are welcome, you can also share this content or invite friends to follow my posts, feel free by providing ideas of getting better or asking questions.

19 March 2012

Introduction to OOP #Part2

In my last Post I described in some way what a class is and how it is defined approximately, I will continue with defining a class definition and methods in and outside a class,

class Person
{
string FirstName {get; set;}
string LastName {get; set;}
}



Methods
So consider a method something like an action that is done into application logic, for example a method could be Sleep() for the current Person class, that's an logical action that applies inside an entity in our case the Person class, a method is contained with some part like this bluePrint:

void MethodName(aType myparameter)
{
// body
}

The method as we can see is contained with a 'Method name' section, 'Parameter' section and the 'body' section, the method name section we use for call or raise the method, the parameter(s) we can use for doing several actions in several conditions (it really depends what we want to achieve) and at last the body section is the main part, there we define our logic. This method can be placed into our Person class and if we do that, the Person class is able to call that method from an instance, an instance is simple a class declaration with the new keyword. For the parameters we are able to add as many parameters as we need, but it isn't a good practice to pass much parameters into a method, a simple rule when we can write our own method is: "If we need some kind of procedure or code logic to repeat the same action(s) over and over again, we define our method and just call it." I will rewrite the person class with a method:

class Person
{
  string FirstName {get; set;}
  string LastName {get; set;}


    void Sleep(int hours)
    {
     // body

    }
}


The class above has now a method called 'Sleep', with a parameter 'hours' of type 'int', logically if we call this method and pass for example value 3 as the parameter value, the person will sleep for 3 hours (nice Idea for now :D). Declaration of a class is really simple, a declaration means simple 'making use of an type' and call/use his 'functions (properties,methods,events,delegates...etc)', I will declare the Person class and make a call of the Sleep Method:



// the declaration of the class

Person p = new Person();
p.Sleep(3); // the method calling code



That was simple, right? what here is done is simple, we have made a use of the person class and call his sleep method.

What's next?
In the next post I will try to expand the example of making use of a class with name and last name, feel free to comment or to share the content.

17 March 2012

Introduction to OOP

So hi to all readers,  I'm Flamur my nickname is Muli
My first post will cover the topic 'Intro to OOP' using C# programming language, C# is used very much today and is fully OOP ( Object Oriented Programming) language, I use Visual Studio 2010 as my IDE, you can use whatever you want. I will start with the explanation of simple classes, some properties and some methods. So before start 'throwing' code here, I will try to explain first what a class is?

Class - A class in OOP is considered one logical entity or object that shows a 'blueprint' of the object itself that has some custom attributes (properties), custom methods (actions), custom events and custom nested types. In short a class is a information entity that can hold custom data, fire (raise) custom events and contain methods in a structural way.

Why to use Classes?
  One simple reason is to keep data structured, assume you have some lines of codes that display to the console (like command prompt) a first and last name of a specific person how would you do it without a class?

....
string firstName = "myFirstName";
string lastName = "myLastName";
....
Console.WriteLine(firstName + " " + lastName);


The code above is a solution of achieving to write a first and last name on the console application, but this applies for one person in this instance for person "myFirstName", imagine it for 30 persons? OK no way to declare 30*2 variables for just 30 persons first and last names (that's more an procedural programming than object oriented), but you can use the same variables (firstName and lastName) and just to change their values, after changing values you could write to the console, but this is still procedural programming rather than object oriented programming, ok it is not a 'must' to use classes but thinking on one question, expect first and last name if you want to add later Age, BirthYear or any other additional attributes how will the code look like? It will grow very much and it would be very difficult to maintain all the static unstructured 'data'.

Class example:

class Person
{
string FirstName {get; set;}
string LastName {get; set;}
}


The class above is very very simple one, it contains just two fields (attributes, properties) the first and last name with a dataType of 'string' (text).

What's next? 
 I will try to explain more about classes  in the next coming posts, I do this only on my free time till then happy days and feel free to comment or share the content.