Why can't I implement an interface property as a static property?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I apologize if this is a stupid question - I'm relatively new to OOP.

I have a property that must exist in a class in order to be used by another class. The property, however, does not change with each instance (it returns an instance of a delegate that points to the same method no matter what the instance).

I thought the best way to make sure that Class1 could be used by Class2 would be to create an interface that defined this property, and that Class1 (and any other classes that Class2 needs to use) could implement.

I tried to implement as a static property, but it won't compile. I can access the property by getting it from an instance, but that doesn't seem like the right way to do things to me (because the property is independent of the instance).

There must be a flaw in my design, but I can't figure it out. Can someone help?
 
Hi Erik,

As long as you need a static property you don't really have to implented it
in neither of your classes. It can be in a separate class say
DeleagteServices

class DeleagateServices
{
public Delagate MyDelegate
{
get
{
....
}
}
}

You can get the delagate from wherever you want Class1, Class 2, ...,ClassN

If the classes share common ancestor you can declare that static porperty in
that common parent (if you have control over it ofcourse).

Anyways, you cannot have static members in interfaces, though.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Erik Harris said:
I apologize if this is a stupid question - I'm relatively new to OOP.

I have a property that must exist in a class in order to be used by
another class. The property, however, does not change with each instance (it
returns an instance of a delegate that points to the same method no matter
what the instance).
I thought the best way to make sure that Class1 could be used by Class2
would be to create an interface that defined this property, and that Class1
(and any other classes that Class2 needs to use) could implement.
I tried to implement as a static property, but it won't compile. I can
access the property by getting it from an instance, but that doesn't seem
like the right way to do things to me (because the property is independent
of the instance).
 
Hi Erik,

I would suggest to use the pattern ArrayList work. I believe is clearer if
you provide the Pair class with an object that knows how to sort different
types of object than to have that lojic impleneted in the logic class
itself.

Look at the ArrayList and its sort method.

--

Stoitcho Goutsev (100) [C# MVP]


Erik Harris said:
If I put the delegate instance in a separate class, how do I point it to the correct method?

Here is some code for what I'm working on. The idea is to have a Pair
class that can hold any two objects in an array. The pair class has a
Sort() method that sorts the objects, but the sorting happens differently
for different types of objects. In this case, Students are sorted by name
alphabetically, and dogs are sorted by ascending weight. So, the sorting
implementation is delegated to the class that is being sorted.
class TestConsole
{
public static void Main()
{
Student Samantha = new Student("Samantha");
Student Billy = new Student("Billy")

Dog Milo = new Dog(65);
Dog Boris = new Dog(35);

Pair studentPair = new Pair(Samantha, Billy);
Pair studentPair = new Pair(Milo, Boris);

//This is where I want to be able to call Student.Sorter and
//Dog.Sorter instead
studentPair.Sort(Samantha.Sorter);
dogPair.Sort(Milo.Sorter);
...
}
}

class Pair
{
private IKeepInPair[] m_thePair = new IKeepInPair[2];

public Pair (IKeepInPair o1, IKeepInPair o2)
{
m_thePair[0] = o1;
m_thePair[1] = o2;
}

public delegate ComparisonEnum WhichComesFirst(IKeepInPair o1, IKeepInPair o2);

public void Sort(WhichComesFirst SortDelegate)
{
//if the return values is SecondComesFirst then swap the order
//of the objects in the array, otherwise don't change anything
}
}

class Student : IKeepInPair
{
private string m_name;

public Student(string Name)
{
this.m_name = Name;
}

// This is the property that I'd like to make static
public Pair.WhichComesFirst Sorter
{
get
{
return new Pair.WhichComesFirst(this.Sort);
}
}

private ComparisonEnum Sort(IKeepInPair o1, IKeepInPair o2)
{
//Decide which Student should come first and return
//appropriate ComparisonEnum value
}
}

class Dog : IKeepInPair
{
...
//Looks like Student except for info to decide which comes
//first based on weight rather than name
}

interface IKeepInPair
{
...

//This is the property that I would like to be static in
//the classes that implement the interface.
Pair.WhichComesFirst Sorter
{
get;
}

...
}

So, if I take your suggestion:

class DelegateServices
{
public Pair.WhichComesFirst Sorter
{
get
{
//How do I return an instance of Pair.WhichComesFirst
//that points to the correct class' Sort method?
}
}
}



--

Erik Harris


Stoitcho Goutsev (100) said:
Hi Erik,

As long as you need a static property you don't really have to implented it
in neither of your classes. It can be in a separate class say
DeleagteServices

class DeleagateServices
{
public Delagate MyDelegate
{
get
{
....
}
}
}

You can get the delagate from wherever you want Class1, Class 2, ....,ClassN

If the classes share common ancestor you can declare that static porperty in
that common parent (if you have control over it ofcourse).

Anyways, you cannot have static members in interfaces, though.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Erik Harris said:
I apologize if this is a stupid question - I'm relatively new to OOP.

I have a property that must exist in a class in order to be used by
another class. The property, however, does not change with each instance (it
returns an instance of a delegate that points to the same method no matter
what the instance).
I thought the best way to make sure that Class1 could be used by
Class2
would be to create an interface that defined this property, and that Class1
(and any other classes that Class2 needs to use) could implement.
I tried to implement as a static property, but it won't compile. I
can
access the property by getting it from an instance, but that doesn't seem
like the right way to do things to me (because the property is independent
of the instance).
There must be a flaw in my design, but I can't figure it out. Can
someone
help?
 
Stoitcho,

Thanks for taking the time to look at this. I appreciate all of your effort. I'll look at the ArrayList solution that you suggested.

Erik
--

Erik Harris


Stoitcho Goutsev (100) said:
Hi Erik,

I would suggest to use the pattern ArrayList work. I believe is clearer if
you provide the Pair class with an object that knows how to sort different
types of object than to have that lojic impleneted in the logic class
itself.

Look at the ArrayList and its sort method.

--

Stoitcho Goutsev (100) [C# MVP]


Erik Harris said:
If I put the delegate instance in a separate class, how do I point it to the correct method?

Here is some code for what I'm working on. The idea is to have a Pair
class that can hold any two objects in an array. The pair class has a
Sort() method that sorts the objects, but the sorting happens differently
for different types of objects. In this case, Students are sorted by name
alphabetically, and dogs are sorted by ascending weight. So, the sorting
implementation is delegated to the class that is being sorted.
class TestConsole
{
public static void Main()
{
Student Samantha = new Student("Samantha");
Student Billy = new Student("Billy")

Dog Milo = new Dog(65);
Dog Boris = new Dog(35);

Pair studentPair = new Pair(Samantha, Billy);
Pair studentPair = new Pair(Milo, Boris);

//This is where I want to be able to call Student.Sorter and
//Dog.Sorter instead
studentPair.Sort(Samantha.Sorter);
dogPair.Sort(Milo.Sorter);
...
}
}

class Pair
{
private IKeepInPair[] m_thePair = new IKeepInPair[2];

public Pair (IKeepInPair o1, IKeepInPair o2)
{
m_thePair[0] = o1;
m_thePair[1] = o2;
}

public delegate ComparisonEnum WhichComesFirst(IKeepInPair o1, IKeepInPair o2);

public void Sort(WhichComesFirst SortDelegate)
{
//if the return values is SecondComesFirst then swap the order
//of the objects in the array, otherwise don't change anything
}
}

class Student : IKeepInPair
{
private string m_name;

public Student(string Name)
{
this.m_name = Name;
}

// This is the property that I'd like to make static
public Pair.WhichComesFirst Sorter
{
get
{
return new Pair.WhichComesFirst(this.Sort);
}
}

private ComparisonEnum Sort(IKeepInPair o1, IKeepInPair o2)
{
//Decide which Student should come first and return
//appropriate ComparisonEnum value
}
}

class Dog : IKeepInPair
{
...
//Looks like Student except for info to decide which comes
//first based on weight rather than name
}

interface IKeepInPair
{
...

//This is the property that I would like to be static in
//the classes that implement the interface.
Pair.WhichComesFirst Sorter
{
get;
}

...
}

So, if I take your suggestion:

class DelegateServices
{
public Pair.WhichComesFirst Sorter
{
get
{
//How do I return an instance of Pair.WhichComesFirst
//that points to the correct class' Sort method?
}
}
}



--

Erik Harris


Stoitcho Goutsev (100) said:
Hi Erik,

As long as you need a static property you don't really have to implented it
in neither of your classes. It can be in a separate class say
DeleagteServices

class DeleagateServices
{
public Delagate MyDelegate
{
get
{
....
}
}
}

You can get the delagate from wherever you want Class1, Class 2, ....,ClassN

If the classes share common ancestor you can declare that static porperty in
that common parent (if you have control over it ofcourse).

Anyways, you cannot have static members in interfaces, though.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


I apologize if this is a stupid question - I'm relatively new to OOP.

I have a property that must exist in a class in order to be used by
another class. The property, however, does not change with each instance (it
returns an instance of a delegate that points to the same method no matter
what the instance).

I thought the best way to make sure that Class1 could be used by Class2
would be to create an interface that defined this property, and that Class1
(and any other classes that Class2 needs to use) could implement.

I tried to implement as a static property, but it won't compile. I can
access the property by getting it from an instance, but that doesn't seem
like the right way to do things to me (because the property is independent
of the instance).

There must be a flaw in my design, but I can't figure it out. Can someone
help?
 
Back
Top