Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ok this is an easy one.

 

How do I create a custom assignment operator for my own classes in C#? The types to be assigned to my class are DataReader classes (for SQL and Access) so my class may act as a wrapper for them.

 

Something like

class Myclass
{
 operator = (accessdatareader adr)
 {
   mydatareader = adr;
 }
} 

Posted
Because I have a wrapper to make it easier to use the Data Adapters, and I also want to extend it into XML, my own config files, MySQL and other data sources, so if I used IDataAdapter I would be more limited.
Posted

Lol, currently I'm just using functions such as AssignMSSQL(sqlDataAdapter);, I'll just keep it like that.

 

Is there a reason why the C# creators chose not to allow assignment overloading? I absolutely love C#, it kicked C++ off my favorite language shelf after only a month of using it, so I'm sure they have a valid reason for doing this :)

Posted (edited)

I don't know why they can't but...

For your problem... maybe you could use a property to get the result ?

No ? Don't have to overload the assignement operator and got the job done.

Edited by Arch4ngel

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

  • *Experts*
Posted

You can overload assignment though it's syntax isn't quite the same - you don't overload the "=" operator.

 

Here's a sample for how to do it:

public static implicit operator MyResultType(DataReader op)
{
return new MyResultType(op);
}

 

This allows assigning a variable of tyep MyResultType to a variable of type DataReader. In my sample, I assume you have a constructor on

MyResultType that takes a DataReader and does something useful with it.

 

You can replace implicit with explicit if you need to. Check the help on more info.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...