I never saw the use of object programming until we could make an object with a sql query.
Of course it is because i never had a program that needed it.
Doing a class with a get and set (wich is simply a variable with extra code) is superfluous, because you can do that with a variable in procedural mode. Without having to include your sub class in your main class and typing 10 lines of extra code just to have an object as a variable.
Most poeple create objects instead of variables and think it's cool. But what about the extra time you charge your client just so your variable are now objects, but the need for it was not there?
Programmer analyst must be analyse first. Do you need an object for a simple variable never used again? That do not need parent property? That does not have childrens?
So a procedural code with a variable is: (csharp)
--- code start csharp visual studio console app -------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace application_obejct_test
{
class Program
{
static void Main(string[] args)
{
string variable01 = "hello";
Console.WriteLine(variable01);
variable01 = "not hello";
Console.WriteLine(variable01);
Thread.Sleep(3000);
}
}
}
--- end code ---
Now let's the same with an object
--- code start csharp visual studio console app -------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace application_obejct_test
{
class Program
{
static void Main(string[] args)
{
string01 variable01 = new string01();
variable01.text01 = "hello";
Console.WriteLine(variable01.text01);
variable01.text01 = "not hello";
Console.WriteLine(variable01.text01);
Thread.Sleep(3000);
}
}
public class string01
{
// here i will not give the same name to the field and the property with a case difference (uppercase the first letter)
// it's just a pain to always use the SHIFT key to type uppercases letters
private string text02; // field, just a variable used to get and set value
public string text01 // property, the thingy after the . in the name of your new object
{
get
{
return text02;
}
set
{
text02 = value; // value is an object property, it's always there
}
}
}
}
------- code end --------
Now, as you can see, for a simple string, making an object to manage it, is really of no use
Poeple doing that with a string, have too much time to loose
BUT, object can be much more
You can validate the length before setting the value of the string object
That changes things, because your object now free you of validating every input from your client
But it would still cut the value without warning...
To add a warning you have to manage error that can happen in your object
--- code start csharp visual studio console app -------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace application_obejct_test
{
class Program
{
static void Main(string[] args)
{
string01 variable01 = new string01();
variable01.text01 = "hello";
Console.WriteLine(variable01.text01);
variable01.text01 = "not hello";
if (variable01.errorcode01 == 99)
{
Console.WriteLine("ERROR " + variable01.errorcode01);
Console.WriteLine("ERROR " + variable01.errormessage01);
Console.WriteLine(variable01.text01);
}
else
{
Console.WriteLine(variable01.text01);
}
Thread.Sleep(3000);
}
}
public class string01
{
// here i will not give the same name to the field and the property with a case difference (uppercase the first letter)
// it's just a pain to always use the SHIFT key to type uppercases letters
private int err01;
private string err02;
private string text02; // field, just a variable used to get and set value
public string text01 // property, the thingy after the . in the name of your new object
{
get
{
return text02;
}
set
{
// validate the value before assigning it to the object
int maxvalue01 = 7;
if (value.Length > maxvalue01)
{
text02 = value.Substring(0, maxvalue01);
err01 = 99;
err02 = "string was cut";
}
else
{
text02 = value; // value is an object property, it's always there
}
}
}
public int errorcode01
{
get { return err01; }
}
public string errormessage01
{
get { return err02; }
}
}
}
--- code end ---
Until now, the difference is we can get and set the object variable WITH a validation on the set
We could do this with a struct passed to a function byref, but we would have to include the action to do on each variable in the struct.
So the difference with an object variable, is the actions are defined in it, and the validation too.
But can we access the object variables dynamically? (by name)
Yes we can:
--- code start ---
// set with dynamic name
typeof(string01).GetProperty("text01").SetValue(variable01, "hello");
// get with dynamic name
object value01 = typeof(string01).GetProperty("text01").GetValue(variable01);
Console.WriteLine(value01);
--- code end ---
Of course, without hardcoding your object, you loose the warning the compiler will give you if you try to give a bad type of value to your object (string to an int etc.), but that is for amateur programmers. Real pro would trap that inside the object, and validate any get or set with an error returned by the object.
That's it for level 1 object programming.