Enum Property with Interface

  • Thread starter =?iso-8859-1?Q?Cristiano_de_P=E1dua_Milagres_Olive
  • Start date

?

=?iso-8859-1?Q?Cristiano_de_P=E1dua_Milagres_Olive

What do I to declare the header of a Property in an Interface, and should this property return a generic Enum?

Example:

public interface IMetric
{
Enum Unit { get;}
}

public class ClassTest : IMetric
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}

private EnumTeste _Unit;

public EnumTeste Unit
{
get
{
return _Unit;
}
}
}
 
Ad

Advertisements

A

Andreas Mueller

Cristiano said:
What do I to declare the header of a Property in an Interface, and
should this property return a generic Enum?

Example:
public interface IMetric
{
Enum Unit { get;}
}

public class ClassTest : IMetric
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}
private EnumTeste _Unit;
public EnumTeste Unit
{
get
{
return _Unit;
}
}
}

This compiles and works:

public interface IMetric
{
Enum Unit { get;}
}
public class ClassTest : IMetric
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}
private EnumTest _Unit = EnumTest.T2;
public Enum Unit{ get{ return _Unit; } }
}

class Program
{
static void Main(string[] args)
{

ClassTest tt = new ClassTest();
ClassTest.EnumTest ttt = (ClassTest.EnumTest)tt.Unit;
}
}

Or using generics:

public interface IMetric<T>
{
T Unit { get;}
}
public class ClassTest : IMetric<ClassTest.EnumTest>
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}
private EnumTest _Unit = EnumTest.T2;
public EnumTest Unit { get { return _Unit; } }
}

class Program
{
static void Main(string[] args)
{

ClassTest tt = new ClassTest();
ClassTest.EnumTest ttt = tt.Unit;
}
}
 
Ad

Advertisements

C

Cristiano de Pádua Milagres Oliveira

Thanks, the change was small but the help was large!

Andreas Mueller said:
Cristiano said:
What do I to declare the header of a Property in an Interface, and should
this property return a generic Enum?

Example:
public interface IMetric
{
Enum Unit { get;}
}

public class ClassTest : IMetric
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}
private EnumTeste _Unit;
public EnumTeste Unit
{
get
{
return _Unit;
}
}
}

This compiles and works:

public interface IMetric
{
Enum Unit { get;}
}
public class ClassTest : IMetric
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}
private EnumTest _Unit = EnumTest.T2;
public Enum Unit{ get{ return _Unit; } }
}

class Program
{
static void Main(string[] args)
{

ClassTest tt = new ClassTest();
ClassTest.EnumTest ttt = (ClassTest.EnumTest)tt.Unit;
}
}

Or using generics:

public interface IMetric<T>
{
T Unit { get;}
}
public class ClassTest : IMetric<ClassTest.EnumTest>
{
public enum EnumTest
{
T1 = 0,
T2 = 1
}
private EnumTest _Unit = EnumTest.T2;
public EnumTest Unit { get { return _Unit; } }
}

class Program
{
static void Main(string[] args)
{

ClassTest tt = new ClassTest();
ClassTest.EnumTest ttt = tt.Unit;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top