Reputation: 701
private void getDetails(out IPAddress ipAddress, out int port)
{
IPAddress Ip;
int Port;
try
{
Ip = IPAddress.Parse(textboxIp.Text);
Port = int.Parse(textboxPort.Text);
}
catch (Exception ex)
{
IPAddress Ip null;
int Port = -1;
MessageBox.Show(ex.Message);
}
}
Why i got this compiler error ? my parameters assigned to value in both cases
Upvotes: 2
Views: 30085
Reputation: 9412
You're not assigning any values to the parameters passed into the method - ipAddress
and port
. Instead of declaring new Ip
and Port
variables, just assign the values to the parameters you've passed in:
private void getDetails(out IPAddress ipAddress, out int port)
{
try
{
ipAddress = IPAddress.Parse(textboxIp.Text);
port = int.Parse(textboxPort.Text);
}
catch (Exception ex)
{
ipAddress = null;
port = -1;
MessageBox.Show(ex.Message);
}
}
EDIT: For other developers, if using "out", you must allow the variable the ability to be set at all points in the function - including "if" statements, and the "catch", like here, just like it was being returned, or it will give the error this guy got.
Upvotes: 16
Reputation: 34218
Quite obviously, you don't assign any value to your out
parameters ipAddress
and port
at any point in the method.
Upvotes: 2
Reputation: 10230
No, you have created another variable - int Port
, that is not the same as out int port
. You are not assigning a value to the actual out parameter. Same goes for the ipAddress
out parameter.
Upvotes: 7
Reputation: 3280
You are not assigning values to both of the out
variables. You are justassigning values to the ones you created inside the method.
Upvotes: 2