473,511 Members | 15,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics in .net 2.0

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("onProductionServe r",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
Nov 17 '05 #1
5 1150
Personally, I would create an accessor for each type I expect to get out of
the .config file ...

GetInt()
GetDouble()
GetBoolean()

etc.

have a look at the post "Reccomended ways of saving WinForms' "viewstate"?"
in group "microsoft.public.dotnet.framework.windowsform s" 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("onProductionServe r",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

Nov 17 '05 #2

"billr" <bi***@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
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.windowsform s" 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("onProductionServe r",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

Nov 17 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Søren Reinke" <so***@reinke.fjernmig.dk> wrote in message
news:de**********@newsbin.cybercity.dk...

"billr" <bi***@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
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.windowsform s" 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("onProductionServe r",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


Nov 17 '05 #4
Be careful with that approach. If retVal is (for example) a boxed int, the
cast will throw an exception if T is anything other than int.

To really make this approach work, conversions are required.

I think this is not really a good application for generics.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uF**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Søren Reinke" <so***@reinke.fjernmig.dk> wrote in message
news:de**********@newsbin.cybercity.dk...

"billr" <bi***@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
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.windowsform s" 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("onProductionServe r",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



Nov 17 '05 #5

"Ted Miller" <te*@millerzone.net> wrote in message
news:11*************@corp.supernews.com...
Be careful with that approach. If retVal is (for example) a boxed int, the
cast will throw an exception if T is anything other than int.

To really make this approach work, conversions are required.

I think this is not really a good application for generics.


Okay, i'll just stick to 1 method per type :)
--
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
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
2441
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
3082
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
2520
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
2715
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
5
2910
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
11
2469
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
5951
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
2419
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
3242
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
3794
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.