2

I have searched around here for similar problems, but couldn't find a solution for my problem. MyClass holds several data and does some type casting between different types.

How can i avoid this Error:

A value of type 'string' cannot be used as default parameter because there are no standard conversions to type Program.MyClass?

I have tried Func and declared multiple function overload to be able to pass multiple argument and handle default parameter. There should be a better way to achieve this. Hopefully you could help.

To clarify my problem i have made this code:

using System;

public class Program
{
    public class MyClass {
            public string Value { get; set; } 
            public MyClass()
            {
                Value = "";
            }
            public MyClass(string s)
            {
                Value = s;
            }
            public override string ToString()
            {
                return this.Value;
            }
    }

    // Causes CS1750
    // A value of type 'string' cannot be used as default parameter
    // because there are no standard conversions to type 'Program.MyClass'
    public static string test2(string a, MyClass b = " with default text")
    {
       return a + b;
    }

    public static string test(string a, string b = " with default text")
    {
       return a + b;
    }

    public static void Main()
    {
        Console.WriteLine(test("test1"));
        Console.WriteLine(test("test1", " with my text"));
    }
}
wessam yaacob
  • 636
  • 1
  • 5
  • 12
biohell
  • 192
  • 11
  • You can not assign string to an object of a class... Why you want to do that? There can not be a default parameter value for a class type of argument. – Chetan Ranpariya Dec 14 '19 at 08:46
  • 1
    You might want to add an [implicit conversion operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators) to you class: `public static explicit operator MyClass(string s)` – Klaus Gütter Dec 14 '19 at 08:50

4 Answers4

2

That's not quite possible. As the error message states, you require a standard conversion: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#standard-conversions

All we can do is to define an implicit conversion operator for the class: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators

In your case, it would be something like:

public static implicit operator MyClass(string val) => new MyClass(val);

And replace your test method with something like this:

public static string test(string a, MyClass b = null)
{
    b = b ?? " with default text";
    return a + b;
}

Also note that both your test methods have the same signature if only one argument is provided, so the compiler won't know which one to use, remove the default value for the 2nd test method.

mdarefull
  • 589
  • 2
  • 11
  • 17
1

use null for default value, and replace it with default value inside method:

public static string test2(string a, MyClass b = null)
{
   if (b == null) return a + " with default text";
   return a + b.Value;
}
ASh
  • 30,500
  • 9
  • 48
  • 72
0

Add an implicit conversion operator between string and MyClass

public class MyClass 
{
        public string Value { get; set; } 
        public MyClass() : this(string.Empty) { }
        public MyClass(string s)
        {
            Value = s;
        }
        public override string ToString() => this.Value;
        public static implicit operator string(MyClass item) => item.Value;
        public static implicit operator MyClass(string value) => new MyClass(value)l;
}
John Alexiou
  • 23,931
  • 6
  • 67
  • 123
-1

You should change 2 steps (In case apply for whole class)

1. Initial default value on Constructor

public MyClass()
{
    Value = " with default text";
}

2. Change default params of the method

public static string test(string a, MyClass b)
{
   return a + b;
}

BTW, This post is helpful for you.


UPDATED: Set default value for only within function

The code above just suit in case applying for whole class. If you wanna set default value for only within function, You can try the code below

public static string test2(string a, MyClass b = null)
{
   var defaultValue = " with default text";
   return a + (b == null ? defaultValue : b.Value);
}
Nguyễn Văn Phong
  • 11,572
  • 15
  • 21
  • 43