marinus's Profile
Reputation: 135
Stalwart
- Group:
- Contributors
- Active Posts:
- 577 (0.51 per day)
- Joined:
- 14-April 10
- Profile Views:
- 5,631
- Last Active:
Oct 19 2011 11:12 PM- Currently:
- Offline
Previous Fields
- Country:
- ZA
- OS Preference:
- Who Cares
- Favorite Browser:
- FireFox
- Favorite Processor:
- Who Cares
- Favorite Gaming Platform:
- Who Cares
- Your Car:
- Toyota
- Dream Kudos:
- 75
Latest Visitors
-
CodingSup3rna... 
02 Feb 2013 - 14:02 -
ProdicalProgr... 
10 Jan 2013 - 07:19 -
ishkabible 
16 Jan 2012 - 13:07 -
Eitxor 
12 Dec 2011 - 15:08 -
darek9576 
25 Nov 2011 - 16:17 -
brep 
20 Oct 2011 - 18:02 -
Ryano121 
10 Oct 2011 - 07:31 -
wiero 
30 Sep 2011 - 07:08 -
Kilorn 
24 Aug 2011 - 12:23 -
SixOfEleven 
23 Aug 2011 - 14:50
Posts I've Made
-
In Topic: Code throwing cast error!
Posted 15 Oct 2011
I got it, Parameters was an struct and not a class . oops! -
In Topic: I Hurt When It Comes To C# Generic Methods? How Important Are They?
Posted 14 Oct 2011
preb, on 14 October 2011 - 01:12 AM, said:So, let's say you had to do some arithmetic with a project you are working on for a company. This program has to have a GUI, and the program should be able to add all the different binary variable types.
Without generics, the following methods can be made:
publice double add(double num1, double num2) { return(num1 + num2) } publice int add(double num1, double num2) { return(num1 + num2) } publice byte add(double num1, double num2) { return(num1 + num2) }
And so on.
But in generics, that can be simplified to (taken from my snippet.):
class GenericOperation<T> where T : struct { public T Add(T num1, T num2) { T result = DynamicAdd(num1, num2); return result; } private static dynamic DynamicAdd(dynamic Num1, dynamic Num2) { return (Num1 + Num2); } }
This gives the advantage of minimizing repetitive code, making your program easier to understand.
Also note that the way you added the integers will probably not work for say an application built in Windows Forms. The example shown above should work in all adding scenarios, GUI, Console, or anything else.
Hope this helps.
preb
Edit: showed code in generic class for clarity.
Don't use dynamic as function parameters , if you do this you would not be able to edit and continue in your hole project
Here is good example
class Program { static void Main(string[] args) { MathTest test = new MathTest(); Console.WriteLine(test.Add(10.4, 12)) ; Console.ReadKey(); //Output 22.4 } } class MathTest:IMath<double> { public double Add(double obj, double obj2) { return obj + obj2; } public double Substract(double obj, double obj2) { return obj + obj2; } } interface IMath<T> { T Add(T obj, T obj2); T Substract(T obj, T obj2); }
Note where MathTest class Will implement from IMath<T> interface i explicitly say double so it implements for a double
T is our type parameter so its sort of a code template . So if i want to do this with a int i would be the following implementation
class MathTest:IMath<int> { public int Add(int obj, int obj2) { return obj + obj2; } public int Substract(int obj, int obj2) { return obj + obj2; } } -
In Topic: Integer Arrays
Posted 10 Oct 2011
An example
static void Main(string[] args) { int []iArr = StringToIntConverter("55344654",true); } public static int[] StringToIntConverter(string strData,bool Reverse) { int[] iArray = new int[strData.Length]; if (!Reverse) for (int i = 0; i < strData.Length; i++) iArray[i] = int.Parse(strData[i].ToString()); else for (int i = 0; i < strData.Length; i++) iArray[i] = int.Parse(strData[(strData.Length - 1) - i].ToString()); return iArray; } -
In Topic: LINQ error ( Unable to cast object of type 'WhereListIterator`1)
Posted 3 Oct 2011
I got it fixed , the code of mine was not well written in the first place and got it fixed now
Thanks -
In Topic: LINQ error ( Unable to cast object of type 'WhereListIterator`1)
Posted 2 Oct 2011
Im not sure what you mean ?
Thanks
My Information
- Member Title:
- D.I.C Addict
- Age:
- 22 years old
- Birthday:
- September 4, 1990
- Gender:
-
- Location:
- Cape Town
- Interests:
- Cricket (-_-)..// ---o & Gaming .
- Full Name:
- LEFTFIELD -D
- Years Programming:
- 2
- Programming Languages:
- C#, ASP.net ,JavaScript ,VB6, SQL, PHP, C++
Contact Information
- E-mail:
- Private
|
|


Find Topics
Find Posts
View Reputation Given

|
Comments
chinchang
01 Apr 2011 - 08:22marinus
03 Mar 2011 - 12:42