C# - How to skip parameters with default values? - c#

Consider the following method example:
public void MyMethod (string par1, bool par2 = "true", string par3="")
{
}
Now let's say that I call MyMethod and set par3's value to "IamString".
How could I do that without setting par2's value to true or false?
I basically want to leave par2 value to its default.
I'm asking this because in Flash's ActionScript it is possible to do that by using the keyword default so I could call MyMethod ("somestring", default, "IamString") and par2 would be interpreted as true, which is its default value. I wonder if it is possible in C# as well.

public void MyMethod (string par1, bool par2 = "true", string par3=""){}
Myclass.MyMethod(par1:"par1", par3:"par3");
By the way, this won't work: bool par2 = "true"
string par2 = "true"
or
bool par2 = true
Talking about default values, you could also use this to get the default value for a particular type:
default(T)

You can specify this by name the parameter:
instance.MyMethod( "Hello", par3:"bla" );
Have a look here.
And there is another bug:
bool par2 = true
is correct..

Related

C# Optional Out Parameter

Is there any way to make an out parameter of a method optional? I have a method that needs several parameters to return null most of the time, but sometimes they will have a value. I am trying to find out if there is a better way to accomplish this than by setting the values to null first, or setting them to null after the if/else. This may be the only way to do it, but it would be nice if there were optional out parameters or something equivalent. Here is the code:
private static void GetInitInfo(string initLine, string ncPointType, out double? relinquishDefault, out bool? ignoreRelinquishDefault, out bool? closedForStart, out bool? adjustDisabled)
{
relinquishDefault = null;
ignoreRelinquishDefault = null;
closedForStart = null;
adjustDisabled = null;
switch (ncPointType)
{
case "MSD":
adjustDisabled = LastToken<bool?>(initLine);
break;
case "BO":
relinquishDefault = SecondToken<double>(initLine);
closedForStart = ThirdToken<bool?>(initLine);
ignoreRelinquishDefault = !ForthToken<bool?>(initLine);//ignoreRelDef would be reverse of use initial value
break;
case "MSO":
closedForStart = SecondToken<bool?>(initLine);
relinquishDefault = ThirdToken<double>(initLine);
ignoreRelinquishDefault = !ForthToken<bool?>(initLine);//ignoreRelDef would be reverse of use initial value
break;
case "AD":
relinquishDefault = ThirdToken<double>(initLine);
ignoreRelinquishDefault = false;
break;
case "BD":
relinquishDefault = SecondToken<double>(initLine);
adjustDisabled = LastToken<bool?>(initLine);
ignoreRelinquishDefault = false;
break;
case "AOS":
relinquishDefault = FirstToken<double>(initLine);
ignoreRelinquishDefault = !ThirdToken<bool?>(initLine);//ignoreRelDef would be reverse of use initial value
break;
}
}
This isn't not allowed according to the C# 4.0 Spec: section 21.1.
A workaround is to overload with another method that does not have out or ref parameters.
I believe this question is asking about having to assign values inside the method with out parameters, and whether there is any way to circumvent that, i.e. by leaving the values unassigned rather than explicitly having to assign them null.
e.g. Do you have to write:
public bool OutMethod(out int? output)
{
output = null;
return true;
}
Or is there a way to do something like:
public bool OutMethod(out int? output)
{
return true;
}
With the same result?
Short answer is no, this can't be avoided. See the documentation for an example which includes assigning null explicitly, and states:
Note that the third argument is assigned to null. This allows methods to return values optionally.
Examples of this can also be found in the .NET framework. e.g. Dictionary<TKey,TValue>'s TryGetValue method:
public bool TryGetValue(TKey key, out TValue value)
{
int num = this.FindEntry(key);
if (num >= 0)
{
value = this.entries[num].value;
return true;
}
value = default(TValue);
return false;
}
Note the explicit assignment to default(TValue)
Use the overload with signature that does not have out parameters:
private static void GetInitInfo(string initLine, string ncPointType)
{
double? relinquishDefault;
bool? ignoreRelinquishDefault;
bool? closedForStart;
bool? adjustDisabled;
GetInitInfo( initLine, ncPointType, out relinquishDefault, out ignoreRelinquishDefault,
out closedForStart, out adjustDisabled);
}
The explanation is quite simple: You can do it. Just assign an out parameter inside the method to the desired value.
Now you can ask yourself, why can't we do that in the method signature? Well, let us have a look at the normal optional value parameters. They are assigned a predefined value if they are not assigned by the caller. So caller knows the value, which will be passed if the parameter is not set explicitly. It has a control of it and it is responsible for that decision.
Since the caller of the method is NOT responsible for the assignment of the out parameter, it makes no sense to offer a default value. The only thing you could achieve, by having a default out parameter value is to let the caller know what one of the possible values of that out parameter would be. But would that make sense? When will this value be used? Under what condition? All this is still hidden from the caller. So there is no real benefit from having an optional out parameter value, apart from having a possiblity to set it inside the signature instead of the method body.
So, having that said, the following would not make much sense:
public bool TrySomething(out string outObject = "default value") { ... }
However, what would be cool is to allow the following method
public bool TrySomething(out string outObject) { ... }
to be invoked as follows:
bool result = TrySomething();
And have that behind the scenes equivalent to:
string dummyWhichWillNeverBeUsed;
bool succeeded = TrySomething(out dummyWhichWillNeverBeUsed);
Unfortunately, that is not allowed.
And of course, as explained in other answers, you can always have overloads without out parameter.

Set alignment on the bases of string value

i am trying this but not sure what it needs as parameter.
VerticalAlignment.TryParse("top"); // Must return VerticalAlignment.Top & ignore case
Is there any way to achieve this? please guide
VerticalAlignment is an enum, so try using Enum.Parse like this:
var result = (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), "top", true);
Of course, you can also write your own generic method to make this syntax more palatable:
public static T ParseEnum<T>(string stringValue) where T : struct
{
return (T)Enum.Parse(typeof(T), stringValue, true);
}
And then use it like this:
var result = ParseEnum<VerticalAlignment>("top");
Since it is an enum use this
VerticAligment va = (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), "top", true);
The last value is the ignoreCase property you need to make it match case insensitive.
You almost had it - you just have to specify the second parameter as true to ignore case:
VerticalAlignment alignment;
VerticalAlignment.TryParse("Top", true, out alignment);

string to bool inline conversion

What I currently have:
bool okPress = !string.IsNullOrEmpty(Ctx.Request["okPress"]) &&
Convert.ToBoolean(Ctx.Request["okPress"]);
Correct me if I'm wrong here, but wouldn't this throw a FormatException if the string isn't "true/True" or "false/False"? Is there any way to handle the conversion in one row, without having to worry about exceptions? Or do I need to use Boolean.TryParse?
You can use Boolean.TryParse:
bool okPress;
bool success = Boolean.TryParse(Ctx.Request["okPress"]), out okPress);
For what it's worth, here a "one-liner", create following extension which might be useful especially in LINQ queries:
public static bool TryGetBool(this string item)
{
bool b;
Boolean.TryParse(item, out b);
return b;
}
and write:
bool okPress = Ctx.Request["okPress"].TryGetBool();
IF you didn't want to use TryParse You could do something like
bool okPress = !string.IsNullOrEmpty(Ctx.Request["okPress"]) &&
(Ctx.Request["okPress"].ToLower()=="true");
This way if the string is not true/false it will just assume false for you with no exceptions thrown.
This does of course assume that you are happy for a value of "fish" to be treated as false rather than as an exception.
Better though is to just not do it as a single line. You don't generally have a maximum number of lines of code so two or three simple lines of code are often better than one complicated line of code...
Why don't you compare the string against true?
bool okPress = !string.IsNullOrEmpty(Ctx.Request["okPress"]) &&
String.Compare(Ctx.Request["okPress"], "true", StringComparison.OrdinalIgnoreCase) == 0
You can use TryParse method of Boolean class as you said.
Tries to convert the specified string representation of a logical
value to its Boolean equivalent. A return value indicates whether the
conversion succeeded or failed.
bool result = Boolean.TryParse(Ctx.Request["okPress"]), out okPress);
It returns true if value was converted successfully; otherwise, false.
Your inline conversion.
public static bool TryParseAsBoolean(this string expression)
{
bool booleanValue;
bool.TryParse(expression, out booleanValue);
return booleanValue;
}
bool okPress = Ctx.Request["okPress"].TryParseAsBoolean();

Bool value always gets outputted to True only

I am having the following code snippet where I pass a value either True or False through the PropertyValue parameter in the method declaration.
public void SetTaskInstance(String PropertyName, String PropertyValue, int row)
{
bool bValue;
try
{
PropertyName = PropertyName.ToUpper();
switch (PropertyName)
{
case "BYPASSRULESENGINE":
m_tInstance.byPassRulesEngine =
System.Boolean.TryParse(PropertyValue.ToString(), out bValue);
break;
}
Console.WriteLine("Invoking method");
}
}
If I pass True, then True is outputted. However if I pass False, the parameter False is passed through the code but once the break statement is reached and when I hover my mouse over m_tInstance.ByPassRulesEngine, I see that the bool value has become True almost magically. Why is this happening ?
The return value of TryParse indicates if the parse was successful. And of course, the value "false" or "False" is valid, so TryParse would return true. The parsed value itself is written into the out bValue parameter.
Change the line
m_tInstance.byPassRulesEngine = System.Boolean.TryParse(PropertyValue.ToString(), out bValue);
to
bool parseSuccessful = System.Boolean.TryParse(PropertyValue.ToString(), out bValue);
if (parseSuccessful)
{
m_tInstance.byPassRulesEngine = bValue;
}
All TryParse methods behave that way. So read the documentation of TryParse on MSDN here:
http://msdn.microsoft.com/en-us/library/system.boolean.tryparse.aspx
Let me explain why you always receive a true value:
The function System.Boolean.TryParse returns true, if it can successfully do the conversion, from a String to a Boolean.
So in your example it will always be true.
Carsten Schütte has already given the solution code.
Look at msdn
the result of the conversion is stored in Output variable (your bValue)
When this method returns, if the conversion succeeded, contains true
if value is equivalent to Boolean.TrueString or false if value is
equivalent to FalseString. If the conversion failed, contains false.
The conversion fails if value is null or is not equivalent to the
value of either the TrueString or FalseString field.

Convert.ToBoolean("1") throws System.Format Exception in C#

Why does
Convert.ToBoolean("1")
throw a System.FormatException?
How should I proceed with this conversion?
Yes, this is as documented:
[throws] FormatException [if] value is not equal to TrueString or FalseString.
TrueString is "True" and FalseString is "False".
If you want to detect whether a string is "1" or not, use this code:
bool foo = text == "1";
Depends on what you want. Perhaps
var result = Convert.ToInt32(yourstirng) != 0
assuming any number but 0 is true. Otherwise a simple comparison would work.
var result = yourstirng == "1"
The parameter must be equal to either Boolean.TrueString or Boolean.FalseString. The values of these strings are "True" and "False", respectively. See MSDN.
The string value "1" is obviously not equal to "True" or "False".
The problem is, that youre giving a String here, not a number. It cant convert the String "1" to true, but the int 1.
Convert.ToBoolean(1);
should work.
When converting to Boolean it is best to use your own routine, where you handle all cases. .net Convert.ToBoolean isn't a practical routine, it is one of those function where you have to explain why it doesn't work.
I know this is old, but in case someone searches... simply do this:
Convert.ToBoolean(Convert.ToInt16("1")) works just fine. Not pretty, but needs be.
Another solution is to use an Extension Method on the string object. I used this technique in a case where I had to parse CSV files that had different strings that had to be converted to boolean values depending on their source and format.
public static class StringExtensions
{
public static bool ToBool(this string value,string trueValue)
{
if (value == trueValue)
{
return true;
}
else
{
return false;
}
}
}
This would be called like so...
MyProperty = CsvColumn[6].ToBool("1");
If you want, the truevalue parameter could be a string array if you needed to compare multiple values (like n/a, na, none) and you could add in false values if you want to further restrict it or use nullable types.

Categories

Resources