What TypeCode to return when implementing IConvertible GetTypeCode()

A

Art

When i implement IConvertible GetTypeCode() which TypeCode would i
return?

TypeCode.Object if it is implemented in a class?

What in case if it is implemented in a struct?


In general i don't know what IConvertible GetTypeCode() is used for.
Can someone explain this to me? The examples i found in the msdn and
the web are not helpful.

Lets consider the unlikely case where i have an object that can best
be represented as a boolean value. Would i then return
TypeCode.Boolean to indicate that a conversion to a boolean value
among the type codes represents the object best?

thanks in advance.

arthur
 
P

Peter Duniho

Art said:
When i implement IConvertible GetTypeCode() which TypeCode would i
return?

TypeCode.Object if it is implemented in a class?

What in case if it is implemented in a struct?


In general i don't know what IConvertible GetTypeCode() is used for.
Can someone explain this to me? The examples i found in the msdn and
the web are not helpful.

Lets consider the unlikely case where i have an object that can best
be represented as a boolean value. Would i then return
TypeCode.Boolean to indicate that a conversion to a boolean value
among the type codes represents the object best?

Granted, I'm just basing this on what I read in the documentation, but…

I would think your code would look something like this:

class MyClass : IConvertible
{
// …other stuff

TypeCode GetTypeCode()
{
typeof(MyClass).GetTypeCode();
}
}

Does that not work?

Pete
 
A

Andy O'Neill

In general i don't know what IConvertible GetTypeCode() is used for.
Can someone explain this to me? The examples i found in the msdn and
the web are not helpful.

Typecode is an enumeration of clr types.
You should only be implementing iConvertible if your class can be converted
to a boolean or whatever.
Only really applicable to pretty low level classes.
As you don't seem to know that already I wonder whether you really want to
be implementing iConvertible.
 
A

Art

Typecode is an enumeration of clr types.
You should only be implementing iConvertible if your class can be converted
to a boolean or whatever.
Only really applicable to pretty low level classes.
As you don't seem to know that already I wonder whether you really want to
be implementing iConvertible.

@andy

you are right, i don't want to implement it. i learn for the
foundations MCTS examn.

but i wondered what would be the correct implementation. as i don't
understand, i wonder where and for what GetTypeCode() is used.

i found this
http://msdn.microsoft.com/en-us/library/2x07fbw8.aspx

what i extract from this article is, that GetTypeCode should return
the code for the type which would be the best representation for a
certain use. in this case it is marshaling.

ok, GetTypeCode() is used by the marshaler.

then i would return TypeCode.Boolean for an object that just holds one
boolen value (for whatever reason, but just for the sake of the
example)?
i would surely return TypeCode.Boolean if the interface is implemented
on a struct that holds one boolean value.
in case i have struct that holds two boolean values, and i don't want
to lose information in the marshaling process, i would return
TypeCode.Object.

what do you make of this?
 
A

Art

Granted, I'm just basing this on what I read in the documentation, but…

I would think your code would look something like this:

   class MyClass : IConvertible
   {
     // …other stuff

     TypeCode GetTypeCode()
     {
       typeof(MyClass).GetTypeCode();
     }
   }

Does that not work?

Pete

@pete

thanks, and yes - it works (the method is static. so it is

TypeCode tc = Type.GetTypeCode(typeof(MyType));

).

but my question, what GetTypeCode is used for and what it shall return
still stands.

i am already a little bit enlighted. i found this article

http://msdn.microsoft.com/en-us/library/2x07fbw8.aspx

and it leads me to assume that, in my implementation, i can return
type codes other than the one retreived in the way you stated. and
that it is encouraged, to provide additional information (in the case
of this article it is additional type code information for the
marshaler). otherwise there would be no need for
IConvertible.GetTypeCode() above Type.GetTypeCode(), would there?

e.g.
Type.GetTypeCode() returns TypeCode.Object for a struct.

but maybe (for whatever reason) i have struct, that just holds one
boolean value. In this case it would be feasible to return
TypeCode.Boolean and implement IConvertible.ToBoolean(). So the
marshaler would marshal it as a boolean value.

So generally speaking, i can tell to whoever calls
IConvertible.GetTypeCode(), to what the instance should be converted
to. hence it is good to know who uses GetTypeCode for what purpose.
after i found the article, mentioned above, i know marshaling is one
case.

do i draw propper conclusions, or do i still miss points?

thanks,
arthur
 
A

Art

I think my problem of understanding emerges where i do not know what
type codes are and what they are used for by the .net framework
 
A

Andy O'Neill

Art said:
I think my problem of understanding emerges where i do not know what
type codes are and what they are used for by the .net framework

There's a number associated with each type.
I guess that's the "code".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top