type casting confusion


W

Wayne M J

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.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Wayne M J said:
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.
 
W

Wayne M J

Jon Skeet said:
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
 
J

Jon Skeet [C# MVP]

Wayne M J said:
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?
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.
 
1

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
 
W

Wayne M J

Jon Skeet said:
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#?
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Wayne M J said:
"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.
 
Ad

Advertisements

W

Wayne M J

Jon Skeet said:
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*.)
fine.

So you can't actually reproduce it now? Hmmm...


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Confused about casting. 3
Type Casting 1
Type casts 6
Type Casting 4
Type Casting 8
Down-casting of Typed Collection (Casting Generic Types?) 1
Type Cast "Object" 2
Runtime Type casting 2

Top