I have a class Card. I have overloaded the + operarator such that +(Card Card, int X). Can I use this + in the Card constructor? For example I want to add 5 to each card as it is being constructed. "this" doesn't seem to work.
Why are you overloading the addition operator? This is generally considered a bad thing, as it results in behavior which other people may not expect. I would suggest writing an Add method instead, as that is more intuitive.
Also - not all .net languages support operator overloading, so if CLS compliance is one of your design goals, you'd need to add an alternate method anyway.
And there are two possible reasons why it's not working in the constructor:
1. Operators are always public static yet depend on instance data.
2. The instance you're trying to add cards to (this) hasn't been fully constructed at that time.
Chip H.
____________________________________________________________________ If you want to get the best response to a question, please read FAQ222-2244 first
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.