-2

I have this function:

public static void SetSettingFlag(string name, ParamViewModel[] array, SET s)
{
    foreach (var setting in array)
    {
        if (setting.Name == name)
        {
            setting.IsSelected = true;
            App.DB.UpdateIntSetting(s, setting.Id);
        }
        else
            setting.IsSelected = false;
    }
}

public enum SET
{
    ABtn = 0,
    BBtn = 1,
    CBtn = 2
}

Is there a way to change this so the parameter s is optional and so if it is not supplied then the App.DB.Update will not be executed?

For reference SET is an enum.

Here's how I would like to call the method:

Utils.SetSettingFlag(name, vm.PTI, SET.Pti);

or

Utils.SetSettingFlag(name, vm.PTI);
maccettura
  • 10,514
  • 3
  • 28
  • 35
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Possible duplicate of [c# - Using an enum as an optional parameter - Stack Overflow](https://stackoverflow.com/questions/21617269/using-an-enum-as-an-optional-parameter) – user202729 Oct 10 '18 at 14:41
  • What type is parameter s? Is it string, int, long...? – Matt Oct 10 '18 at 14:41
  • Possible duplicate of [How can you use optional parameters in C#?](https://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c) – sr28 Oct 10 '18 at 14:44

4 Answers4

2

I propose a different approach since I feel your method is doing too much. There's no harm in being very explicit about the intention of the method as opposed to having to null check a parameter to determine behaviour.

public static void SetSettingFlag(string name, ParamViewModel[] array)
{
    foreach (var setting in array)
    {
        setting.IsSelected = setting.Name == name;
    }
}

public static void SetSettingFlagAndUpdate(string name, ParamViewModel[] array, SET s)
{
    foreach (var setting in array)
    {
        setting.IsSelected = setting.Name == name;

         if (setting.IsSelected)
         {
            App.DB.UpdateIntSetting(s, setting.Id);
         }
    }
}

public enum SET
{
    ABtn = 0,
    BBtn = 1,
    CBtn = 2
}

This is very clear to consumers of this code what is expected of them, and what they can expect to happen.

1

Add a default parameter for s:

public static void SetSettingFlag(string name, ParamViewModel[] array, SET s = null)

Then you can check whether it is null:

if (s != null)
     App.DB.UpdateIntSetting(s, setting.Id);

If SET is a struct, change it to SET? s = null

adjan
  • 13,371
  • 2
  • 31
  • 48
  • This gives me an error: /Helpers/Utils.cs(84,84): Error CS1750: A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'SET' (CS1750) – Alan2 Oct 10 '18 at 14:42
1

Yes, you can use optional parameters:

SET s = null

This would mean if s is not supplied it is null. More examples here: https://www.dotnetperls.com/optional-parameters

Optional parameters need to be supplied at the end of the parameter list, which yours is, so your ok there.

Your code would need updating like this:

public static void SetSettingFlag(
string name, ParamViewModel[] array, SET s = null)
{
    foreach (var setting in array)
    {
        if (setting.Name == name)
        {
            setting.IsSelected = true;
            if (s != null) 
            {
                App.DB.UpdateIntSetting(s, setting.Id);
            }
        }
        else
            setting.IsSelected = false;
    }
}

UPDATE

As SET is an enum, just add a value to indicate you don't want to do the db update, then set that as the default value rather than null and check for that value.

sr28
  • 4,728
  • 5
  • 36
  • 67
0

Yes, you can do it with an optional parameter:

public static void SetSettingFlag(string name, ParamViewModel[] array, SET s = SET.None)
{
    foreach (var setting in array)
    {
        if (setting.Name == name)
        {
            setting.IsSelected = true;
            if(s != SET.None)
                App.DB.UpdateIntSetting(s, setting.Id);
        }
        else
            setting.IsSelected = false;
    }
}

UPDATE As your SET is an enum, you can do it this way: Add a new value to your enum, lets say

public enum SET{
    None = -1,
... the rest of the enum ...    

And then replace the null (in parameter and in the if) for this new enum value

Guillermo Gerard
  • 808
  • 10
  • 21
  • This gives me an error: /Helpers/Utils.cs(84,84): Error CS1750: A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'SET' (CS1750) – Alan2 Oct 10 '18 at 14:42
  • So SET is not nullable. Could you do it nullable? If not, you can use the default value of a SET here Oh, I've just saw that SET is an enum, I'll update my answer – Guillermo Gerard Oct 10 '18 at 14:45