Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. using explicit conversion operators for arrays?

using explicit conversion operators for arrays?

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SimonS
    wrote on last edited by
    #1

    I'm trying to establish a good way of doing something before finalising my codegen. Below is an example:

    private void Form1_Load(object sender, EventArgs e)
    {
    Employee emp = ReallyCoolFuncSingle();
    //MessageBox.Show(emp.Age + " " + emp.BasePerson.PersonName);
    }

        public Employee ReallyCoolFuncSingle()
        {
            Employee e = new Employee();
            Person p = CoolFuncSingle();
    
            e = (Employee)p;
            e.Age = "100";
            
            return e;
        }
        //public Employee\[\] ReallyCoolFuncArray()
        //{
        //    Employee\[\] e;
        //    Person\[\] p = CoolFuncArray();
    
        //    e = (Employee\[\])p;
        //    //e.Age = "100";
    
        //    return e;
        //}
    
    
        public Person CoolFuncSingle()
        {
            Person p = new Person();
            p.PersonName="billy bob";
            return p;
        }
        public Person\[\] CoolFuncArray()
        {
            Person\[\] p = new Person\[2\];
            p\[0\] = new Person();
            p\[1\] = new Person();
            p\[0\].PersonName = "billy bob";
            p\[1\].PersonName = "frank";
    
            return p;
        }
    
    
    
        public class Person
        {
            private string \_PersonName;
    
            public string PersonName
            {
                get { return \_PersonName; }
                set { \_PersonName = value; }
            }
    
            public static explicit operator Person(Employee e)
            {
                Person p = new Person();
                p.PersonName = e.PersonName;
                //e.Age = 100;
                return p;
            }
    
        }
        public class Employee// : Person
        {
            //private Person \_BasePerson;
    
            //public Person BasePerson
            //{
            //    get { return \_BasePerson; }
            //    set { \_BasePerson = value; }
            //}
    
            #region person stuff
            private string \_PersonName;
    
            public string PersonName
            {
                get { return \_PersonName; }
                set { \_PersonName = value; }
            }
            #endregion
    
            private string \_Age;
    
            public string Age
            {
                get { return \_Age; }
                set { \_Age = value; }
            }
    
            public static explicit operator Employee(Person p)
    
    R 1 Reply Last reply
    0
    • S SimonS

      I'm trying to establish a good way of doing something before finalising my codegen. Below is an example:

      private void Form1_Load(object sender, EventArgs e)
      {
      Employee emp = ReallyCoolFuncSingle();
      //MessageBox.Show(emp.Age + " " + emp.BasePerson.PersonName);
      }

          public Employee ReallyCoolFuncSingle()
          {
              Employee e = new Employee();
              Person p = CoolFuncSingle();
      
              e = (Employee)p;
              e.Age = "100";
              
              return e;
          }
          //public Employee\[\] ReallyCoolFuncArray()
          //{
          //    Employee\[\] e;
          //    Person\[\] p = CoolFuncArray();
      
          //    e = (Employee\[\])p;
          //    //e.Age = "100";
      
          //    return e;
          //}
      
      
          public Person CoolFuncSingle()
          {
              Person p = new Person();
              p.PersonName="billy bob";
              return p;
          }
          public Person\[\] CoolFuncArray()
          {
              Person\[\] p = new Person\[2\];
              p\[0\] = new Person();
              p\[1\] = new Person();
              p\[0\].PersonName = "billy bob";
              p\[1\].PersonName = "frank";
      
              return p;
          }
      
      
      
          public class Person
          {
              private string \_PersonName;
      
              public string PersonName
              {
                  get { return \_PersonName; }
                  set { \_PersonName = value; }
              }
      
              public static explicit operator Person(Employee e)
              {
                  Person p = new Person();
                  p.PersonName = e.PersonName;
                  //e.Age = 100;
                  return p;
              }
      
          }
          public class Employee// : Person
          {
              //private Person \_BasePerson;
      
              //public Person BasePerson
              //{
              //    get { return \_BasePerson; }
              //    set { \_BasePerson = value; }
              //}
      
              #region person stuff
              private string \_PersonName;
      
              public string PersonName
              {
                  get { return \_PersonName; }
                  set { \_PersonName = value; }
              }
              #endregion
      
              private string \_Age;
      
              public string Age
              {
                  get { return \_Age; }
                  set { \_Age = value; }
              }
      
              public static explicit operator Employee(Person p)
      
      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Although I don't have a clue what you want to accomplish with this weird code (why not just let Employee be a subclass of Person?) I'll try to help you: It won't work for arrays :). The problem is that there is no way to defined operators for arrays of a given type. But you can nevertheless transfer its elements one by one to a new array:

      Person[] persons = GetMyPersonsArray();
      Emplyee[] employess = new Employee[persons.Length];
      for (int i = 0; i < persons.Length; i++)
      employess[i] = (Employee)persons[i];

      -- modified at 4:23 Sunday 14th May, 2006

      L 1 Reply Last reply
      0
      • R Robert Rohde

        Although I don't have a clue what you want to accomplish with this weird code (why not just let Employee be a subclass of Person?) I'll try to help you: It won't work for arrays :). The problem is that there is no way to defined operators for arrays of a given type. But you can nevertheless transfer its elements one by one to a new array:

        Person[] persons = GetMyPersonsArray();
        Emplyee[] employess = new Employee[persons.Length];
        for (int i = 0; i < persons.Length; i++)
        employess[i] = (Employee)persons[i];

        -- modified at 4:23 Sunday 14th May, 2006

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Robert Rohde wrote:

        The problem is that there is no way to defined operators for arrays of a given type.

        But others can reference array parameters :) ie

        public static explicit string[] operator (Employee e){}

        PS: I allways forget the syntax, could wrong :p**

        xacc.ide-0.1.3.12 - Now a whole lot faster (and better)
        Consolas size screenshots (see how fractional font sizes look)

        **

        R S 2 Replies Last reply
        0
        • L leppie

          Robert Rohde wrote:

          The problem is that there is no way to defined operators for arrays of a given type.

          But others can reference array parameters :) ie

          public static explicit string[] operator (Employee e){}

          PS: I allways forget the syntax, could wrong :p**

          xacc.ide-0.1.3.12 - Now a whole lot faster (and better)
          Consolas size screenshots (see how fractional font sizes look)

          **

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Yes, but either the return value or the parameter must exactly match the class which is defined in. Thus in your case either return type or parameter type must be Employee and so something like public static explicit Person[] operator (Employee[] e){} is not possible (neither return type nor parameter type is Employee).

          1 Reply Last reply
          0
          • L leppie

            Robert Rohde wrote:

            The problem is that there is no way to defined operators for arrays of a given type.

            But others can reference array parameters :) ie

            public static explicit string[] operator (Employee e){}

            PS: I allways forget the syntax, could wrong :p**

            xacc.ide-0.1.3.12 - Now a whole lot faster (and better)
            Consolas size screenshots (see how fractional font sizes look)

            **

            S Offline
            S Offline
            SimonS
            wrote on last edited by
            #5

            this is the route I want to go. More like (pseudo code):

                    public static explicit operator Person\[\] Employee(Person p)
                    {
                        Employee e = new Employee();
                        e.PersonName = p.PersonName;
                        return e;
                    }
            

            So, the Employee object knows how to cast from Person[] to Employee[]. I could go the route of a static function on Employee, but was hoping for something a little more correct. Cheers, Simon > blog:: brokenkeyboards > what I think of the OPTIONAL keyword in VB.NET? :: here > CV :: PDF > skype :: SimonMStewart

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups