marinus's Profile User Rating: -----

Reputation: 135 Stalwart
Group:
Contributors
Active Posts:
577 (0.51 per day)
Joined:
14-April 10
Profile Views:
5,631
Last Active:
User is offline 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

Icon   marinus spends 80% of the time on a solution and 20% time on the problem

Posts I've Made

  1. In Topic: Code throwing cast error!

    Posted 15 Oct 2011

    I got it, Parameters was an struct and not a class . oops!
  2. In Topic: I Hurt When It Comes To C# Generic Methods? How Important Are They?

    Posted 14 Oct 2011

    View Postpreb, 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;
            }
        }
    
  3. 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;
            }
    
    
  4. 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
  5. 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

Comments

Page 1 of 1
  1. Photo

    chinchang Icon

    01 Apr 2011 - 08:22
    No...I play soccer. dats y :)
  2. Photo

    marinus Icon

    03 Mar 2011 - 12:42
    Mining the cap sins 1996 !
Page 1 of 1