473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to add a constraint of Nullable Types to a generics method

Hi,

compiling the following code:

public class App
{
static void Main()
{
int? x = 5;
bool? i = null;
Console.WriteLine(GetType(x).FullName);
Console.WriteLine(" x={0}", GetSafeValue(x));
Console.WriteLine(" i={0}", GetSafeValue(i));
Console.ReadLine();
}
public static object GetSafeValue<T, N>(T value) where T:struct
{

if (!IsNullableValueType(GetType(value)))
{
Console.WriteLine(" {0} is not a Nullable type", value.GetType());
return value;
}
object nullableValue = value;

return nullableValue ??
GetDefaultDataTypeValue((GetType(value)).GetGeneri cArguments()[0]);

}
}

I get the error message
"The type 'int?' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method
'App.GetSafeValue<T>(T)'"

Obviously, I have set the constraint to only accept value types.

Now, how do I modify the constraint so that I can accept either value
types or Nullable types?

Thanks in advance.

Jul 17 '06 #1
2 5462
Joe,

Why not just get the Value on the nullable to begin with? It will
return a default value if it is "null" (meaning, it is flagged as null, it
isn't actually null).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Bloggs" <es***@rocketmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Hi,

compiling the following code:

public class App
{
static void Main()
{
int? x = 5;
bool? i = null;
Console.WriteLine(GetType(x).FullName);
Console.WriteLine(" x={0}", GetSafeValue(x));
Console.WriteLine(" i={0}", GetSafeValue(i));
Console.ReadLine();
}
public static object GetSafeValue<T, N>(T value) where T:struct
{

if (!IsNullableValueType(GetType(value)))
{
Console.WriteLine(" {0} is not a Nullable type", value.GetType());
return value;
}
object nullableValue = value;

return nullableValue ??
GetDefaultDataTypeValue((GetType(value)).GetGeneri cArguments()[0]);

}
}

I get the error message
"The type 'int?' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method
'App.GetSafeValue<T>(T)'"

Obviously, I have set the constraint to only accept value types.

Now, how do I modify the constraint so that I can accept either value
types or Nullable types?

Thanks in advance.

Jul 17 '06 #2
"Joe Bloggs" <es***@rocketmail.comwrote:
public static object GetSafeValue<T, N>(T value) where T:struct
Yes, struct excludes nullable types.
Obviously, I have set the constraint to only accept value types.

Now, how do I modify the constraint so that I can accept either value
types or Nullable types?
You can't do it statically. You'll have to do it at runtime.

-- Barry

--
http://barrkel.blogspot.com/
Jul 17 '06 #3

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

Similar topics

10
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in...
2
by: julio | last post by:
I need to assign null values to datetime fields Where I can find types like NullableDateTime or NullableInt. TIA
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
1
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5;
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
3
by: Tony Johansson | last post by:
Hello! Is it possible to declare existing .net generics to have nullable types(for example type int? ) ? If the answer on the previous question is yes then I assume that you can also define...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.