473,396 Members | 1,968 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.

compile time casting guarantee

I'm wondering if there's a compile time cast gaurantee if any?

Given the following:

//----- begin sample code
public interface IRegion
{
string Text { get; }
RectangleF Area { get; }
}

public class Region : IRegion
{
public Region()
{
}
public Region(string text)
{
this._text = text;
}
public string Text { get { return this._text; } } // takes care of
IRegion.Text
internal protected void Set_Text(string value) { this._text = value; }
public RectangleF Area { get { return RectangleF.Empty; } } //takes care
of IRegion.Area
}
//---- end of code

Since Region specifically implements IRegion, we are guaranteed that a cast
from Region to IRegion will work, so I'm hoping for a more strict compile
time casting check that would obviously fail if the Region class doesnt
implement IRegion anymore, instead of finding out at runtime that the cast
failed.

Are there any plans for this?

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]
Nov 15 '05 #1
3 1784
Eric,

Unfortunately, no, there is no compile-time check to see if it
implements the interface. You will have to wait until runtime to find out
if the cast is valid.

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

"Eric Newton" <er**@cc.ensoft-software.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
I'm wondering if there's a compile time cast gaurantee if any?

Given the following:

//----- begin sample code
public interface IRegion
{
string Text { get; }
RectangleF Area { get; }
}

public class Region : IRegion
{
public Region()
{
}
public Region(string text)
{
this._text = text;
}
public string Text { get { return this._text; } } // takes care of
IRegion.Text
internal protected void Set_Text(string value) { this._text = value; }
public RectangleF Area { get { return RectangleF.Empty; } } //takes care of IRegion.Area
}
//---- end of code

Since Region specifically implements IRegion, we are guaranteed that a cast from Region to IRegion will work, so I'm hoping for a more strict compile
time casting check that would obviously fail if the Region class doesnt
implement IRegion anymore, instead of finding out at runtime that the cast
failed.

Are there any plans for this?

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

Nov 15 '05 #2
Let us consider the following

Region reg = new Region();
IRegion ireg = reg;

You don't need any cast on the second line if Region implements the IRegion
interface (*)

If Region did not implement IRegion, you whould need to write:

IRegion ireg = (IRegion)reg;

The compiler should generate an error if Region was a sealed class (I did
not check if it actually does this but a good compiler should), but
otherwise (Region not sealed), static type analysis is insufficient to
conclude.

(*) Would be nice if the compiler generated warning messages when it
encouters "superfluous" casts like in this case.

Bruno.

"Eric Newton" <er**@cc.ensoft-software.com> a écrit dans le message de
news:uf**************@TK2MSFTNGP10.phx.gbl...
I'm wondering if there's a compile time cast gaurantee if any?

Given the following:

//----- begin sample code
public interface IRegion
{
string Text { get; }
RectangleF Area { get; }
}

public class Region : IRegion
{
public Region()
{
}
public Region(string text)
{
this._text = text;
}
public string Text { get { return this._text; } } // takes care of
IRegion.Text
internal protected void Set_Text(string value) { this._text = value; }
public RectangleF Area { get { return RectangleF.Empty; } } //takes care of IRegion.Area
}
//---- end of code

Since Region specifically implements IRegion, we are guaranteed that a cast from Region to IRegion will work, so I'm hoping for a more strict compile
time casting check that would obviously fail if the Region class doesnt
implement IRegion anymore, instead of finding out at runtime that the cast
failed.

Are there any plans for this?

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

Nov 15 '05 #3
Ok cool, I was overlooking a very simple solution

When Region directly implements IRegion then the compiler does in fact allow
it
and when region doesnt implement IRegion then the compiler throws an error
saying it cannot implicitly cast which is the correct operation...

I knew I had to be overlooking something obvious ;-)
--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Let us consider the following

Region reg = new Region();
IRegion ireg = reg;

You don't need any cast on the second line if Region implements the IRegion interface (*)

If Region did not implement IRegion, you whould need to write:

IRegion ireg = (IRegion)reg;

The compiler should generate an error if Region was a sealed class (I did
not check if it actually does this but a good compiler should), but
otherwise (Region not sealed), static type analysis is insufficient to
conclude.

(*) Would be nice if the compiler generated warning messages when it
encouters "superfluous" casts like in this case.

Bruno.

"Eric Newton" <er**@cc.ensoft-software.com> a écrit dans le message de
news:uf**************@TK2MSFTNGP10.phx.gbl...
I'm wondering if there's a compile time cast gaurantee if any?

Given the following:

//----- begin sample code
public interface IRegion
{
string Text { get; }
RectangleF Area { get; }
}

public class Region : IRegion
{
public Region()
{
}
public Region(string text)
{
this._text = text;
}
public string Text { get { return this._text; } } // takes care of
IRegion.Text
internal protected void Set_Text(string value) { this._text = value; } public RectangleF Area { get { return RectangleF.Empty; } } //takes

care
of IRegion.Area
}
//---- end of code

Since Region specifically implements IRegion, we are guaranteed that a

cast
from Region to IRegion will work, so I'm hoping for a more strict compile time casting check that would obviously fail if the Region class doesnt
implement IRegion anymore, instead of finding out at runtime that the cast failed.

Are there any plans for this?

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]


Nov 15 '05 #4

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

Similar topics

17
by: Bryan Bullard | last post by:
hi, is there a way to force the user of an object to catch exceptions of specific type? for instance a compile error is issued if exception 'ExeceptionXXX' thrown by method...
6
by: DCSudolcan | last post by:
I know that a program can create and properly initialize an array of pointers to functions at build time, but can something like the following be done at build time? void foo(void); unsigned...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
31
by: Twister | last post by:
Hi All, I have a question which might sound very basic. I have a simple structure: struct simple{ void *buffer; }; typedef struct simple Simple;
23
by: Christian Christmann | last post by:
Hi, a question on castings. My code example: #include <stdio.h> int main( void ) {
8
by: lennin | last post by:
#define NULL 0 int main() { unsigned int p = NULL; /* Empty your mind */ if (p!=NULL) delete &p;
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.