3

I have a file with this code:

namespace A
{
    public enum DT
    {
        Byte = 0,
        SByte = 1,
        BCD8 = 2,
        Int16 = 3,
        UInt16 = 4,
        BCD16 = 5,
        Int32 = 6,
        UInt32 = 7,
        BCD32 = 8,
        Single = 9,
        String = 10,
        Structure = 11,
        WString = 12
    }
}

In my WebForm1.aspx.cs file, I want to use an element from code above. My WebForm1.aspx.cs looks like:

using A;
namespace SComm
{
    public partial class WebForm1 : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           A.DT tData = new A.DT.Int16;
           //some other code
        }
    }
}

I get the error CS0426:

"The type Int16 does not exist in the type DT"

I suppose is because of the different namespaces. What should I do solve this error?

Ionut
  • 724
  • 2
  • 9
  • 25
  • 3
    Should be `DT tData = DT.Int16;` methinks. – Matthew Watson Nov 08 '19 at 13:25
  • The 2 parts of code are in two different files, with different namespaces. DT is not recognized when I type DT tData... – Ionut Nov 08 '19 at 13:27
  • Does this answer your question? [C# explicit initialization for Enum](https://stackoverflow.com/questions/4410979/c-sharp-explicit-initialization-for-enum) – xdtTransform Nov 08 '19 at 13:30
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/enumeration-types and https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum – xdtTransform Nov 08 '19 at 13:32
  • The first example from the first microsoft link looks alike to what I want. `Day today = Day.Monday; int dayNumber =(int)today;` But I get the above error when I try to use `Monday` – Ionut Nov 08 '19 at 13:41

1 Answers1

1

In the original post you were declaring a variable of type A but A is a namespace and also using new to create an enum which is not correct. With the new it is looking for a type "Int16" in the type A.DT which obviously does not exist. Its like this

A.DT tData = A.DT.Int16; 
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47