Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

generics problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian - 26 Oct 2007 17:58 GMT
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;
           }
       }
   }
}
Nicholas Paldino [.NET/C# MVP] - 26 Oct 2007 19:46 GMT
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

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.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
[quoted text clipped - 46 lines]
>    }
> }
ssamuel - 26 Oct 2007 20:04 GMT
Brian,

I think you may be going about things the wrong way. It appears as if
you're using some very strong tools (generics, reflection, etc.) for
something that may just as easily be solved with something like a
typed DataSet. The problem of typing common database types (int,
float, etc.) is well-solved.

It looks like the reason you're having this problem is that you're
making assumptions about the type passed to your method. It looks like
you expect that the only thing to be passed is some numerical type,
hence your attempt to return -999. The compiler won't allow this
because you could be given a non-numeric struct. Consider these
structs:

public struct Complex { public int real; public int imaginary; }

public struct WebListItem { public string name; public string value; }

While either one would be valid to pass to your method, neither of
those can map cleanly to -999.

s}

> 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
[quoted text clipped - 45 lines]
>
> - Show quoted text -

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.