Can an object be cast into a certain type?

C

CA

Hi,

I have a function where I would like to test whether an object is of a
certain type. Here is my code so far.

public bool HasValidType(Type t, object val)
{
try
{
if (t==typeof(double))
{
double x = (double) val;
}
else if (t==typeof(System.DateTime))
{
DateTime x = (DateTime) val;
}
return true;
}
catch
{
return false;
}
}

I guess what I would like is the ability to write a statement of the
form

try
{
t variable = (t) val;
return true;
}
catch
{
return false;
}

Can I do this using reflection? I appreciate any help in this matter.
Thanks in advance.

Chris
 
B

Blake

Hey Chris,

Would this work for your purposes?

public bool HasValidType(Type t, object val)
{
return val.GetType().Equals(t);
}

-Blake
 
J

Jon Skeet

CA said:
I have a function where I would like to test whether an object is of a
certain type.

I guess what I would like is the ability to write a statement of the
form

try
{
t variable = (t) val;
return true;
}
catch
{
return false;
}

Can I do this using reflection? I appreciate any help in this matter.

Casting is just there to tell the compiler what it can do - what would
you be able to do with "variable" if you *could* do such a cast?
Basically a cast is providing compile-time information - and in this
case the information isn't available at compile-time.

Now, what are you *actually* trying to do? What's the bigger picture?
We may be able to help you find a better approach.
 
E

Eric Cadwell

Use the 'as' syntax for testing if a cast succeeded. If you are like me, you
are trying to minimize the number of try, catch statements.

object [] myObjects = new object[6];
myObjects[0] = new MyClass1();
myObjects[1] = new MyClass2();
myObjects[2] = "hello";
myObjects[3] = 123;
myObjects[4] = 123.4;
myObjects[5] = null;

for (int i=0; i<myObjects.Length; ++i)
{
string s = myObjects as string;
Console.Write ("{0}:", i);
if (s != null)
Console.WriteLine ( "'" + s + "'" );
else
Console.WriteLine ( "not a string" );
}

HTH,
Eric Cadwell
http://www.origincontrols.com
 
P

ppyrstr

Chris,

If you're still looking for ideas about this you could use
the "is" functionality:

public bool HasValidType(object val)
{
if (val is double)
{
return true
}
else if (val is System.DateTime)
{
return true;
}
return false;
}
 
C

CA

Basically what I am trying to do is to develop a generic function at
the base level class that verifies whether or not the data entries to
the user interface are valid.

So the base class would have the form:

public abstract class BaseDataEntry
{
public bool HasValidType(Type t, Value o);
public abstract object Value { get; set; }
}

At the level of the derived class, one would just set the type and
hopefully the base class function would determine whether or not it is
valid. I suppose I could override the function at the derived class
level.

I guess what I am try to prevent is to add an additional else if each
time I add a new class. The reason why comparing the types directly
does not work for me is that

float valF = 1.0F;
bool ret = HasValidType(typeof(double), valF); // should return true

Thanks for taking the time to reply to my post.

Regards

Chris

--------------------------------------------------------

The reason I am reluctant to compare the types directly as suggested
earlier
Eric Cadwell said:
Use the 'as' syntax for testing if a cast succeeded. If you are like me, you
are trying to minimize the number of try, catch statements.

object [] myObjects = new object[6];
myObjects[0] = new MyClass1();
myObjects[1] = new MyClass2();
myObjects[2] = "hello";
myObjects[3] = 123;
myObjects[4] = 123.4;
myObjects[5] = null;

for (int i=0; i<myObjects.Length; ++i)
{
string s = myObjects as string;
Console.Write ("{0}:", i);
if (s != null)
Console.WriteLine ( "'" + s + "'" );
else
Console.WriteLine ( "not a string" );
}

HTH,
Eric Cadwell
http://www.origincontrols.com


CA said:
Hi,

I have a function where I would like to test whether an object is of a
certain type. Here is my code so far.

public bool HasValidType(Type t, object val)
{
try
{
if (t==typeof(double))
{
double x = (double) val;
}
else if (t==typeof(System.DateTime))
{
DateTime x = (DateTime) val;
} return true;
}
catch
{
return false;
}
}

I guess what I would like is the ability to write a statement of the
form

try
{
t variable = (t) val;
return true;
}
catch
{
return false;
}

Can I do this using reflection? I appreciate any help in this matter.
Thanks in advance.

Chris
 
J

Jon Skeet

CA said:
Basically what I am trying to do is to develop a generic function at
the base level class that verifies whether or not the data entries to
the user interface are valid.

So the base class would have the form:

public abstract class BaseDataEntry
{
public bool HasValidType(Type t, Value o);
public abstract object Value { get; set; }
}

At the level of the derived class, one would just set the type and
hopefully the base class function would determine whether or not it is
valid. I suppose I could override the function at the derived class
level.

I guess what I am try to prevent is to add an additional else if each
time I add a new class. The reason why comparing the types directly
does not work for me is that

float valF = 1.0F;
bool ret = HasValidType(typeof(double), valF); // should return true

Well, float and double are special cases, because there is an implicit
conversion from float to double. However, for classes you can use
Type.IsAssignableFrom.
 
J

Jeffrey Tan[MSFT]

Hi ,

For reference type, you can judge this by the "is" keyword.

For value type, I think you should create a signable table for
suitable conversion, such as

int-value&float-type,int-value&double-type
float-value&double-type,int-value&long-type
and so on,

In you function, you can determine the logic through this table.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: (e-mail address removed) (CA)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: Can an object be cast into a certain type?
| Date: 23 Sep 2003 21:40:47 -0700
| Organization: http://groups.google.com/
| Lines: 105
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
| NNTP-Posting-Host: 24.162.9.9
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1064378448 825 127.0.0.1 (24 Sep 2003
04:40:48 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: 24 Sep 2003 04:40:48 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!prodigy.com!prodigy.com!pd2nf1so.cg.shawcable.net!residential.shaw.ca!sn-x
it-03!sn-xit-01!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186939
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Basically what I am trying to do is to develop a generic function at
| the base level class that verifies whether or not the data entries to
| the user interface are valid.
|
| So the base class would have the form:
|
| public abstract class BaseDataEntry
| {
| public bool HasValidType(Type t, Value o);
| public abstract object Value { get; set; }
| }
|
| At the level of the derived class, one would just set the type and
| hopefully the base class function would determine whether or not it is
| valid. I suppose I could override the function at the derived class
| level.
|
| I guess what I am try to prevent is to add an additional else if each
| time I add a new class. The reason why comparing the types directly
| does not work for me is that
|
| float valF = 1.0F;
| bool ret = HasValidType(typeof(double), valF); // should return true
|
| Thanks for taking the time to reply to my post.
|
| Regards
|
| Chris
|
| --------------------------------------------------------
|
| The reason I am reluctant to compare the types directly as suggested
| earlier
| > Use the 'as' syntax for testing if a cast succeeded. If you are like
me, you
| > are trying to minimize the number of try, catch statements.
| >
| > object [] myObjects = new object[6];
| > myObjects[0] = new MyClass1();
| > myObjects[1] = new MyClass2();
| > myObjects[2] = "hello";
| > myObjects[3] = 123;
| > myObjects[4] = 123.4;
| > myObjects[5] = null;
| >
| > for (int i=0; i<myObjects.Length; ++i)
| > {
| > string s = myObjects as string;
| > Console.Write ("{0}:", i);
| > if (s != null)
| > Console.WriteLine ( "'" + s + "'" );
| > else
| > Console.WriteLine ( "not a string" );
| > }
| >
| > HTH,
| > Eric Cadwell
| > http://www.origincontrols.com
| >
| >
| > | > > Hi,
| > >
| > > I have a function where I would like to test whether an object is of a
| > > certain type. Here is my code so far.
| > >
| > > public bool HasValidType(Type t, object val)
| > > {
| > > try
| > > {
| > > if (t==typeof(double))
| > > {
| > > double x = (double) val;
| > > }
| > > else if (t==typeof(System.DateTime))
| > > {
| > > DateTime x = (DateTime) val;
| > > }
| > return true;
| > > }
| > > catch
| > > {
| > > return false;
| > > }
| > > }
| > >
| > > I guess what I would like is the ability to write a statement of the
| > > form
| > >
| > > try
| > > {
| > > t variable = (t) val;
| > > return true;
| > > }
| > > catch
| > > {
| > > return false;
| > > }
| > >
| > > Can I do this using reflection? I appreciate any help in this matter.
| > > Thanks in advance.
| > >
| > > Chris
|
 

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