conversion operator() ??

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

I try to assign an object of MyDate to an object of Person but don't know
well how ? (see code below)

The way I have implemented it so far will assign a MyDate-object to a new
Person-object, not to an already existing one.
I'm stuck.
Who can help me ?
thnx

Chris
******************************************
class MyDate
{
public int m_day, m_month, m_year;
public MyDate(int d, int m, int y)
{
m_day = d;
m_month = m;
m_year = y;
}
public override string ToString()
{
return m_day + "/" + m_month + "/" + m_year;
}
}
class Person
{
public string m_firstname;
public string m_lastname;
public MyDate m_birthdate;

public Person(string first, string last)
{
m_firstname = first;
m_lastname = last;
}

public Person(MyDate date)
{
m_birthdate = date;
}
public static implicit operator Person(MyDate date)
{
return new Person(date);
}
}

class App
{
public static void Main()
{
MyDate dt = new MyDate(10,10,1970);
Person annie = new Person("Annie", "Janssen");
annie = dt; // will assign to a new object ,NOT to existing one
}
}
 
Hi Chris

See my lines at the end.
Chris
******************************************
class MyDate
{
public int m_day, m_month, m_year;
public MyDate(int d, int m, int y)
{
m_day = d;
m_month = m;
m_year = y;
}
public override string ToString()
{
return m_day + "/" + m_month + "/" + m_year;
}
}
class Person
{
public string m_firstname;
public string m_lastname;
public MyDate m_birthdate;

public Person(string first, string last)
{
m_firstname = first;
m_lastname = last;
}

public Person(MyDate date)
{
m_birthdate = date;
}
public static implicit operator Person(MyDate date)
{
return new Person(date);
}
}

class App
{
public static void Main()
{
MyDate dt = new MyDate(10,10,1970);
Person annie = new Person("Annie", "Janssen");
annie = dt; // will assign to a new object ,NOT to existing one
}
}

Yes, that's right. You convert dt to class person. It doesn't know wnything
about "annie" how do you expect the type casting operator to set the annie's
birthday.
I don't see any reason to have that casting operator. It doesn't make sense
to me to convert from date to Person because the relation between person and
date in your case is one to many. One person has exactly one birthday
(unfortunaley :-) ), but there 0 or more people born in given date. So you
can convert from person to date but not vice versa.
What you might wanna do is to declare MyDate as a structure (I believe
struct is more appropriate for this type (see DateTime in the framework it
is a structure)) and expose person's birthday as a read/write property.

You can do then:
class App
{
public static void Main()
{
MyDate dt = new MyDate(10,10,1970);
Person annie = new Person("Annie", "Janssen");
annie.Birthday = dt;
}
}

Even you can add birthday to the constructor parameters list as long as
there is no person without a birthday

Person annie = new Person("Annie", "Janssen", new MyDate(10,10,1970));
 
Back
Top