assign operator in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have two classes. Base and Derived. I want to call assign operator in
function f in Derived

In C++, I can do it like following -- void Derived::f(Base obj){ *this =
obj;}.

Now in c#, I write a function SetData(Base obj) to do it. But I am wondering
whether there is similar way (or standard way) in C# to do it.

Best Regards,
Michael zhang
 
Hi,

I have two classes. Base and Derived. I want to call assign operator in
function f in Derived

In C++, I can do it like following -- void Derived::f(Base obj){ *this =
obj;}.

Now in c#, I write a function SetData(Base obj) to do it. But I am wondering
whether there is similar way (or standard way) in C# to do it.

Best Regards,
Michael zhang

Hi Michael,

What do you want to achieve? Do you want the base class to point to
other class?

Moty
 
Hi,

I want to copy data from obj to this in void Derived::f(Base obj). I wonder
whether there is any standard way (override a function or operator) to do it.

Best Regards
Michael zhang
 
Hi,

I want to copy data from obj to this in void Derived::f(Base obj). I wonder
whether there is any standard way (override a function or operator) to do it.

Best Regards
Michael zhang

In C# you have two standard choices, but both of them involve doing
this when the derived object is first created:

1) A copy constructor: public DerivedClass(BaseClass baseData) { ... }

2) A copy constructor in combination with a cast overload, so that in
your code you can simply say:

BaseClass b = new BaseClass(...);
DerivedClass d = b;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top