By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,198 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,198 IT Pros & Developers. It's quick & easy.

Generic string type defaults to null?

P: n/a
When I use this code:

public void CreateColumn<T>( string sName)
{
T thetype = default(T);
dc.DataType = thetype.GetType();
.....
}

via CreateColumn<String>

I fail on the GetType() because thetype is null. It works fine for
Boolean and Int32. Is there something special I need to get a default
for strings?

Thanks,
Brett

Mar 31 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Sorry, missing a line of code:

public void CreateColumn<T>( string sName)
{
T thetype = default(T);
DataColumn dc = new DataColumn( );
dc.DataType = thetype.GetType();

thetype is a goofy variable I use to add the type to the DataColumn.
I'd like to know a better way if there is one.

Thanks,
Brett

Mar 31 '06 #2

P: n/a
typeof(T)

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Sorry, missing a line of code:

public void CreateColumn<T>( string sName)
{
T thetype = default(T);
DataColumn dc = new DataColumn( );
dc.DataType = thetype.GetType();

thetype is a goofy variable I use to add the type to the DataColumn.
I'd like to know a better way if there is one.

Thanks,
Brett

Mar 31 '06 #3

P: n/a
Brett Romero <ac*****@cygen.com> wrote:
When I use this code:

public void CreateColumn<T>( string sName)
{
T thetype = default(T);
dc.DataType = thetype.GetType();
.....
}

via CreateColumn<String>

I fail on the GetType() because thetype is null. It works fine for
Boolean and Int32. Is there something special I need to get a default
for strings?


null is the default for strings, because string is a reference type.
Your code won't work for *any* reference type.

As Ted said, use typeof(T).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #4

P: n/a
It works great. Thanks!

Is there a way to have intellisense display an enumerator with the
types I want when the user gets this far:

CreateColumn<

After the opening carrot, the enum would display (such as what happens
on an assignment for certain types so you know what can be assigned).
The user scrolls through the enum or struct until they find the type
(bool, int, or string). Of course, I'd probably need to enforce it
inside the method somehow. Any suggestions on how to implement these
two approaches?

Thanks,
Brett

Mar 31 '06 #5

P: n/a
Brett Romero <ac*****@cygen.com> wrote:
It works great. Thanks!

Is there a way to have intellisense display an enumerator with the
types I want when the user gets this far:

CreateColumn<

After the opening carrot, the enum would display (such as what happens
on an assignment for certain types so you know what can be assigned).
The user scrolls through the enum or struct until they find the type
(bool, int, or string). Of course, I'd probably need to enforce it
inside the method somehow. Any suggestions on how to implement these
two approaches?


If you want to constrain the available options in a way that generics
doesn't allow, you might want to provide an enum of available types,
and pass that in. You'd have to have a map of some description from the
enum value to the appropriate type, of course...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.