Re: Generics in .net 2.0

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Søren,

You should be able to use generics here, on the method level. You could
declare the method as:

// Name it GetConfigValue, not getConfigValue, since
// it is a violation of the public naming guidelines.
public T GetConfigValue<T>(string key, T defaultValue)
{
// Get the value here, it should be returned as an object.
object retVal = ...;

// Cast the return value.
return (T) retVal;
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Søren Reinke" <soren@xxxxxxxxxxxxxxxxxx> wrote in message
news:de263k$7vt$1@xxxxxxxxxxxxxxxxxxxxxxx
>
> "billr" <billr@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:373E66FC-2F88-477F-BC16-D4D75836CFC0@xxxxxxxxxxxxxxxx
>> Personally, I would create an accessor for each type I expect to get out
>> of
>> the .config file ...
>>
>> GetInt()
>> GetDouble()
>> GetBoolean()
>
> That is what i have done, just interested in knowing if it was possible to
> do in Generics.
>
>
> --
> Best regards C.T.O. Søren Reinke
> www.Xray-Mag.com/ - Your free diving magazin on the net. Download it in
> PDF
> Aug-sept issue of X-RAY Magazine is ready to download:
> EGYPT Finding Yolanda Wreck, Celebrate the Seas 2005
>
>>
>> etc.
>>
>> have a look at the post "Reccomended ways of saving WinForms'
>> "viewstate"?"
>> in group "microsoft.public.dotnet.framework.windowsforms" for a sample
>> .config file reader/writer
>>
>>
>> --
>> Of all words of tongue and pen, the saddest are: "It might have been"
>>
>> Bill.Richards @ greyskin .co .uk
>> http://greyskin.co.uk
>>
>>
>> "Søren Reinke" wrote:
>>
>>> Hi there
>>>
>>> I have a problem, with key/value pairs in web.config.
>>>
>>> The value is sometimes a boolean, sometimes an integer and sometimes a
>>> string and so on.
>>>
>>> Do i need to make a method for each type ?
>>> Or can i use generics ?
>>>
>>> I would like to be able to do something like:
>>>
>>> Integer age=getConfigValue("KeyName",17); //name,default value if
>>> something
>>> fails)
>>> Boolean productionServer=getConfigValue("onProductionServer",false);
>>>
>>> And more like that.
>>>
>>>
>>> --
>>> Best regards C.T.O. Søren Reinke
>>> www.Xray-Mag.com/ - Your free diving magazin on the net. Download it in
>>> PDF
>>> Aug-sept issue of X-RAY Magazine is ready to download:
>>> EGYPT Finding Yolanda Wreck, Celebrate the Seas 2005
>>>
>>>
>>>
>
>


.