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? 3 1759
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" <Erik Ha****@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.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?
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********@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.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("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) [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 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" <Erik Ha****@discussions.microsoft.com> wrote in message news:92**********************************@microsof t.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?
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********@discussions.microsoft.com> wrote in message news:40**********************************@microsof t.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("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) [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 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" <Erik Ha****@discussions.microsoft.com> wrote in message news:92**********************************@microsof t.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? >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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:
|
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...
|
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" %>...
|
by: Peter Morris |
last post by:
interface Interface1
{
}
interface Interface2 : Interface1
{
}
class A : Interface2
{
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |