473,509 Members | 2,671 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 3184
Wayne M J <no*@home.nor.bigpuddle.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.2003FEB.1033/cpref/html/frlrfSystemNetSocketsSocketClas
sReceiveFromTopic.htm
Nov 15 '05 #3
Wayne M J <no*@home.nor.bigpuddle.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.2003FEB.1033/cpref/html/frlrfSystemNetSocketsSocketClas
sReceiveFromTopic.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(IPAddress.Any, 0);
EndPoint senderRemote = sender;
}
}

compiles with no problems.

--
Jon Skeet - <sk***@pobox.com>
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.bigpuddle.com> wrote in message
news:uK*************@TK2MSFTNGP11.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.2003FEB.1033/cpref/html/frlrfSystemNetSocketsSocketClas sReceiveFromTopic.htm

Nov 15 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.bigpuddle.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.IPEndPoint' to 'ref System.Net.EndPoint'

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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.IPEndPoint' to 'ref System.Net.EndPoint'

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
4067
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 :...
5
8238
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...
1
9128
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
3414
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
4004
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
3477
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...
16
12772
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
4211
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...
11
32226
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? ...
0
7410
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...
1
7067
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
5650
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,...
1
5060
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...
0
4729
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...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.