0

My function receives a parameter 'value', of type object. I want to assign it to a member of an object, but the cast doesn't happen implicitly, so I need to specify it explicitly. However, I don't want to actually specify the current type of that member in the object, so I could do this:

positiveOrder.OrderID = (int)value;

But, if business requirements change and OrderIDs are generated and stored in a different way, including a type change to Strings for GUIDS for example, I'll have to come back here and change it, which is undesirable cohesion. I've experimented with various permutations of code like

positiveOrder.OrderID = value as thisOrder.OrderID.GetType();

or

positiveOrder.OrderID = (typeof(thisOrder.OrderID)) value;

But nothing seems to be working. Any ideas on programatically specifying the type to convert to? It can be done at compile time or run time, since the Order class doesn't change.

Neil
  • 3,100
  • 5
  • 29
  • 36
  • 2
    "if tomorrow I change the Order class to save its OrderID member as String instead" why would you do this? And how does this affect the type of 'value' (could that also change)? – Codesleuth Jul 06 '12 at 12:44
  • value is always sent in as an object, by code I don't control. That code is unaware of my implementation details, which is why it sends it leaves the casting to me. I might have to change the type of OrderID according to changing business requirements. Right now I'm using simple incremental integers in the mockup, but in the final application I might use strings instead, for example. I'd just like to not repeat myself by specifying the type of OrderID twice, so that I don't have to change it twice if I ever have to change it. This is more of a good design question than a functional problem. – Neil Jul 06 '12 at 17:04

2 Answers2

2

If positiveOrder.OrderID will always be an integer (even if represented in a string), don't change the member to be anything but an integer. Instead, expose some method that can deal with different source types.

For example:

public void SetOrderID(object value)
{
    this.OrderID = Convert.ToInt32(value);
}

See Convert.ToInt32().

Codesleuth
  • 10,321
  • 8
  • 51
  • 71
  • Thanks codesleuth, but as you'll see from my comment on the question, this isn't quite the change I want to be prepared for. I just want this casting to be agnostic of the underlying type of OrderID, which can change as it requires, to whatever type and kind (text, number, GUID). But yes, if the actual IDs were going to remain the same, just the physical representation was changing, then converting them back to ints would be the right way to go. – Neil Jul 06 '12 at 17:09
  • Actually this made me think, and you're right, I could include a setter function in the Order class that does the casting. It'll still be mentioning the type of OrderID twice, but atleast it'll be in the same class! I found methods that should convert between types, but they still need casting for some odd reason (Convert.ChangeType). I think this solution is probably the least overhead for implementing a fix to something that ain't broken. – Neil Jul 06 '12 at 17:26
0

Something like:

using System.Reflection;

receiver.GetType().GetProperty(propName).SetValue(receiver, value, null);
Thom Smith
  • 13,916
  • 6
  • 45
  • 91
  • Thanks, I didn't quite get what your code was doing at the start, but it gave me good google fodder to reach this question: http://stackoverflow.com/questions/7616177/convert-to-a-type-on-the-fly-in-c-net, which seems to be what I need, going to try it out now... – Neil Jul 06 '12 at 17:12