0

Hi I have declared an enum within the master page as

public enum AlertType
        {
            success = 1,
            danger,
            warning,
            info,
            primary,
            secondary,
            light,
            dark
        }

But when I tried to access one element of this enum inside my content page using string Name = this.Master.AlertType.danger.ToString(); it gives an error 'cannot reference a type through an expression'. What to do with this error? how can I access those values inside the enum here in my content page?

Richu R
  • 71
  • 3
  • 12

1 Answers1

0

The problem is exactly what the error message says. You're using a local property this.Master.AlertType to access an enum. This property has a value of its own (maybe success) so it makes no sense to add .danger to it.

Try

string Name = AlertType.danger.ToString();
Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
  • But I can't access the AlertType directly in the content page?? – Richu R Mar 11 '19 at 11:15
  • Then you probably need to reference the class that owns it. We don't know what that is, because we've not seen the rest of your code. It'll be something like `masterPage.AlertType.danger.ToString()` – Robin Bennett Mar 11 '19 at 11:17
  • Yes I can access it with Master page name. But I am asking about why we can't use this.Master instead? – Richu R Mar 11 '19 at 11:21
  • I already set master type VirtualPath and everything and I can access all other variables declared public within master page using this.Master – Richu R Mar 11 '19 at 11:23
  • 1
    because this.Master is an object with properties. It represents a single thing with properties that have values. The class that defines it (and contains the enum) are the blueprint from which it is built. The compiler needs to know whether your referring to the class or the object. I suppose it could work it out, but it's not that clever. – Robin Bennett Mar 11 '19 at 11:43