Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Returning int, string, logical, ? from method
Message
From
22/05/2008 21:53:13
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01319040
Message ID:
01319156
Views:
14
Hi Paul,

>>
>>I am trying to hide some complexity from the application within a settings Manager class. I want to have a method to return a value based on a setting name. The problem is the value may be an integer, string, logical, decimal, double, or datetime. What is the best way to do this so I can use a single method available to the app for obtaining these? A call might look like this:
>>
>>
>>
>>serialPort.Name = oSettingsManager.GetSettingByName("RfidPort");
>>
>>Or do I have only the option of returning everything as an object?
>>serialPort.Name = oSettingsManager.GetSettingByName("RfidPort").ToString();
>>
>>
>>
>
>You would have to pass everything as an object and cast it back to the correct type. I'm using a class from West Wind for this:
>
>http://www.west-wind.com/wwAppConfiguration/
>
>You basically create a property on an object and it automatically handles persisting & retrieving those settings for you. It's worth the money, but if you don't want to purchase anything you can use a similar idea for your class (what I'm about to describe isn't really how Rick's class works, but it might be easier to implement).
>
>Basically, you could create a class which has strongly-typed members & properties. In the get of the property call your GetSettingByName method which returns an object type and then cast it back to the correct type. Or, instead of a get method just have a constructer which automatically loads all the settings once. Now all your code which accesses these values becomes much easier because it's type safe w/intellisense.

Currently I have a class with strongly typed properties and I persist the data out to a xml file. The problem has always been in the interface for saving and changes the properties. I am changing this over to be database based with a single row for each property. I think the ability to dynamically add new properties and still be able to retrieve them and save them back this way will be much more flexible. I suspected I was going to have to pass it back as an object and cast each one back but then I have to do this in the applciation. It seemed a little fragile to expect the applciation to do this. I will take a look at the west wind appConfiguration. Otherwise I will figure a way to do it with a composite method like you suggest.
Thanks for the advice, it helps out.

Naomi, I was thinking about how to do this with generics, but some how I would have to create a class that used generics for a single value and provide the type with each value. I couldn't figure out how to do that and save me anything. I wish you could pass back a varied (generic) return value from a method but then you sort of break the whole idea of type safe when you do that. If I don't find something out there maybe in westwind stuff I like then I will figure out how to do this best.

I can pass back a entity row and have a single method in the application to sort the type first. I will post back what I come up with.
Thanks
Tim
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform