VB -> C#: You can’t assign char to string directly
Input code
Friend Const DlM As Char = "^"c
Friend Function LeftSideOf(ByVal strDlM As String = DlM) As String
...
End Function
Erroneous output
internal const char DlM = '^';
internal static string LeftSideOf(string strDlM = DlM)
{
...
}
Expected output
internal const char DlM = '^';
internal static string LeftSideOf(string strDlM = null)
{
strDlM = strDlM ?? DlM.ToString();
...
}
Details
- Product in use: extension
- Version in use: 8.0.4
In c# you can’t assign char to string directly. In case of method param you should assign it null and add smth like: strDlM = strDlM ?? DlM.ToString(); in the method body.
The opposite case is also an issue, here's a case that includes both:
Public Module Issue557
Friend Const BestString As String = "^"
Friend Const WorstChar As Char = '¦'
Friend Function GetBest(Optional ByVal chr As Char = BestString) As Char
Return chr
End Function
Friend Function GetWorst(Optional ByVal str As String = WorstChar) As String
Return str
End Function
End Module
Thanks for posting this. It's a rather annoying problem to solve since the C# compiler just can't do it: https://stackoverflow.com/a/410778/1128762 The simplest solution (but less pretty+maintable) would be to just inline the value rather than keeping it as a field.
I think your solution would work in most cases, but if someone was passing null to the method before, they'll now get a different result. For the opposite case I posted above, char would have to be made in the method signature. Perhaps it'd be better to generate an extra field as needed:
public static partial class Issue557
{
internal const string BestString = "^";
internal const char BestString_char = '^';
internal const char WorstChar = '¦';
internal const string WorstChar_string = "¦";
internal static char GetBest(char chr = BestString_char)
{
return chr;
}
internal static string GetWorst(string str = WorstChar_string)
{
return str;
}
}
This would work for this particular case, but not for other cases where a constant is required, hence the above is preferred:
public static partial class Issue557
{
internal const string BestString = "^";
internal const string WorstChar = "¦";
internal static char GetBest() => GetBest(BestString[0]);
internal static char GetBest(char chr)
{
return chr;
}
internal static string GetWorst() => GetWorst(WorstChar.ToString());
internal static string GetWorst(string str)
{
return str;
}
}