473,396 Members | 1,766 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,396 software developers and data experts.

Quick Interface Question...

//Let's say the Document class implements the IStorable interface...

class Document : IStorable
{...}

//Then we create a document object...
Document doc = new Document();

//Then we have the following line of code(the part I don't understand)...
IStorable isDoc = doc as Istorable;

Is this last line the same as saying:
IStorable isDoc = (IStorable) doc;

In other words, is the "as" word, just another way of casting?
Thanks,

Jeff

Nov 16 '05 #1
5 1270
Yes, as casts, but rather than throwing an InvalidCastException if it
cannot property type cast from one type to another it sets the reference
to null. So you should always check that the resulting object is not
null before trying to work with it or you will receive a
NullReferenceException.

Thanks,
Steven

z_learning_tester wrote:
//Let's say the Document class implements the IStorable interface...

class Document : IStorable
{...}

//Then we create a document object...
Document doc = new Document();

//Then we have the following line of code(the part I don't understand)...
IStorable isDoc = doc as Istorable;

Is this last line the same as saying:
IStorable isDoc = (IStorable) doc;

In other words, is the "as" word, just another way of casting?
Thanks,

Jeff

Nov 16 '05 #2
z_learning_tester <so*****@microsoft.com> wrote:
In other words, is the "as" word, just another way of casting?


See http://www.pobox.com/~skeet/csharp/faq#cast.as

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Oh, so that explains the if(Istorable != Null)... that followed ;-)
If the 'as' check for a valid cast failed, it would return null...
Thanks!

Jeff

"Steven Behnke" <st****@xsdev.net> wrote in message
news:dVMDc.961$876.426@fed1read07...
Yes, as casts, but rather than throwing an InvalidCastException if it
cannot property type cast from one type to another it sets the reference
to null. So you should always check that the resulting object is not
null before trying to work with it or you will receive a
NullReferenceException.

Thanks,
Steven

z_learning_tester wrote:
//Let's say the Document class implements the IStorable interface...

class Document : IStorable
{...}

//Then we create a document object...
Document doc = new Document();

//Then we have the following line of code(the part I don't understand)... IStorable isDoc = doc as Istorable;

Is this last line the same as saying:
IStorable isDoc = (IStorable) doc;

In other words, is the "as" word, just another way of casting?
Thanks,

Jeff

Nov 16 '05 #4
Thanks for the reference.
I wish C# would last a little longer.
Now I'm afraid that by the time I learn it, Whidbey will be out and I'll
have to start over.
Oh well, that's technology...
Thanks again!

Jeff
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
z_learning_tester <so*****@microsoft.com> wrote:
In other words, is the "as" word, just another way of casting?


See http://www.pobox.com/~skeet/csharp/faq#cast.as

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
z_learning_tester <so*****@microsoft.com> wrote:
Thanks for the reference.
I wish C# would last a little longer.
Now I'm afraid that by the time I learn it, Whidbey will be out and I'll
have to start over.
Oh well, that's technology...


You certainly won't have to start over. C# v2.0 will have things added,
but I don't think much (if anything) will be taken away. Sure, you may
never need to use ArrayList.ToArray(Type) again, but most of what
you're learning now will still be useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

0
by: Tom | last post by:
I have made in the past a bunch of VB apps to test hardware. They work and some call functions written in C++ (dll calls). I need to make a quick, dirty and cheap console app to access a PCI...
3
by: barb | last post by:
I'm developing an assembly that will be used from ASP (not ASP.NET). Can I overload methods? My initial tests suggest that I can't. Thanks, Barb
8
by: Menon | last post by:
Hi folks I am not at all an SQL server guy (more of an Oracle developer.) I wanted to know if you guys could tell me if SQL Server supports objects in the database. If so, could you kindly point...
4
by: motessa | last post by:
Hello All, I have a quick, easy question. Hopefully, someone will give me a hint. I will greatly appreciate any help. I am pretty new to Access. I have created a Main Menu in Form in Access....
2
by: Andrew Bullock | last post by:
Hi, I'm trying to use direct show to grab arbitrary frames from a DV .avi. I'm using the PlayWnd code from this example: http://www.codeproject.com/cs/media/directshownet.asp Currently an...
0
by: r035198x | last post by:
Overriding therefore allows you to use the same method name to refer to differently implemented methods. Here is an example using the famous shapes example. class Shape { public Shape()...
1
by: brianlanning | last post by:
I have a class that represents a business object. Currently, this class exists as a vb6 "class" which will be ported to .net. The plan is to separate this out into a .net object residing in a web...
1
by: =?Utf-8?B?Tmljaw==?= | last post by:
I do not know what started this, but my quick launch and start menu disappear after one second, and I can't click on anything.I tried system restore to before this started, but it keeps failing and...
3
by: Mat | last post by:
Hi all, It's been a long while since I've posted or used access and one of the things that had me stop was the realisation I couldn't dynamically add new controls to a form. I am using Access...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.