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 5 2512
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Visual Systems AB \(Martin Arvidsson\) |
last post by:
Hi!
Is it possible to alter the string type?
Basicly what i want to do is to create my own string type. lets call it
sqlString, when i use this
it will always use replace on the string...
|
by: Jiho Han |
last post by:
Here's the issue.
You have a class,
Class Person
{
public int id;
public string firstname;
public string lastname;
}
|
by: Frederick Gotham |
last post by:
For objects, we have "void*" as the generic pointer type. For instance:
enum ParamType { Int, Double };
void Func(void *const p,ParamType const pt)
{
switch (pt)
{
case Int: *(int*)p = 42;...
|
by: Mike |
last post by:
Hi,
I'm passing a JSON-encoded string to json_decode() and am expecting
its output to be an object type, but am getting a string type instead.
How can I return an object?
In the docs, the...
|
by: yogb21 |
last post by:
Hello !
I want to know...can I write a method inside C# class which has generic input parameters and generic return type. If yes..can someone help me with such method declaration and method usage....
|
by: SchoolOfLife |
last post by:
Does an object in C++ of std::string type terminates with null character?
The charater string literals in C++ (to maintain backward compatibility with C) end with a null character. Is this same with...
|
by: JKAT |
last post by:
Hi All,
I am using a com object to maniplate data inside an external program using VB.NET. One of the methods this com object provides requires 1 string parameter.
The external program,...
|
by: labmonkey111 |
last post by:
I have a table in SQLServer with a varchar field set to NOT NULL, with a default value of the empty string. This table is linked in access, and the field is linked to a text box on a form. When I...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |