473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Type conversion Syntax

Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something more
profound than a simple syntax preference of the developer.

x = someObject as MySpecificType;

x = (MySpecificType) someObject;
Thanks.
Aug 5 '07 #1
8 3120
Smithers wrote:
Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something more
profound than a simple syntax preference of the developer.

x = someObject as MySpecificType;

x = (MySpecificType) someObject;
Thanks.

One important difference is that the first one will give you a
conversion error if someObject cannot be converted to a MySpecificType
at compile time. The second one will always compile but will throw a
run-time error if someObject cannot be cast to a MySpecificType.

When you know that at compile-time that someObject is of type (or a type
derived from) MySpecificType then use the first syntax. If you don't
know until run-time whether someObject will be of type (or a type
derived from) MySpecificType type then use the second syntax.
--
-glenn-
Aug 5 '07 #2
Smithers wrote:
Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something more
profound than a simple syntax preference of the developer.

x = someObject as MySpecificType;
This will attemt to cast the reference, and if it's not possible, it
will put a null reference in the variable x.

Use this if you aren't certain what the actual type is, and check if the
reference is null to find out if the casting was possible or not.
x = (MySpecificType) someObject;
This will attemt to cast the reference, and it it's not possible, it
will throw an exception.

Use this if you are certain what the actual type is.

--
Göran Andersson
_____
http://www.guffa.com
Aug 5 '07 #3
GlennDoten wrote:
Smithers wrote:
>Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something
more profound than a simple syntax preference of the developer.

x = someObject as MySpecificType;

x = (MySpecificType) someObject;
Thanks.

One important difference is that the first one will give you a
conversion error if someObject cannot be converted to a MySpecificType
at compile time.
No, it won't.
The second one will always compile but will throw a
run-time error if someObject cannot be cast to a MySpecificType.

When you know that at compile-time that someObject is of type (or a type
derived from) MySpecificType then use the first syntax. If you don't
know until run-time whether someObject will be of type (or a type
derived from) MySpecificType type then use the second syntax.
That's completely backwards.

--
Göran Andersson
_____
http://www.guffa.com
Aug 5 '07 #4
Göran Andersson wrote:
GlennDoten wrote:
>Smithers wrote:
>>Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something
more profound than a simple syntax preference of the developer.

x = someObject as MySpecificType;

x = (MySpecificType) someObject;
Thanks.

One important difference is that the first one will give you a
conversion error if someObject cannot be converted to a MySpecificType
at compile time.

No, it won't.
Göran, look at this code:

public class MySpecificType { }
public class MySpecificType2 : MySpecificType { }
public class FooTester
{
public void A()
{
Version v = new Version();
Random r = new Random();
Random r2 = (Random)v;
Random r3 = v as Random;

object o = new object();

MySpecificType x = o as MySpecificType;
MySpecificType y = (MySpecificType)o;

MySpecificType x2 = o as MySpecificType2;
MySpecificType y2 = (MySpecificType2)o;
}
}

On the r2 line, C# gives this error message: "Cannot convert type
'System.Version' to 'System.Random' via a built-in conversion".

On the r3 line, C# gives this error message: "Cannot convert type
'System.Version' to 'System.Random'".

I always thought that C# would let these lines compile and give a
run-time error, but the compiler's smarter than I thought (maybe it's
something new in C# 2.0). The x line compiles but gives this run-time
error: "Unable to cast object of type 'System.Object' to type
'Console_Playground.MySpecificType'".

I think we're both right, and that it depends on whether the compiler
can figure out the type compatibility fully at compile-time or not.
>The second one will always compile but will throw a run-time error if
someObject cannot be cast to a MySpecificType.
You're right; I'm wrong here. I did believe that "as" would always
compile until I tried the r3 line above (at your prompting).
>When you know that at compile-time that someObject is of type MySpecificType (or a
type derived from) MySpecificType then use the first syntax. If you
don't know until run-time whether someObject will be of type (or a
type derived from) MySpecificType type then use the second syntax.

That's completely backwards.
Not quite, I don't think. Although to be clearer I should have said this:

"then use[STRIKE "USE"] the first syntax [ADD "CAN BE USED"]". Though
typically I would use the cast syntax. And I use this pattern all the time:

MySpecificType x = o as MySpecificType;
if (x == null)
{
// x isn't of MySpecificType or doesn't it's type doesn't derive
// from MySpecificType.
}

Sorry if I wasn't clear about that.

Assuming that Smithers means this:

MySpecificType x = someObject as MySpecificType;
MySpecificType x = (MySpecificType) someObject;

and also that Smithers means that someObject is is of type
MySepcificType or a class that derives from MySpecific type. However,
since what he posted is a little ambiguous (he doesn't specify what the
type of x is) I may well be reading his question differently than you
and/or what he means, and maybe the two assumptions I just mentioned
aren't what he intended, but that's the way I read he question. Maybe
Smithers will clarify for us.

The bottom line is the first variant (again, assuming these two
assumptions) is useful when you don't want an exception throw at
run-time and will check the variable to see if it is null or not, and
the second variant is useful when you pretty much now the type of the
variable being cast and if you don't know 100% you don't mind an
exception being thrown at run-time.

--
-glenn-
Aug 5 '07 #5
RE:
<< Maybe Smithers will clarify for us. >>

Gladly - and thanks for the good information so far. The big picture is that
I'm writing an application that is extensible via a plug-in architecture
(thus the use of Activator.CreateInstance() and the subsequent need to
convert to a specific type).

Here is the relevant code. Notice Lines 7 and 8 provide the alternative
conversion syntax in question.

1 private List<CompanyNameHere.CommonInstallerTypes.Installe r>
m_InstallerInstances;
2 Type installerTypeReference = null;
3 System.Reflection.Assembly loadedAssembly =
System.Reflection.Assembly.LoadFile(pathToAssembly );
4 installerTypeReference =
loadedAssembly.GetType(currentInstaller.ClassName) ;
5 if (installerTypeReference != null)
6 {
7 installerInstance = (CompanyNameHere.CommonInstallerTypes.Installer)
Activator.CreateInstance(installerTypeReference);
8 // installerInstance = Activator.CreateInstance(installerTypeReference)
as CompanyNameHere.CommonInstallerTypes.Installer;
9 m_InstallerInstances.Add(installerInstance);
10 }
Thanks for the great dialog. It is very helpful. You can see I'm wanting to
understand this and not simply "get it to work".

-"Smithers"
Aug 5 '07 #6
Smithers wrote:
RE:
<< Maybe Smithers will clarify for us. >>

Gladly - and thanks for the good information so far. The big picture is that
I'm writing an application that is extensible via a plug-in architecture
(thus the use of Activator.CreateInstance() and the subsequent need to
convert to a specific type).

Here is the relevant code. Notice Lines 7 and 8 provide the alternative
conversion syntax in question.

1 private List<CompanyNameHere.CommonInstallerTypes.Installe r>
m_InstallerInstances;
2 Type installerTypeReference = null;
3 System.Reflection.Assembly loadedAssembly =
System.Reflection.Assembly.LoadFile(pathToAssembly );
4 installerTypeReference =
loadedAssembly.GetType(currentInstaller.ClassName) ;
5 if (installerTypeReference != null)
6 {
7 installerInstance = (CompanyNameHere.CommonInstallerTypes.Installer)
Activator.CreateInstance(installerTypeReference);
8 // installerInstance = Activator.CreateInstance(installerTypeReference)
as CompanyNameHere.CommonInstallerTypes.Installer;
9 m_InstallerInstances.Add(installerInstance);
10 }
Both will work. You might use casting--as in line 7--if you were sure
that the ClassName variable always specified the name of a type that is
of type Installer, or a type that derives from type Installer. But even
if the type specified by ClassName were not such a type the code would
still work, it would just throw an exception when line 7 executes. This
may be perfectly acceptable behavior so long as your callers know to
trap for this potential exception and recover accordingly.

Line 8 would work just as well, but typically you would use it if you
had a corresponding line 8a that did something like this:

if (installInstance == null) { /* Handle the incorrect type. */ }

But without such a check then it doesn't matter whether you use a cast
(which will throw an exception if the wrong type is created) or whether
you use the "as" keyword (which will not throw an exception but will
return a null if a wrong type is created).

So which one you use depends on how you want this code to behave.
Personally, I use a cast when creating a "late-bound" object like this
because I know that the type name specified in the ClassName variable is
going to specify a proper type, and if it doesn't I'll catch that bug
during testing. Though you can't always make that assumption, in which
case I'd probably still just wrap a "try" around the cast. Another
pattern I use for this type of code is to replace your line 7 with
something like this:

object instance = Activator.CreateInstance(installerTypeReference);
Debug.Assert(instance is Installer);
installerInstance = (Installer)instance;

Make sense?
Thanks for the great dialog. It is very helpful. You can see I'm wanting to
understand this and not simply "get it to work".
Same thing here!

--
-glenn-
Aug 5 '07 #7
Yes - it all makes sense... exactly the sort of perspective I was looking
for. Thank you very much.

-"Smithers"


"GlennDoten" <gd****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Smithers wrote:
>RE:
<< Maybe Smithers will clarify for us. >>

Gladly - and thanks for the good information so far. The big picture is
that I'm writing an application that is extensible via a plug-in
architecture (thus the use of Activator.CreateInstance() and the
subsequent need to convert to a specific type).

Here is the relevant code. Notice Lines 7 and 8 provide the alternative
conversion syntax in question.

1 private List<CompanyNameHere.CommonInstallerTypes.Installe r>
m_InstallerInstances;
2 Type installerTypeReference = null;
3 System.Reflection.Assembly loadedAssembly =
System.Reflection.Assembly.LoadFile(pathToAssembl y);
4 installerTypeReference =
loadedAssembly.GetType(currentInstaller.ClassName );
5 if (installerTypeReference != null)
6 {
7 installerInstance =
(CompanyNameHere.CommonInstallerTypes.Installer )
Activator.CreateInstance(installerTypeReference );
8 // installerInstance =
Activator.CreateInstance(installerTypeReference ) as
CompanyNameHere.CommonInstallerTypes.Installer;
9 m_InstallerInstances.Add(installerInstance);
10 }

Both will work. You might use casting--as in line 7--if you were sure that
the ClassName variable always specified the name of a type that is of type
Installer, or a type that derives from type Installer. But even if the
type specified by ClassName were not such a type the code would still
work, it would just throw an exception when line 7 executes. This may be
perfectly acceptable behavior so long as your callers know to trap for
this potential exception and recover accordingly.

Line 8 would work just as well, but typically you would use it if you had
a corresponding line 8a that did something like this:

if (installInstance == null) { /* Handle the incorrect type. */ }

But without such a check then it doesn't matter whether you use a cast
(which will throw an exception if the wrong type is created) or whether
you use the "as" keyword (which will not throw an exception but will
return a null if a wrong type is created).

So which one you use depends on how you want this code to behave.
Personally, I use a cast when creating a "late-bound" object like this
because I know that the type name specified in the ClassName variable is
going to specify a proper type, and if it doesn't I'll catch that bug
during testing. Though you can't always make that assumption, in which
case I'd probably still just wrap a "try" around the cast. Another pattern
I use for this type of code is to replace your line 7 with something like
this:

object instance = Activator.CreateInstance(installerTypeReference);
Debug.Assert(instance is Installer);
installerInstance = (Installer)instance;

Make sense?
>Thanks for the great dialog. It is very helpful. You can see I'm wanting
to understand this and not simply "get it to work".

Same thing here!

--
-glenn-

Aug 5 '07 #8
This is still not quite right.

the as operator is a reference converter. This means it will convert an
object reference from one type to another based on the rules of polymorphism.
e.g
Imagin you have this object hierachy:
System.Object
MyClassA
MyClassB
MyClassC
MyClassD

So as MyClassA is at the same level as MyClassB this will fail as you cant
point to an instance of MyClassA with a reference of type MyClassB

MyClassA inst = new MyClassA();
MyClassB inst2 = isnt as MyClassB();

This However will work as the rules of polymorphism allow this
Object inst = new MyClassC();
MyClassB inst2 = isnt as MyClassB();
MyClassC inst3 = isnt2 as MyClassC();

This however will fail:

Object inst = new MyClassC();
MyClassD inst3 = isnt2 as MyClassD();

as an instance of MyClassC cant be made more special.
As you can see the 'as' operator works solely on the reference and leaves
the actual instance untouched in all cases. so the reference returned is to
the same memory location as the original but it a different type.

Casting is a little different, the rules above will apply the same way for
casting

BUT

you can add implicit and explicit conversion functions to make casting work
for completly different types. Castign is used this wayto make a different
instance of a different class which is a conversion of the original.

This allows this to work

int a = 1;
double b = (double)a;

int and double are unrelated classes but have conversion functions to allow
you to convert one to the other. In the above case, the memory rerence of b
and a are different (I know this is the case as much to the stack based
nature of them but its the same with reference types and their members).

Hopefully this highlights the difference between the 2.

as = pointer conversion only
(cast) = automatic pointer conversion for reference types or object
conversion for all supporting types.
The other difference is 'as' doesnt throw an expcetion but returns a null if
it fails, therefore it only works for reference types. (casting) throws an
exception on failure and cant be used on value types too.

The compiler support for detecting improper conversions is just MS being
clever. If the compiler can detect a garunteed failure it will throw an
error. The same as writing int i = 10/0; throws a divide by zero exception.
Hope this helps to clarify

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Göran Andersson" wrote:
Smithers wrote:
Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something more
profound than a simple syntax preference of the developer.

x = someObject as MySpecificType;

This will attemt to cast the reference, and if it's not possible, it
will put a null reference in the variable x.

Use this if you aren't certain what the actual type is, and check if the
reference is null to find out if the casting was possible or not.
x = (MySpecificType) someObject;

This will attemt to cast the reference, and it it's not possible, it
will throw an exception.

Use this if you are certain what the actual type is.

--
Göran Andersson
_____
http://www.guffa.com
Aug 6 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
19
by: Randy Yates | last post by:
Consider the following code: #include "dsptypes.h" /* definitions */ #define VECTOR_LENGTH 64 /* local variables */
11
by: Peter Oliphant | last post by:
I've been trying all morning to convert my 2003 project (managed) to 2005 (/clr since I have both managed and unmanaged code). I'm guessing I have tens of thousands of lines of code to change. Did...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
3
by: PeterK | last post by:
I am trying to set Public overridable CreditlimitS() as System.Data.SqlTypes.SqlMoney to Creditlimit as Double like CreditLimitS=creditlimit and get this error "Value of type double cannot be...
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
1
by: zaeminkr | last post by:
I have a question about type conversion function for user defined type. I have two classes class DRect { private : double x0; double y0;
22
by: Ruben | last post by:
Why would a method that is defined to return a reference such as with the operator overload of , operator, href& operator(int index){ return _array; } not cause a type mismatch compiler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.