0

I have three classes. A B and C. A and B both derived from C (C has OAuth properties), and A and B both have different properties other than C properties

I have a function that sets the OAuth properties of both A and B, but because they are different types I pass in an object through the method. I then check to see if A is A and B is B, but I don't know where to set the OAuth properties (i don't want to duplicate my code)

public string SerializeForAPI(object myObject)
{
    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

    if (myObject is A)
    {
        A data = (A)myObject; 
    }
    else if (myObject is B) 
    {
        B data = (B)myObject;
    }
    // these are obviously not set because the object is within the if statement
    data.oauth_consumer_key = this.ConsumerKey;
    data.oauth_token = this.Token;

    string serialized = jsonSerializer.Serialize(data);
    ...
}
2
  • 1
    Create a function that takes a C, and pass your A object or B object? Commented Jan 16, 2014 at 16:45
  • I could do that, but within that function that I pass the A object or the B object, I will still have to convert them to a certain type Commented Jan 16, 2014 at 16:59

2 Answers 2

3

Just cast it to a C, if you know that the myObject will always be a subclass of it:

C data = (C) myObject;
data.oauth_consumer_key = this.ConsumerKey;
// etc.
Sign up to request clarification or add additional context in comments.

7 Comments

BTW, you do not need to explicitly cast to a base type. (C) is not necessary.
@Dmitry Sure it is, because myObject has type object ... look at the OP code again.
ah, sorry... my bad 8)
but will the casted object with all the properties of class A carry over? For example: if A has a property called FirstName, but C does not, how will the objects be carried over?
@user3192012 Not sure what you mean. You won't be able to access the A-specific properties of the object via data (you'd have to check it with is in a conditional and set them there, as in your original post).
|
0

The method should accept an object of type C, not an object of type object.

public string SerializeForAPI(C myObject)

Now you can pass in either an A or a B instance to this method, and the method can be sure that anything passed into it will have the oauth_consumer_key and oauth_token members, so this method can do everything that it needs with this type. By not having a parameter of type object it's not even possible to pass in an object that won't work.

3 Comments

but if I am setting the properties of A and B before calling SerializeForAPI, how do I keep those properties?
@user3192012 They never go away. That this method is treating the object as its base class doesn't mean it doesn't also have all of the fields from the more derived class.
Ah you are correct! as B is a part of C, sending B as a C still keeps the B properties. that is the trouble I had, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.