2

i have searched everywhere but cannot find a solution, so I am asking the question here. I have a procedure which takes a parameter of a string enum type (which has been created in a static class), but I keep getting string cannot be converted to mytype. Which i find odd as I am actually sending it one of its type. Can someone please help me understand what I am missing. I understand the error but I do not get it as I am passing through its type.

Below is the static class type created in c#

public static class ExportedType
{
    public const string CSV = "csv";
    public const string XML = "xml";
}

I create a procedure in vb.net

Private Sub ExportData(exportedType As ExportedType)
   'stuff in here
End Sub

I try to call it

ExportData(ExportedType.XML)

and i keep getting value type string cannot be converted to ExportedType

My guess is I am missing something vital, but can some try to explain for me.

Thanks

altaaf.hussein
  • 274
  • 2
  • 13

3 Answers3

0

ExportedType.XML is of type string - not of type ExportedType as your method signature expects.

Strings are strings, enums are enums. If you want an enum, define ExportedType as an enum.

So either define your method like this

Private Sub ExportData(exportedType As String)
   'stuff in here
End Sub

or keep your method as-is and define your enum like this:

public enum ExportedType
{
    CSV,
    XML
}

It is fairly common to markup enum values with a DescriptionAttribute to provide a string representation:

public enum ExportedType
{
    [Description("csv")]
    CSV,
    [Description("xml")]
    XML
}

These can be read quite easily using reflection

public static class EnumExtensions
{
    public static string ToDescriptionString(this ExportedType val)
    {
        DescriptionAttribute[] attributes = (DescriptionAttribute[])val
           .GetType()
           .GetField(val.ToString())
           .GetCustomAttributes(typeof(DescriptionAttribute), false);
        return attributes.Length > 0 ? attributes[0].Description : string.Empty;
    }
} 
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

ExportedType.CSV is a string - you declared it as a string with public const string CSV. It's not an instance of ExportedType, and therefore you cannot pass it to a method which expects a parameter of type ExportedType.

One option is to make your ExportData method take a string instead of an ExportedData. Then you can pass ExportedData.CSV (which evaluates to the string "CSV"):

Another option is to use a normal enum, and forget about strings:

public enum ExportedData { CSV, XML }

The third option, and I think this is what you were going for, is to create a "Java-style" enum. This gives you some of the best of both worlds: it's obvious to callers of ExportData that they need to pass a member of ExportedType (and not any old string), but it also lets you get a string value out of each member of ExportedType.

To do this, your ExportedData class needs to have static members which are of type ExportedData, not string:

public class ExportedType
{
    public string Value { get; }
    private ExportedType(string value)
    {
        Value = value;
    }

    public static readonly ExportedType CSV = new ExportedType("CSV");
    public static readonly ExportedType XML = new ExportedType("XML");
}

Then you can pass ExportedType.CSV as an ExportedType instance, and access exportedType.Value to get the string "CSV".

canton7
  • 37,633
  • 3
  • 64
  • 77
0

CSV and XML are (string) properties of the ExportedType class, and not instances of it. At the very least, this: Private Sub ExportData(exportedType As ExportedType) needs to be changed to Private Sub ExportData(exportedType As String).

npinti
  • 51,780
  • 5
  • 72
  • 96