By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,626 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,626 IT Pros & Developers. It's quick & easy.

Generics limitation on .Net

P: n/a
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);
}

// ...
}
Dec 28 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: A6**********************************@microsoft.com...

|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
Dec 28 '05 #2

P: n/a
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" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: A6**********************************@microsoft.com...

|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

Dec 28 '05 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
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" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: A6**********************************@microsoft.com...

|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

Dec 28 '05 #4

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> a écrit
dans le message de news: OQ**************@TK2MSFTNGP09.phx.gbl...

| Then you will have to do a type comparison on T and then set your value
| based on the type of T.

Hehe, you beat me to that one :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 28 '05 #5

P: n/a
I'm sneaky like that.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> a écrit
dans le message de news: OQ**************@TK2MSFTNGP09.phx.gbl...

| Then you will have to do a type comparison on T and then set your
value
| based on the type of T.

Hehe, you beat me to that one :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 28 '05 #6

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
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" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: A6**********************************@microsoft.com...

|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


Dec 28 '05 #7

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:DE**********************************@microsof t.com...
> 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" <Lu*********@discussions.microsoft.com> a écrit dans le
>> message de news: A6**********************************@microsoft.com...
>>
>> |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
>>
>>
>>


Dec 28 '05 #8

P: n/a
Luc,
One way might be to cast the value to an object, then cast it to T,
something like:

| if (valeurType.GetType() == typeof(Byte))
| valeurType = (T)(object)Byte.MinValue;

If you know you want "MinValue" I would consider using Reflection to find
the "MinValue" field on the respective type, something like:

private void InitializeValue(out T value)
{
Type t = typeof(T);
System.Reflection.FieldInfo fi = t.GetField("MinValue");
if (fi == null)
{
value = default(T);
}
else
{
value = (T)fi.GetValue(null);
}
}

This helps ensure that C<T> supports "MinValue" on new types that may be
used with it, without modifying C<T> itself.

NOTE: The above routine assumes that MinValue is a static field...
Although I would define the class such that default(T) was acceptable or
used another field to indicate the special value...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
|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]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
| > news:DE**********************************@microsof t.com...
| > > 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" <Lu*********@discussions.microsoft.com> a écrit dans
le
| > >> message de news:
A6**********************************@microsoft.com...
| > >>
| > >> |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
| > >>
| > >>
| > >>
| >
| >
| >
Dec 28 '05 #9

P: n/a
Nicholas,
| valueType = (T) Char.MinValue;
Produces a compile error!

You need to cast to object first, as Char to T is not defined. Without a
constraint only object to/from T is defined.

| valueType = (T)(object)Char.MinValue;

Even then a runtime error could occur...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eV**************@TK2MSFTNGP12.phx.gbl...
| 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]
| - mv*@spam.guard.caspershouse.com
|
| "Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
| news:44**********************************@microsof t.com...
| >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]
| >> - mv*@spam.guard.caspershouse.com
| >>
| >> "Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
| >> news:DE**********************************@microsof t.com...
| >> > 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" <Lu*********@discussions.microsoft.com> a écrit dans
le
| >> >> message de news:
A6**********************************@microsoft.com...
| >> >>
| >> >> |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
| >> >>
| >> >>
| >> >>
| >>
| >>
| >>
|
|
Dec 28 '05 #10

P: n/a
Good point! =)

You think that people would buy newsreaders with compilers built into
them? =)

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

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:OQ***************@TK2MSFTNGP15.phx.gbl...
Nicholas,
| valueType = (T) Char.MinValue;
Produces a compile error!

You need to cast to object first, as Char to T is not defined. Without a
constraint only object to/from T is defined.

| valueType = (T)(object)Char.MinValue;

Even then a runtime error could occur...

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:eV**************@TK2MSFTNGP12.phx.gbl...
| 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]
| - mv*@spam.guard.caspershouse.com
|
| "Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
| news:44**********************************@microsof t.com...
| >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]
| >> - mv*@spam.guard.caspershouse.com
| >>
| >> "Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in
message
| >> news:DE**********************************@microsof t.com...
| >> > 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" <Lu*********@discussions.microsoft.com> a écrit
dans
le
| >> >> message de news:
A6**********************************@microsoft.com...
| >> >>
| >> >> |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
| >> >>
| >> >>
| >> >>
| >>
| >>
| >>
|
|

Dec 28 '05 #11

P: n/a
| You think that people would buy newsreaders with compilers built into
| them? =)
They might! There's been days when I would find it helpful...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2******************@TK2MSFTNGP09.phx.gbl...
| Good point! =)
|
| You think that people would buy newsreaders with compilers built into
| them? =)
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - mv*@spam.guard.caspershouse.com
|
<<snip>>
Dec 28 '05 #12

P: n/a
Thanks a lot for your answer,

cast value->Object->T works fine.

I don't need the MinValue, it was just for the example.
But Reflexion is a good idea, I will keep it in mind

Thanks again
Luc
"Jay B. Harlow [MVP - Outlook]" wrote:
Luc,
One way might be to cast the value to an object, then cast it to T,
something like:

| if (valeurType.GetType() == typeof(Byte))
| valeurType = (T)(object)Byte.MinValue;

If you know you want "MinValue" I would consider using Reflection to find
the "MinValue" field on the respective type, something like:

private void InitializeValue(out T value)
{
Type t = typeof(T);
System.Reflection.FieldInfo fi = t.GetField("MinValue");
if (fi == null)
{
value = default(T);
}
else
{
value = (T)fi.GetValue(null);
}
}

This helps ensure that C<T> supports "MinValue" on new types that may be
used with it, without modifying C<T> itself.

NOTE: The above routine assumes that MinValue is a static field...
Although I would define the class such that default(T) was acceptable or
used another field to indicate the special value...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:44**********************************@microsof t.com...
|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]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
| > news:DE**********************************@microsof t.com...
| > > 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" <Lu*********@discussions.microsoft.com> a écrit dans
le
| > >> message de news:
A6**********************************@microsoft.com...
| > >>
| > >> |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
| > >>
| > >>
| > >>
| >
| >
| >

Dec 29 '05 #13

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit dans
le message de news: OQ***************@TK2MSFTNGP15.phx.gbl...

| You need to cast to object first, as Char to T is not defined. Without a
| constraint only object to/from T is defined.
|
|| valueType = (T)(object)Char.MinValue;

You can also use the Convert class :

T value = (T) Convert.ChangeType(Char.MinValue, typeof(T))

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 29 '05 #14

P: n/a
> You can also use the Convert class :

T value = (T) Convert.ChangeType(Char.MinValue, typeof(T))
This will also box Char.MinValue into Object
"Joanna Carter [TeamB]" wrote:
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit dans
le message de news: OQ***************@TK2MSFTNGP15.phx.gbl...

| You need to cast to object first, as Char to T is not defined. Without a
| constraint only object to/from T is defined.
|
|| valueType = (T)(object)Char.MinValue;

You can also use the Convert class :

T value = (T) Convert.ChangeType(Char.MinValue, typeof(T))

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 29 '05 #15

P: n/a
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 7F**********************************@microsoft.com...

| This will also box Char.MinValue into Object

I won't deny that :-)

Tell me, what are you doing that requires such super-optimised code ? Most
business applications are only really as fast as the person typing at the
keyboard.

If speed is that much of an issue, then don't write code that invokes so
much boxing. The example that you give doesn't really seem to fit the
generic programming model due to the possible differences in behaviour
required by the different types that may be used. In which case, I would
tend to look elsewhere for a solution; generics are only really useful when
the behaviour for *all* anticipated types is *identical*.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 29 '05 #16

P: n/a
Is that an answer ?

I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.
If speed is that much of an issue, then don't write code that invokes so
much boxing
That's why I wanted to use Generics...
The example that you give doesn't really seem to fit the
generic programming model due to the possible differences in behaviour
required by the different types that may be used
The example I gave was just to demonstrate the initialization issue. It was
not a real use case...
Same remark about the 'Generics limitation' post I sent today. The example
is to desmonstrate the != operator issue with generics, and this is not my
real program...

Why don't you try to answer my questions instead of explaining me that most
programs don't need speed, and that my examples don't "really seem to fit the
generic programming model".

My questions are simple:

1) How do I initialize a T value without doing boxing/unboxing ?

The answer is:
Not possible, must use T value = (T)(Object)constant value
even if I know that the constant value if of type T

2) How do I compare two T values without boxing/unboxing ?

The answer is:
Not possible from the answers I got in this post
Regards
Luc
"Joanna Carter [TeamB]" wrote:
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 7F**********************************@microsoft.com...

| This will also box Char.MinValue into Object

I won't deny that :-)

Tell me, what are you doing that requires such super-optimised code ? Most
business applications are only really as fast as the person typing at the
keyboard.

If speed is that much of an issue, then don't write code that invokes so
much boxing. The example that you give doesn't really seem to fit the
generic programming model due to the possible differences in behaviour
required by the different types that may be used. In which case, I would
tend to look elsewhere for a solution; generics are only really useful when
the behaviour for *all* anticipated types is *identical*.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 29 '05 #17

P: n/a
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 33**********************************@microsoft.com...

| Is that an answer ?

Sometimes answers need to include further questions :-)

Sometimes, a good argument can help to avoid following "les poissons rouges"

| I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.

No need to shout, such things are not always apparent.

| That's why I wanted to use Generics...

What made you think that generics would necessarily be fast ?

| The example I gave was just to demonstrate the initialization issue. It
was
| not a real use case...
| Same remark about the 'Generics limitation' post I sent today. The example
| is to desmonstrate the != operator issue with generics, and this is not my
| real program...

As I said, your particular requirements may indicate that generics are not
necessarily the best direction. If you wanted to set T to its true default,
then that is what the default(...) function is there for.

| Why don't you try to answer my questions instead of explaining me that
most
| programs don't need speed, and that my examples don't "really seem to fit
the
| generic programming model".

Because, from years of experience in OO, I felt that you were going down a
direction simply because it seemed right but not necessarily wise. You are,
of course, welcome to accept or discard any advice given; it comes without
malice or antagonism.

| My questions are simple:
|
| 1) How do I initialize a T value without doing boxing/unboxing ?
|
| The answer is:
| Not possible, must use T value = (T)(Object)constant value
| even if I know that the constant value if of type T

The answer is to understand that generics provides a mechanism, default(T),
which you feel is not adequate, therefore, you may have to either accept the
shortcomings of the alternatives like boxing, or choose an alternative to
generics.

If the types you are going to use are within your control, then you could
always have them implement a common interface with a method like
Initialise().

| 2) How do I compare two T values without boxing/unboxing ?
|
| The answer is:
| Not possible from the answers I got in this post

The only other alternative I could think of was to add the IComparable<T>
constraint to the generic class, allowing you to use :

{
int result = CompareTo(T other);
}

That is, assuming that your T types support IComparable<T>.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 29 '05 #18

P: n/a
Ok, I'm bored feeding the troll...
Because, from years of experience in OO, I felt that you were going down a
direction simply because it seemed right but not necessarily wise. You are,
of course, welcome to accept or discard any advice given; it comes without
malice or antagonism.
With your "years of experience in OO", you should felt that answer :

class MyComp : IComparer<short>, IComparer<int>, IComparer<long>
{
public int Compare(int x, int y) { return x - y; }
public int Compare(short x, short y) { return x - y; }
public int Compare(long x, long y) { return x - y; }
}

class GenClass<T, I> where I : IComparer<T>, new()
{
private IComparer<T> comparer = new I();

public GenClass(T value1, T value2)
{
// THIS IS AN EXAMPLE, NOT MY REAL PROGRAM !!!
// (I'M NOT SHOUTING..., IT'S JUST A WARNING...)
if (comparer.Compare(value1, value2) == 0)
Console.WriteLine("value1 equals value2");
}
}

class Program
{
static void Main(string[] args)
{
// THIS IS AN EXAMPLE TOO, NOT MY REAL PROGRAM !!!
GenClass<short, MyComp> A = new GenClass<short, MyComp>(123, 123);
GenClass<int, MyComp> B = new GenClass<int, MyComp>(123, 123);
GenClass<long, MyComp> B = new GenClass<long, MyComp>(123, 123);
}
}
What made you think that generics would necessarily be fast ? Have you ever written or used generics ? Is Generics just a search/replace
tool for you ?

"In addition to type safety, generic collection types generally perform
better for storing and manipulating value types because there is no need to
box the value types." taken from
"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/6b90f9c3-eee3-4cd1-98d6-82019c83a1df.htm" :

Regards
Luc

"Joanna Carter [TeamB]" wrote:
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 33**********************************@microsoft.com...

| Is that an answer ?

Sometimes answers need to include further questions :-)

Sometimes, a good argument can help to avoid following "les poissons rouges"

| I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.

No need to shout, such things are not always apparent.

| That's why I wanted to use Generics...

What made you think that generics would necessarily be fast ?

| The example I gave was just to demonstrate the initialization issue. It
was
| not a real use case...
| Same remark about the 'Generics limitation' post I sent today. The example
| is to desmonstrate the != operator issue with generics, and this is not my
| real program...

As I said, your particular requirements may indicate that generics are not
necessarily the best direction. If you wanted to set T to its true default,
then that is what the default(...) function is there for.

| Why don't you try to answer my questions instead of explaining me that
most
| programs don't need speed, and that my examples don't "really seem to fit
the
| generic programming model".

Because, from years of experience in OO, I felt that you were going down a
direction simply because it seemed right but not necessarily wise. You are,
of course, welcome to accept or discard any advice given; it comes without
malice or antagonism.

| My questions are simple:
|
| 1) How do I initialize a T value without doing boxing/unboxing ?
|
| The answer is:
| Not possible, must use T value = (T)(Object)constant value
| even if I know that the constant value if of type T

The answer is to understand that generics provides a mechanism, default(T),
which you feel is not adequate, therefore, you may have to either accept the
shortcomings of the alternatives like boxing, or choose an alternative to
generics.

If the types you are going to use are within your control, then you could
always have them implement a common interface with a method like
Initialise().

| 2) How do I compare two T values without boxing/unboxing ?
|
| The answer is:
| Not possible from the answers I got in this post

The only other alternative I could think of was to add the IComparable<T>
constraint to the generic class, allowing you to use :

{
int result = CompareTo(T other);
}

That is, assuming that your T types support IComparable<T>.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 30 '05 #19

P: n/a
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 81**********************************@microsoft.com...

| Ok, I'm bored feeding the troll...

I'm sorry you feel that way. I have never "trolled" newsgroups; had I done
so, I doubt that I would have been appointed as a newsgroup mediator for
Borland. You obviously have a problem with people who don't give you advice
or don't have opinions with which you agree.

| With your "years of experience in OO", you should felt that answer :

Hey, I am officially on holiday for Christmas. Just because I don't come up
with the right answer first time, doesn't give you the right to denegrate my
experience. If I don't understand your question fully the first ime around,
it may be that I am working on more than just solving your problems; like
writing articles for people that do appreciate the time and effort that goes
into trying to help others understand some pretty complicated stuff.

| Have you ever written or used generics ? Is Generics just a search/replace
| tool for you ?
|
| "In addition to type safety, generic collection types generally perform
| better for storing and manipulating value types because there is no need
to
| box the value types." taken from
|
"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/6b90f9c3-eee3-4cd1-98d6-82019c83a1df.htm"
:

It seems it takes a troll to think they have found an other troll :-) I find
your attitude to be insulting.

So, does the IComparer solve your problem ?

Have a peaceful New Year.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 30 '05 #20

P: n/a
Joanna,

I felt angry because of such remarks :

- "Tell me, what are you doing that requires such super-optimised code ? Most
business applications are only really as fast as the person typing at the
keyboard"
==> My question was 'I need speed', and your answer is "Why, nobody cares
about speed ?"

- "If speed is that much of an issue, then don't write code that invokes so
much boxing"
==> That was the purpose of my question : how to avoid so much boxing ?

- "The example that you give doesn't really seem to fit the generic
programming model"
==> You first denigrate my work and experience based on an silly example
without knowing the work I'm doing and my needs

Now, I apologize if my provious message hurts you, and I sincerly thanks you
for the time you gave me answering my messages (I really think that).

Regards
Luc

"Joanna Carter [TeamB]" wrote:
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: 81**********************************@microsoft.com...

| Ok, I'm bored feeding the troll...

I'm sorry you feel that way. I have never "trolled" newsgroups; had I done
so, I doubt that I would have been appointed as a newsgroup mediator for
Borland. You obviously have a problem with people who don't give you advice
or don't have opinions with which you agree.

| With your "years of experience in OO", you should felt that answer :

Hey, I am officially on holiday for Christmas. Just because I don't come up
with the right answer first time, doesn't give you the right to denegrate my
experience. If I don't understand your question fully the first ime around,
it may be that I am working on more than just solving your problems; like
writing articles for people that do appreciate the time and effort that goes
into trying to help others understand some pretty complicated stuff.

| Have you ever written or used generics ? Is Generics just a search/replace
| tool for you ?
|
| "In addition to type safety, generic collection types generally perform
| better for storing and manipulating value types because there is no need
to
| box the value types." taken from
|
"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/6b90f9c3-eee3-4cd1-98d6-82019c83a1df.htm"
:

It seems it takes a troll to think they have found an other troll :-) I find
your attitude to be insulting.

So, does the IComparer solve your problem ?

Have a peaceful New Year.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 30 '05 #21

P: n/a
"Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
message de news: C2**********************************@microsoft.com...

| Now, I apologize if my provious message hurts you, and I sincerly thanks
you
| for the time you gave me answering my messages (I really think that).

De rien :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 30 '05 #22

P: n/a
In the context of the OPs question: I would stay with the cast over convert,
as Convert.ChangeType "requires" the class implement IConvertible, or at the
very least Convert.ChangeType needs to know the conversion.

The cast doesn't care what the value implements...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:en**************@TK2MSFTNGP11.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> a écrit
dans
| le message de news: OQ***************@TK2MSFTNGP15.phx.gbl...
|
|| You need to cast to object first, as Char to T is not defined. Without a
|| constraint only object to/from T is defined.
||
||| valueType = (T)(object)Char.MinValue;
|
| You can also use the Convert class :
|
| T value = (T) Convert.ChangeType(Char.MinValue, typeof(T))
|
| Joanna
|
| --
| Joanna Carter [TeamB]
| Consultant Software Engineer
|
|
Jan 3 '06 #23

P: n/a
| 1) How do I initialize a T value without doing boxing/unboxing ?

| Not possible, must use T value = (T)(Object)constant value
| even if I know that the constant value if of type T
Boxes the value! Remember (object) says to box value types!

You have two options on initializing T values without boxing:

1. default(T)
2. Parameterized constructor

NOTE: The reflection trick I showed earlier will box values.

| 2) How do I compare two T values without boxing/unboxing ?
Constrain T by IComparable<T>

http://msdn2.microsoft.com/en-us/library/4d7sx9hd.aspx

NOTE: System.IComparable<T> is different then System.IComparable!

In the case of simple equality, I would recommend constraining T by
IEquatable<T>.

http://msdn2.microsoft.com/en-us/lib...US,VS.80).aspx

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Luc Vaillant" <Lu*********@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
| Is that an answer ?
|
| I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.
|
| > If speed is that much of an issue, then don't write code that invokes so
| > much boxing
|
| That's why I wanted to use Generics...
|
| > The example that you give doesn't really seem to fit the
| > generic programming model due to the possible differences in behaviour
| > required by the different types that may be used
|
| The example I gave was just to demonstrate the initialization issue. It
was
| not a real use case...
| Same remark about the 'Generics limitation' post I sent today. The example
| is to desmonstrate the != operator issue with generics, and this is not my
| real program...
|
| Why don't you try to answer my questions instead of explaining me that
most
| programs don't need speed, and that my examples don't "really seem to fit
the
| generic programming model".
|
| My questions are simple:
|
| 1) How do I initialize a T value without doing boxing/unboxing ?
|
| The answer is:
| Not possible, must use T value = (T)(Object)constant value
| even if I know that the constant value if of type T
|
| 2) How do I compare two T values without boxing/unboxing ?
|
| The answer is:
| Not possible from the answers I got in this post
|
|
| Regards
| Luc
|
|
| "Joanna Carter [TeamB]" wrote:
|
| > "Luc Vaillant" <Lu*********@discussions.microsoft.com> a écrit dans le
| > message de news: 7F**********************************@microsoft.com...
| >
| > | This will also box Char.MinValue into Object
| >
| > I won't deny that :-)
| >
| > Tell me, what are you doing that requires such super-optimised code ?
Most
| > business applications are only really as fast as the person typing at
the
| > keyboard.
| >
| > If speed is that much of an issue, then don't write code that invokes so
| > much boxing. The example that you give doesn't really seem to fit the
| > generic programming model due to the possible differences in behaviour
| > required by the different types that may be used. In which case, I would
| > tend to look elsewhere for a solution; generics are only really useful
when
| > the behaviour for *all* anticipated types is *identical*.
| >
| > Joanna
| >
| > --
| > Joanna Carter [TeamB]
| > Consultant Software Engineer
| >
| >
| >
Jan 3 '06 #24

This discussion thread is closed

Replies have been disabled for this discussion.