using explicit conversion operators for arrays?
-
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)
-
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)
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
-
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
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)**
-
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)**
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). -
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)**
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