473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type casting confusion

I have worked out most type casting [(int)char] and the likes but I am
curious about one aspect.

Endpoint ep...;
IPEndPoint iep...;
....
ep = (EndPoint)iep;
....
iep = (IPEndPoint)ep;
....

For some reason the above works
(http://www.wmjackson.cable.nu/website/goto.aspx?ID=4) but what I do not
understand is why?

Oddly enough, it was the only way for me to accomplish my task.
Nov 15 '05 #1
7 3197
Wayne M J <no*@home.nor.b igpuddle.com> wrote:
I have worked out most type casting [(int)char] and the likes but I am
curious about one aspect.

Endpoint ep...;
IPEndPoint iep...;
...
ep = (EndPoint)iep;
...
iep = (IPEndPoint)ep;
...

For some reason the above works
(http://www.wmjackson.cable.nu/website/goto.aspx?ID=4) but what I do not
understand is why?

Oddly enough, it was the only way for me to accomplish my task.


Why would you expect it not to work? You don't actually need the cast
in the first one - you can just do:

ep = iep;

because EndPoint is the base class of IPEndPoint.

Don't forget that the variables ep and iep just hold references to
objects. Casting the reference doesn't change the type of the object at
all.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Why would you expect it not to work? You don't actually need the cast
in the first one - you can just do:

ep = iep;

because EndPoint is the base class of IPEndPoint.
When I did the ep = iep; it complained about an implicit cast.

That was when I originally wrote the code under VS.Net2002.
Don't forget that the variables ep and iep just hold references to
objects. Casting the reference doesn't change the type of the object at
all.


Actually I just reread the dox and this is the where I am sure I got the
idea from:
ms-help://MS.MSDNQTR.2003 FEB.1033/cpref/html/frlrfSystemNetS ocketsSocketCla s
sReceiveFromTop ic.htm
Nov 15 '05 #3
Wayne M J <no*@home.nor.b igpuddle.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Why would you expect it not to work? You don't actually need the cast
in the first one - you can just do:

ep = iep;

because EndPoint is the base class of IPEndPoint.


When I did the ep = iep; it complained about an implicit cast.

That was when I originally wrote the code under VS.Net2002.


What exactly do you mean by "complained "? What did it say, exactly?
Could you reproduce it?
Don't forget that the variables ep and iep just hold references to
objects. Casting the reference doesn't change the type of the object at
all.


Actually I just reread the dox and this is the where I am sure I got the
idea from:
ms-help://MS.MSDNQTR.2003 FEB.1033/cpref/html/frlrfSystemNetS ocketsSocketCla s
sReceiveFromTop ic.htm


The cast in that code is unnecessary too. For instance:

using System;
using System.Net;

public class Test
{
static void Main()
{
IPEndPoint sender = new IPEndPoint(IPAd dress.Any, 0);
EndPoint senderRemote = sender;
}
}

compiles with no problems.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
100
Hi Wayne,

IPEndPoint inherits from EndPoint type. So IPEndPoint *is* EndPoint. you can
either reference IPEndPoint objects using IPEndPoint variables or EndPoint
variables.
You don't have to use explicit cast here.

So, legal implicit casts are:
IPEndPoint ipep = .....
EndPoint ep = ipep;
object obj = ipep;

B\rgds
100

"Wayne M J" <no*@home.nor.b igpuddle.com> wrote in message
news:uK******** *****@TK2MSFTNG P11.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Why would you expect it not to work? You don't actually need the cast
in the first one - you can just do:

ep = iep;

because EndPoint is the base class of IPEndPoint.
When I did the ep = iep; it complained about an implicit cast.

That was when I originally wrote the code under VS.Net2002.
Don't forget that the variables ep and iep just hold references to
objects. Casting the reference doesn't change the type of the object at
all.


Actually I just reread the dox and this is the where I am sure I got the
idea from:

ms-help://MS.MSDNQTR.2003 FEB.1033/cpref/html/frlrfSystemNetS ocketsSocketCla s sReceiveFromTop ic.htm

Nov 15 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
What exactly do you mean by "complained "? What did it say, exactly?
Could you reproduce it?


"Cannot implicitly convert 'IPEndPoint' to 'EndPoint'"

But I have just rebuilt the app using the 'ep = iep' and it worked fine.

This I do not understand - is there an equivalent to VB's "Option Strict"
under C#?
Nov 15 '05 #6
Wayne M J <no*@home.nor.b igpuddle.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
What exactly do you mean by "complained "? What did it say, exactly?
Could you reproduce it?
"Cannot implicitly convert 'IPEndPoint' to 'EndPoint'"


That sounds very odd, as it definitely *can* implicitly convert
IPEndPoint to EndPoint. I don't suppose you were trying to pass a
parameter by reference, were you? That would produce an error such as:

Test.cs(13,18): error CS1503: Argument '1': cannot convert from 'ref
System.Net.IPEn dPoint' to 'ref System.Net.EndP oint'

which isn't quite the same, but is far more understandable.
(Reference/out parameters must match in type *exactly*.)
But I have just rebuilt the app using the 'ep = iep' and it worked fine.
So you can't actually reproduce it now? Hmmm...
This I do not understand - is there an equivalent to VB's "Option Strict"
under C#?


No - C# is always typesafe.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
That sounds very odd, as it definitely *can* implicitly convert
IPEndPoint to EndPoint. I don't suppose you were trying to pass a
parameter by reference, were you? That would produce an error such as:

Test.cs(13,18): error CS1503: Argument '1': cannot convert from 'ref
System.Net.IPEn dPoint' to 'ref System.Net.EndP oint'

which isn't quite the same, but is far more understandable.
(Reference/out parameters must match in type *exactly*.)
But I have just rebuilt the app using the 'ep = iep' and it worked fine.

So you can't actually reproduce it now? Hmmm...
This I do not understand - is there an equivalent to VB's "Option

Strict" under C#?


No - C# is always typesafe.


The machine I originally wrote the code on had VS6, VS2002 and VS2003; I
wonder if that may have cause a problem.

The current system only has VS6 and VS2003 installed.
Nov 15 '05 #8

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

Similar topics

1
4072
by: maxim vexler | last post by:
in a book i am ready now : O'Reilly - Web Database Application with PHP and MySQL, 2nd ed. by David Lane, Hugh E. Williams on chapter 9 the author give an example for age validation : (simplified code, not a direct quote) $dob = mktime(0,0,0, 5, 3, 1983); if ((float)$dob > (float)strtotime("-18years")) {
5
8255
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new Derived(); Base* b1 = static_cast<Base*>(d); Base* b2 = (Base*)d; // traditional type casting Such is my understanding from code samples, my own uses, and this: http://www.cplusplus.com/doc/tutorial/tut5-4.html
1
9134
by: JohnK | last post by:
under the covers is type casting in VB.Net the same as C# ? myObject = CType(..,..) in VB.Net vs myObject = (SomeClass)aObject
1
3429
by: chook | last post by:
Wherein differences between type casting in C++ : static_cast, dinamic_cast, reinterpret_cast, const_cast and C type casting, like xxx = (type)yyy; What, when and why is necessary to use?
9
4032
by: Roman Mashak | last post by:
Hello, All! Given the sample piece of code I have: #include <stdio.h> #include <string.h> int main(void) { short int i, j;
23
3504
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement).
16
12785
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
7
4215
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an invisible cast is made? Is there any reason why one would use databinder.eval while in VB.NET? I can see why one might use it in C# so as to avoid specifying the type for the cast but since this is not necessary in VB.NET, I'm not sure I follow. ...
11
32384
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? class Derived (Base): def __init__ (self, base_object): # ( copy all attributes ) ...
0
8397
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
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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
8732
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...
0
7333
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
6167
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
5632
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();...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
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

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.