473,804 Members | 3,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing zero (int) to an enum in .NET Socket constructor?!

This code compiles (this is how it should work):
Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);

This code does not compile (passing an int instead of an AddressFamily is
bad):
Socket sock = new Socket(1, SocketType.Stre am, ProtocolType.Tc p);

But why on earth does not compile:
Socket sock = new Socket(0, SocketType.Stre am, ProtocolType.Tc p);

Is the number zero some kind of special case here? Why is zero accepted when
the parameter type clearly says that an AddressFamily enum value is expected?!
regards,
martin
Apr 5 '06 #1
1 1788

martin wrote:
This code compiles (this is how it should work):
Socket sock = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);

This code does not compile (passing an int instead of an AddressFamily is
bad):
Socket sock = new Socket(1, SocketType.Stre am, ProtocolType.Tc p);

But why on earth does not compile:
Typo, I assume.
Socket sock = new Socket(0, SocketType.Stre am, ProtocolType.Tc p);

Is the number zero some kind of special case here? Why is zero accepted when
the parameter type clearly says that an AddressFamily enum value is expected?!


Yes, zero is a special case. Here is a shorter example:

System.Net.Sock ets.AddressFami ly af1 = 1; // no compile
System.Net.Sock ets.AddressFami ly af1 = 0; // compile

The reason can be found in section 6.1.3 of the C# spec:

<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/csspec/html/vclrfcsharpspec _6_1_2.asp>

(sic, the url says 6.1.2 but the page is 6.1.3 :))
C# Language Specification

6.1.3 Implicit enumeration conversions
An implicit enumeration conversion permits the decimal-integer-literal
0 to be converted to any enum-type.


Elsewhere in the docs, we are also told "The default value of an enum E
is the value produced by the expression (E)0."

--
Larry Lard
Replies to group please

Apr 5 '06 #2

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

Similar topics

1
2333
by: Clive | last post by:
Hello everyone, This is my first time posting, up until now i've only used google to seach :) I'm creating a multiplayer minesweeper game in Windows, so I have been reading up on CSockets. I am begining to have a grasp on them, but I have this one query. If a socket some data comes along that isnt part of my protocol, I want to
20
4852
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
3
4147
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API calls are needed! // using System; using System.Runtime.InteropServices;
0
3291
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer = 0ms.....60ms. I'm already timing the callbacks using the Win32API QueryPerformanceCounter/Frequency. The code I have is actually performing the callback and it looks like it is close to 1ms but I now need to get my object passed to this static...
8
4416
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and click a button to determine whether the zip code is unique. If the zip code is not unique, another form/dialog is displayed (fclsLookup) - lookup form/dialog. The zip code is passed to the lookup form/dialog by reference. I then load a...
22
4046
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to EndReceive return zero bytes length , also the buffer is empty. here is the code: public static byte...
1
6128
by: Steve Richter | last post by:
when I call a win32 api like CreateFile, I would like to be able to pass an enumerated value as the parameter value: hFile = CreateFile( InPath, GENERIC_READ, ShareMode.Read, // this is an enum IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); I know I can cast the enum: (uint) ShareMode.Read
2
4947
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept that connection and get the socket. I try to signal an event when this happens and pass that new socket object when the event happens, but when I try to pass the socket with this statement: ConnectionReceived(this, work); I get this exception in...
4
5308
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array of struct */ #include <stdio.h>
0
9704
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
9571
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
10561
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
10318
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
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
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
6845
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
4277
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
2976
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.