473,785 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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?

Nov 16 '05 #1
3 1783
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
DeleagteService s

class DeleagateServic es
{
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" <Erik Ha****@discussi ons.microsoft.c om> wrote in message
news:92******** *************** ***********@mic rosoft.com...
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?

Nov 16 '05 #2
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" <Er********@dis cussions.micros oft.com> wrote in message
news:40******** *************** ***********@mic rosoft.com...
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("Samant ha");
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.Sor t(Samantha.Sort er);
dogPair.Sort(Mi lo.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(WhichComes First SortDelegate)
{
//if the return values is SecondComesFirs t 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.WhichComes First Sorter
{
get
{
return new Pair.WhichComes First(this.Sort );
}
}

private ComparisonEnum Sort(IKeepInPai r 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.WhichComes First Sorter
{
get;
}

...
}

So, if I take your suggestion:

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

--

Erik Harris
"Stoitcho Goutsev (100) [C# MVP]" wrote:
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
DeleagteService s

class DeleagateServic es
{
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" <Erik Ha****@discussi ons.microsoft.c om> wrote in message
news:92******** *************** ***********@mic rosoft.com...
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?


Nov 16 '05 #3
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) [C# MVP]" wrote:
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" <Er********@dis cussions.micros oft.com> wrote in message
news:40******** *************** ***********@mic rosoft.com...
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("Samant ha");
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.Sor t(Samantha.Sort er);
dogPair.Sort(Mi lo.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(WhichComes First SortDelegate)
{
//if the return values is SecondComesFirs t 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.WhichComes First Sorter
{
get
{
return new Pair.WhichComes First(this.Sort );
}
}

private ComparisonEnum Sort(IKeepInPai r 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.WhichComes First Sorter
{
get;
}

...
}

So, if I take your suggestion:

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

--

Erik Harris
"Stoitcho Goutsev (100) [C# MVP]" wrote:
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
DeleagteService s

class DeleagateServic es
{
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" <Erik Ha****@discussi ons.microsoft.c om> wrote in message
news:92******** *************** ***********@mic rosoft.com...
> 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?
>


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1525
by: Eidolon | last post by:
I am looking to have a base class which provides that all inherited classes MUST have some property, but that they can each define it as whatever type they want. So maybe three class inherit from the base class, each MUST have a property, but one defines it as OracleType, one as SqlType and one as DB2Type, for example. I know i can just declare it as Object, and then have the classes each do typechecks on value assignment, but this is...
4
2449
by: wesley | last post by:
Hi, Why must abstract classes implements all the methods/property in the interface it implements? Since it's an abstract class it shouldn't be able to be instantiated and the child classes are those that should implement the interface. However in .Net I have to implement the interface in the abstract class as well. Why is this so?
2
15945
by: Paul Selormey | last post by:
I have looked through the documents but could not find any information on this. Is anything like static properties in interfaces? If not, how do I define property in interface to be made static property in an abstract base class implementing the property? The problem is, I have a lot of interfaces in a library project and will wish most uses of the library classes should be through
3
330
by: John Underwood | last post by:
Hi.. I was looking at interface, and I have a example in the docs i'll paste below.. I'm not grasping what you would gain by using a interface, does any one have a brief description of their benefit? Thanks, John Underwood
3
9835
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has user interface. how should have get window handle from managed windows? Thanks in advance. Sushil
5
1914
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
1
3704
by: midnight madness | last post by:
I tried but failed to implement a template class that support IEnumerator<T> interface using C++/CLI in VS 2005 Professional version. I could not figure out the proper syntax to implement the property "Current". The challange is that I need to implement two versions of the property: one of type System::Object as required by Collections::IEnumerator, the other of type T^ as required by Collections::Generic::IEnumerator<T> ...
6
9161
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
7
1683
by: Peter Morris | last post by:
interface Interface1 { } interface Interface2 : Interface1 { } class A : Interface2 {
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.