Hi,
I'm new to the concept of the keyword 'using' and am not sure if my code is correct.
The compiler issues this warning: "Possibly incorrect assignment to local 'socket' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local."
Does the warning mean that the dispose call is pointless because prior to the start of the using statement socket = null ??
If this is wrong how should I have coded it?
Many thanks.
Code:Socket socket = null; using (socket) { IPHostEntry hostadd = Dns.GetHostEntry(server); IPEndPoint EPhost = new IPEndPoint(hostadd.AddressList[0], 123); socket = new Socket(EPhost.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 8000); socket.SendTo(data, EPhost); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint tempRemoteEP = (EndPoint)sender; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 15000); socket.ReceiveFrom(receiveData, ref tempRemoteEP); if (!IsResponseValid()) { throw new Exception("Invalid response from server: " + server); } }




Reply With Quote