473,463 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

generics problem

I need to write a method that does something like the following. In general
it returns a nullable of the same type as the one passed in. Here is the
problem. If I try to assign a value to the nullable by uncommenting the line
' //tRet = -999;' it won't compile (cannot implicity convert type xxx to T?)
.. Is there a way I can do what I want...perhaps with some reflection method?
If I am heading down the wrong direction with this pleae let me know.

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// should have the (3,3) record in the database at this point...
int? i1 = 10;
int? i1r = (int?)MyMethod(i1);
int? i2 = null;
int? i2r = (int?)MyMethod(i2);
float? f1 = 10f;
float? f1r = (float?)MyMethod(f1);
float? f2 = null;
float? f2r = (float?)MyMethod(f2);
}
private static T? MyMethod<T>(T? myVal) where T : struct
{
try
{
if (myVal.HasValue)
return myVal;

// Now make the constructed type.
Type cType = typeof(Nullable<>).MakeGenericType(typeof(T));
T? tRet = (T?)Activator.CreateInstance(cType);
//tRet = -999;

return tRet;
}
catch
{
return null;
}
}
}
}
Oct 26 '07 #1
1 974
Brian,

It's not going to work, because 999 is a numeric value, and T can be any
kind of structure (like a DateTime, a TimeSpan) which you can't assign that
value to.

You would have to change your method to take 999 as input, and then
assign it in the method to the parameter passed in:

private static T? MyMethod<T>(T? myVal, T value) where T : struct

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

"Brian" <Br***@discussions.microsoft.comwrote in message
news:DE**********************************@microsof t.com...
>I need to write a method that does something like the following. In general
it returns a nullable of the same type as the one passed in. Here is the
problem. If I try to assign a value to the nullable by uncommenting the
line
' //tRet = -999;' it won't compile (cannot implicity convert type xxx to
T?)
. Is there a way I can do what I want...perhaps with some reflection
method?
If I am heading down the wrong direction with this pleae let me know.

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// should have the (3,3) record in the database at this
point...
int? i1 = 10;
int? i1r = (int?)MyMethod(i1);
int? i2 = null;
int? i2r = (int?)MyMethod(i2);
float? f1 = 10f;
float? f1r = (float?)MyMethod(f1);
float? f2 = null;
float? f2r = (float?)MyMethod(f2);
}
private static T? MyMethod<T>(T? myVal) where T : struct
{
try
{
if (myVal.HasValue)
return myVal;

// Now make the constructed type.
Type cType = typeof(Nullable<>).MakeGenericType(typeof(T));
T? tRet = (T?)Activator.CreateInstance(cType);
//tRet = -999;

return tRet;
}
catch
{
return null;
}
}
}
}


Oct 26 '07 #2

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

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: consumer62000 | last post by:
Hello All I found some *issue* with the generics in C#. It looks like an issue to me : In C++ the following code compiles fine class Person { public: char last;
5
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
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
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...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
13
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...
7
by: Andy Bell | last post by:
Can this be done via .net generics? How? The % signs below are just to show how I want to do it, I realise they're not valid syntax. public abstract class BaseSelectionRequirement { ......
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.