tags:

views:

90

answers:

5

In a c# initialiser, I want to not set a property if a condition is false.

Something like this:

ServerConnection serverConnection = new ServerConnection()  
{  
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    if (!windowsAuthentication)
    {
        Login = user,  
        Password = password  
    }
};

It can be done? How?

+7  A: 

This is not possible in an initializer; you need to make a separate if statement.

Alternatively, you may be able to write

ServerConnection serverConnection = new ServerConnection()  
{  
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    Login = windowsAuthentication ? null : user,  
    Password = windowsAuthentication ? null : password
};

(Depending on how your ServerConnection class works)

SLaks
Why was this downvoted?
SLaks
That was my fault. I originally downvoted you when all you had in your answer was that it's not possible, before you added your tertiary operators. Now it's apparently locked in and I can't rescind the vote. Thanks server barfs!
Randolpho
@Randolpho: Now you can rescind it.
SLaks
Ok... it's done.
Randolpho
+3  A: 

Note: I do not recommend this approach, but if it must be done in an initializer (i.e. you're using LINQ or some other place where it has to be a single statement), you can use this:

ServerConnection serverConnection = new ServerConnection()
{
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    Login = windowsAuthentication ? null : user,
    Password = windowsAuthentication ? null : password,   
}
Randolpho
A: 

You can't do this; C# initializers are a list of name = value pairs. See here for details: http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic5

You'll need to move the if block to the following line.

Tim Robinson
Hi, why the downvote? Is this information not correct?
Tim Robinson
+4  A: 

I suspect this would work, but using logic this way sort of defeats the purpose of using the initializer.

ServerConnection serverConnection = new ServerConnection()  
{  
    ServerInstance = server,  
    LoginSecure = windowsAuthentication,  
    Login = windowsAuthentication ? null : user,
    Password = windowsAuthentication ? null :password
};
Jay
+1  A: 

As others mentioned, this can't exactly be done within an initializer. Is it acceptable to just assign null to the property instead of not setting it at all? If so, you can use the approach that others have pointed out. Here's an alternative that accomplishes what you want and still uses the initializer syntax:

ServerConnection serverConnection;
if (!windowsAuthentication)
{
    serverConection = new ServerConnection()
    {
        ServerInstance = server,
        LoginSecure = windowsAuthentication,
        Login = user,
        Password = password
    };
}
else
{
    serverConection = new ServerConnection()
    {
        ServerInstance = server,
        LoginSecure = windowsAuthentication,
    };
}

In my opinion, it shouldn't really matter much. Unless you're dealing with anonymous types, the initializer syntax is just a nice to have feature that can make your code look more tidy in some cases. I would say, don't go out of your way to use it to initialize all of your properties if it sacrifices readability. There's nothing wrong with doing the following code instead:

ServerConnection serverConnection = new ServerConnection()  
{
    ServerInstance = server,
    LoginSecure = windowsAuthentication,
};

if (!windowsAuthentication)
{
    serverConnection.Login = user,
    serverConnection.Password = password
}
Dr. Wily's Apprentice