-1

So I have to initiate an object from a class into the main form, but the arguments I have to put in the constructor are from an enum type I made in that class.

    private List<Geluidsfragment> fragmenten;
    private enum ThemaSoorten 
    { 
        Muziek, 
        Luisterboeken, 
        Cabaretshows 
    }    

^this part is now outside the class, as was advised.

// Constrcutors

    public BGExperience(ThemaSoorten thema)
    {
        fragmenten = new List<Geluidsfragment>();
        this.thema = thema;
    }    

These are the fields and constructor for the field. Below is the initiation of the object of this class I need.

public GeluidsfragmentForm()
    {
        InitializeComponent();
        BGExperience bgExperience = new BGExperience("Muziek");
    }

So the overload has to be of the type ThemaSoorten and it has to be in the enum, but at this point it gets stuck.

Anyone know how to solve this?

Lukas
  • 13
  • 6
  • BGExperience bgExperience = new BGExperience(ThemaSoorten.thema1); – Bill Dinger Jan 05 '15 at 14:56
  • You need to declare the enum as `public`and probably outside of the class. – Sriram Sakthivel Jan 05 '15 at 14:58
  • 1
    possible duplicate of [Get the name of Enum value](http://stackoverflow.com/questions/16039037/get-the-name-of-enum-value) – John Alexiou Jan 05 '15 at 15:07
  • You cannot specify a string value for an enum value like that, you can only specify numeric values. Why not use `public enum ThemaSoorten { Muziek, Luisterboeken, Cabaretshows };` ? – Lasse V. Karlsen Jan 05 '15 at 15:11
  • You cannot assign a string to the `enum` values (at least no in C# 4.0). [MSDN](http://msdn.microsoft.com/en-us/library/vstudio/sbbt4032(v=vs.110).aspx) states "The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong." – John Alexiou Jan 05 '15 at 15:12

3 Answers3

2

You should make first your enum to be public and then try this one:

BGExperience bgExperience = new BGExperience(BGExperience.ThemaSoorten.Muziek);

If you don't make your enum to be public, you will not be able to access it outside your class let alone to create an instance of your class.

Update

As correctly Sriram has already pointed out in his comments, it would be a better design approach to not make this enum to be a nested type. You could declare it outside of your class, like:

public enum ThemaSoorten 
{ 
    Muziek, 
    Luisterboeken, 
    Cabaretshows 
}

and then create a new BGExperience object as below:

var bgExperience = new BGExperience(ThemaSoorten.Muziek);

Important Note (thanks Selman22)

You can't declare an enum with the way you have already done it. This couldn't even compile. Furthermore, you should take an error message while you were typing this. Anyway, the correct way of defininit an enum is the following:

public enum EnumName
{
    Member1,
    Member2,
    Member3
}

In place of EnumName you should put the name of your enum and in place of Member1, Member2 and Member3 the names of the items in your enum. If you want this items have specific values, you should do it like:

public enum EnumName
{
    Member1 = 1,
    Member2 = 2,
    Member3 = 3
}
Christos
  • 53,228
  • 8
  • 76
  • 108
  • That still wont compile ;) – Sriram Sakthivel Jan 05 '15 at 14:57
  • @SriramSakthivel yeah the enumeration is private and out of scope – Bill Dinger Jan 05 '15 at 14:58
  • @SriramSakthivel I suppose you were saying so, because the enum access modifier wasn't public. Correct? – Christos Jan 05 '15 at 14:59
  • @Christos Yes, my comment was made before you edited the answer. I still recommend to make the enum non nested type. – Sriram Sakthivel Jan 05 '15 at 14:59
  • @SriramSakthivel Thanks. I agree with you 100%. However I tried to give a solution in the context of OP. – Christos Jan 05 '15 at 15:01
  • 2
    how does this compile ? `thema1 = "Muziek",` – Selman Genç Jan 05 '15 at 15:09
  • It doesn't, it's invalid syntax. You cannot specify a *string* value for an enum in C# or .NET, only a numeric value. – Lasse V. Karlsen Jan 05 '15 at 15:10
  • @Selman22 you are completely correct. My mistake...that I didn't pointed out and I replicated the erorr. Thanks for pointing this out. – Christos Jan 05 '15 at 15:11
  • @Christos I now have defined it the way you said it but I still get the error "cannot implicitely convert type 'string' to 'int'" – Lukas Jan 05 '15 at 15:27
  • @Lukas have you defined the `Enum` like in my answer, outside your class and pass the argument `ThemaSoorten.Muziek` to the constructor and still you get an error? The underlying type of an enum is an integral type. When we don't define explicitly the underlying type, then by default the underlying type of the enum is the int. When you set the first item of the enum to be `Member1` it's value is 0, the next member's value is 1 and so on. On the other hand, if you pass a string it's reasonable that you will get this error. – Christos Jan 05 '15 at 15:29
  • @Christos yes, I have done everything exactly like in your answer – Lukas Jan 05 '15 at 15:31
  • @Lukas please check this https://dotnetfiddle.net/92opwE. There I don't get any error. So the problem should be somewhere else. – Christos Jan 05 '15 at 15:37
0

Or perhaps you want to provide string value of the enum to the constructor, then here is how it will work:

BGExperience bgExperience = new BGExperience((BGExperience.ThemaSoorten)Enum.Parse(typeof(BGExperience.ThemaSoorten), "Muziek"));

Make sure to make the enum public for that. Otherwise you have to provide string in the constructor and parsing it to the enum value within the constructor.

Irfan
  • 2,713
  • 3
  • 29
  • 43
-1
BGExperience bgExperience = new BGExperience(ThemaSoorten.thema1);
Rad1
  • 308
  • 2
  • 9