stovellp Posted March 25, 2004 Posted March 25, 2004 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; } } Quote
Administrators PlausiblyDamp Posted March 25, 2004 Administrators Posted March 25, 2004 In C# the assignment operator (=) cannot be overloaded. What are you trying to do? Is your class encapsulating the DataReaders and adding extra functionality? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stovellp Posted March 25, 2004 Author Posted March 25, 2004 Yep. I want to be able to do: MyResultType result = sqlDataReader; or result = oledbDataReader; or result = myresulttype; Quote
Administrators PlausiblyDamp Posted March 25, 2004 Administrators Posted March 25, 2004 How about using the IDataAdapter interface? IDataAdapter result = sqlDataReader; //or result = oleDbDataReader; Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stovellp Posted March 25, 2004 Author Posted March 25, 2004 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. Quote
Administrators PlausiblyDamp Posted March 25, 2004 Administrators Posted March 25, 2004 Could you not make your class implement IDataAdapter instead then? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stovellp Posted March 25, 2004 Author Posted March 25, 2004 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 :) Quote
Arch4ngel Posted March 25, 2004 Posted March 25, 2004 (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 March 25, 2004 by Arch4ngel Quote "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* Nerseus Posted March 25, 2004 *Experts* Posted March 25, 2004 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.