Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 11, 2008 10:00 PM by SGWellens
Aug 11, 2008 06:04 PM|LINK
Is this a way of boxing? Can you please explain what it's is please? This code is inside a method which contains parameters.
string n1 = ((ClassName)MethodParam).ClassMember;
Aug 11, 2008 10:00 PM|LINK
Is this a way of boxing?
MethodParam is a base class type. However, what is being passed is a derived type.
To get to the derived type member, first MethodParam is cast to type ClassName, then ClassName's properties are available.
The parenthesis are needed because the dot operator has higher precedence than the cast operator.
Example: Say there is a base class called Animal and a class called Dog that derives from it. Not all Animals have Collars but Dogs do...so:
String Collar = ((Dog)Animal).Collar;