Re: Generics limitation on .Net

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Luc,

You have to cast the result of the call of <type>.MinValue to T, like
so:

valueType = (T) Char.MinValue;


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Luc Vaillant" <LucVaillant@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:44FCF28B-0AA8-4F10-8493-A63937D4A8E9@xxxxxxxxxxxxxxxx
>I have already tried that, but it didn't work (for the same reasons):
>
> public class C<T>
> {
> private T myValue;
>
> private void InitializeValue(out T value)
> {
> if (valeurType.GetType() == typeof(Byte))
> valeurType = Byte.MinValue;
> else if (valeurType.GetType() == typeof(Char))
> valeurType = Char.MinValue;
> else if (valeurType.GetType() == typeof(Int16))
> valeurType = Int16.MinValue;
> // and so on...
> else
> valeurType = default(T);
> }
>
> public C()
> {
> InitializeValue(out myValue);
> }
>
> // ...
> }
>
> Error messages are :
> Error 1 Cannot implicitly convert type 'byte' to 'T'
> Error 2 Cannot implicitly convert type 'char' to 'T'
> Error 3 Cannot implicitly convert type 'short' to 'T'
>
> and so on...
>
>
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> Then you will have to do a type comparison on T and then set your
>> value
>> based on the type of T.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
>>
>> "Luc Vaillant" <LucVaillant@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>> news:DE4CDE78-3DF8-4967-821D-105AE9193AE9@xxxxxxxxxxxxxxxx
>> > Yes, this is for the default value, but what if I want to initialize my
>> > variable with a value that is not the default one ?
>> >
>> > "Joanna Carter [TeamB]" wrote:
>> >
>> >> "Luc Vaillant" <LucVaillant@xxxxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le
>> >> message de news: A67F0244-5B17-4521-85CD-124ADCD8B06A@xxxxxxxxxxxxxxxx
>> >>
>> >> |I need to initialise a typed parameter depending of its type in a
>> >> generic
>> >> | class.
>> >> | I have tried to use the C++ template form as follow, but it doesn't
>> >> work.
>> >> | It seems to be a limitation of generics vs C++ templates.
>> >> | Does anyone knows a workaround to do this ? Thx :
>> >> |
>> >> | public class C<T>
>> >> | {
>> >> | private T myValue;
>> >> |
>> >> | private void InitializeValue(out Byte value) { value =
>> >> Byte.MinValue; }
>> >> | private void InitializeValue(out Char value) { value =
>> >> Char.MinValue; }
>> >> | private void InitializeValue(out Int16 value) { value =
>> >> Int16.MinValue; }
>> >> | // ... and so on...
>> >> | private void InitializeValue(out String value) { value = "Initial
>> >> value"; }
>> >> | private void InitializeValue(out Object value) { value = this; }
>> >> |
>> >> | public C()
>> >> | {
>> >> | InitializeValue(out myValue);
>> >> | }
>> >> |
>> >> | // ...
>> >> | }
>> >>
>> >> You are not specifying the type that you wish to initialise, to the
>> >> constructor.
>> >>
>> >> In any case, you can use the 'default' keyword to initialise any type
>> >> passed
>> >> as the parameter to the generic class :Then you don't even have to
>> >> declare a
>> >> constructor for this purpose.
>> >>
>> >> public class C<T>
>> >> {
>> >> private T myValue = default(T);
>> >> }
>> >>
>> >> Joanna
>> >>
>> >> --
>> >> Joanna Carter [TeamB]
>> >> Consultant Software Engineer
>> >>
>> >>
>> >>
>>
>>
>>


.



Relevant Pages

  • Re: Generics limitation on .Net
    ... public class C ... private void InitializeValue ... InitializeValue(out myValue); ... >>> You are not specifying the type that you wish to initialise, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: java heap space
    ... public class Gui_Start extends JFrame ... Gui_Start(Control control) ... private void button_personalActionPerformed{ ... will repost your code as an SSCCE*. ...
    (comp.lang.java.programmer)
  • Re: update doesnt´t work ?
    ... public class Frame04 extends JFrame{ ... Parabeln myC = new Parabeln; ... private void setCtrlAccelerator ... public class Parabeln extends JPanel{ ...
    (comp.lang.java.programmer)
  • Re: How to update my data bound control?
    ... public class Woman: IPerson ... Then I got around to writing a controller ... public class PersonController ... private void AdvanceIterator() ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: How to update my data bound control?
    ... public class Woman: IPerson ... Then I got around to writing a controller ... public class PersonController ... private void AdvanceIterator() ...
    (microsoft.public.dotnet.framework.windowsforms)