473,796 Members | 2,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Empt y; } } //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 1812
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.co m

"Eric Newton" <er**@cc.enso ft-software.com> wrote in message
news:uf******** ******@TK2MSFTN GP10.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.Empt y; } } //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 "superfluou s" casts like in this case.

Bruno.

"Eric Newton" <er**@cc.enso ft-software.com> a écrit dans le message de
news:uf******** ******@TK2MSFTN GP10.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.Empt y; } } //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******@clu b-internet.fr> wrote in message
news:%2******** ********@TK2MSF TNGP10.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 "superfluou s" casts like in this case.

Bruno.

"Eric Newton" <er**@cc.enso ft-software.com> a écrit dans le message de
news:uf******** ******@TK2MSFTN GP10.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.Empt y; } } //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
3148
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 'ThrowsExceptionXXX' is not caught. thanks,
6
2070
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 char myArray={ (unsigned char) (foo&0xFF), (unsigned char) ((foo&0xFF00)>>8), (unsigned char) ((foo&0xFF0000)>>16),
14
2635
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 says this about the <ctype.h> functions: "The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the
31
3200
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
31
3870
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
1975
by: Christian Christmann | last post by:
Hi, a question on castings. My code example: #include <stdio.h> int main( void ) {
8
1427
by: lennin | last post by:
#define NULL 0 int main() { unsigned int p = NULL; /* Empty your mind */ if (p!=NULL) delete &p;
16
5434
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 first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
17
2232
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 type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.