473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

assign operator in c#

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
May 14 '07 #1
3 2771
On May 14, 5:55 pm, zhanglr <zhan...@discussions.microsoft.comwrote:
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

May 14 '07 #2
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

"Moty Michaely" wrote:
On May 14, 5:55 pm, zhanglr <zhan...@discussions.microsoft.comwrote:
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

May 14 '07 #3
On May 14, 10:38 am, zhanglr <zhan...@discussions.microsoft.com>
wrote:
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;

May 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Rim | last post by:
Hi, I have been thinking about how to overload the assign operation '='. In many cases, I wanted to provide users of my packages a natural interface to the extended built-in types I created for...
3
by: Tony Johansson | last post by:
Hello! Assume you have a constructor for class AccountForStudent defined in this way AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s), balance_(balance) {} //Here ...
18
by: ineedyourluvin1 | last post by:
Hi, I would appreciate if someone could tell me what I'm doing wrong ? #include<iostream> using namepace std ; struct person{ char *firstname ; int age ;
10
by: John | last post by:
Ok. I'm not sure whether this is cool or perverted, I need second opinion ;) I define two classes as follows: /********************************************/ public sealed class Dummy {...
7
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
4
by: kotau | last post by:
Hi, I'm having trouble with something that would appear to have a simple solution. Here's a version of the code I'm working with: const Item* p 0; name::const_iterator i;
4
by: Avi | last post by:
Hi I am creating web application in which i want to assign by default values to the property which i had created my own. In that one of the property is of type color and i am unable to assign...
4
by: Chris Roth | last post by:
vector<doublev1(5,1); vector<doublev2; v2 = v1; // 1 v2.assign(v1.begin(),v1.end()); // 2 Are 1 and 2 the same, or are their subtle differences between them. Which is preferable, if either? ...
29
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.