|
Edmundisme wrote: I don't know if simply giving the child an IPlayable iterface and the Vet an IEuthanizable interface will be restrictive enough as Child could cast IPlayable to Dog and thereby access its "Euthanize" method.
This will sound trite, but the simple answer is to make sure that your Child code doesn't do that.
Interfaces are good for enforcing the kind of contraints you've described; they limit how the outside world views an object thereby constraining what actions you can perform on them. However, if someone (another programmer) is going to abuse the contract you've given them by only exposing your objects through a limited interface... well, they've broken the contract and all bets are off.
Other than that, you could have some type of key that has to be passed to the Euthanize method. Only a vet would posses such a key, so in theory only a vet could euthanize a pet. Just make sure that the key is hidden from the child.
I'm curious about this problem, however, and will enjoy reading other answers. Hopefully, they will be more helpful.
|
|
|
|
|
Yeah, I was afraid of that. I've been wondering if I've been trying too hard to solve a problem that doesn't need solving. Perhaps using interfaces is enough.
It's interesting that you suggested using a "key". I suggested this option to one of my coworkers but it sounded a bit strange. So, either it's not a strange solution or I'm not the only one with strange ideas! Is there a pattern that describes this solution?
|
|
|
|
|
Edmundisme wrote: It's interesting that you suggested using a "key". I suggested this option to one of my coworkers but it sounded a bit strange. So, either it's not a strange solution or I'm not the only one with strange ideas!
Edmundisme wrote: Is there a pattern that describes this solution?
I'm not aware of any pattern, but one may exist.
I was thinking, though, if you want to model this so that it's more inline with the Vet/Pet metaphor, maybe instead of some generic "key," you would use a "medicine" object that is passed to Euthanize. And only a vet would have access to the medicine. But the role of the "medicine" object would be the same as a key: An object not obtainable by a child that must be passed to the Euthanize method.
Anyway, erm, as you can see, I'm not at a loss for strange ideas!
|
|
|
|
|
We are actually using a type like you suggest as the key, although it's difficult to explain the details in the pet/vet world because my problem doesn't exactly fit the metaphor. Thanks for your ideas!
|
|
|
|
|
Edmundisme wrote: because my problem doesn't exactly fit the metaphor.
Obviously since a Child would have a Dog instance not an IPlayable instance. In your real problem the Child would have an IPlayable instance and "should not" cast it to Dog since it could be a Cat. Upcasting is indicative of bad design. Languages do not protect against bad design or bad use. A Client should "read" a socket not "close" it, but nothing stops the developer from writing bad code that closes the socket, then he posts a message on CodeProject "Help Urgent why my code not work".
Edmundisme wrote: We are actually using a type like you suggest as the key
I would not advise a Rube Goldberg[^] design as an attempt to thwart bad developers from abusing a good design. Two wrongs don't make a right.
led mike
|
|
|
|
|
Although I agree this approach seems strange, what makes it "wrong"?
Also, this wouldn't be a problem if C# had friend classes or allowed you to make private types at the namespace level. Any idea why the CLR does not allow friend classes or private namespace types?
|
|
|
|
|
To elaborate on this approach, what I am doing matches this metaphor:
The factory class DogFactory has several methods:
GetLab()
GetPoodle()
GetCocker()
GetMutt()
This factory class encapsulates the instantiation of the classes:
class Lab : Dog
class Poodle : Dog ... etc.
Each Dog-derived class requires an IVet reference during construction. I've nested a private Vet class (implements IVet) in the DogFactory class and DogFactory has an instance of this private nested class. When a method is called to get a dog, the factory passes its instance of Vet (implements IVet) to the Dog-derived class's constructor:
Public GetLab()
{
return new Lab(this.vet);
}
By automatically managing the relationship between the dog and the vet, much vital logic is removed from the engineer's list of responsibilities. So I really want the factory to be used to create Dog objects. The engineer is not prevented from implementing IVet and creating a Dog object without using DogFactory, but because he would have to implement his own vet, he is less likely to do this. Even if he decides to implement his own vet, he will by this time have become aware of the DogFactory and will not be coding ignorantly.
Thoughts?
|
|
|
|
|
Edmundisme wrote: but because he would have to implement his own vet, he is less likely to do this
Less likely to do it than cast the IPlayable to a Dog and then cast that to an IEuthanizable then call the Euthanize() method if he doesn't want to kill the dog? I don't think so. You have overly complicated (Rube Goldberg) the design to arrive at an arguably nominal barrier to improper use.
Your continued use of the Dog/Vet metaphor is not helping. I thought we already established that in your real problem the Child would not have a Dog but an IPlayable. If that is not the case then my posts are probably not relevant.
led mike
|
|
|
|
|
I have reposted an explanation of my problem and solution in this forum because I want to hear what others might say about it. I have removed the metaphor and explained the actual scenario.
Have a peek if you're interested. I would love to hear your take.
Thanks!
|
|
|
|
|
Does anyone know if when I Load an assembly using Reflection I also have to load all of it Dependencies?
This is the behavior I am currently seeing – but was hoping things would be a bit simpler.
Cheers
Rich
|
|
|
|
|
AJ123 wrote: Does anyone know if when I Load an assembly using Reflection I also have to load all of it Dependencies?
Yes. You could try using ReflectionOnlyLoad[^] /ReflectionOnlyLoadFrom instead, to load just that assembly.
|
|
|
|
|
I want 2 change hue saturation of gif image how can I do it??? Can anybuddy help me??????????
THANX
|
|
|
|
|
Hello, I am the creator of a game called SharpKonquest (http://sourceforge.net/projects/sharpkonquest). I want to create a new version that support multiplayer through Internet, but I dont know how do this. I have think to create a TCP connection between server and client, but I don't know how synchronize the clients with the server. Exist some methodology or some class or tutorial that can help me?
Thanks
-----------------
Recuerda, no hay arma mas mortífera que una palabra brotada de un corazón noble, y un par de huevos que la respalden.
|
|
|
|
|
I haven't used this much so it might so I need some help on it.
I have a xml file and its schema file "xsd".
I can load the schema and load the xml but
I am only interested in 6 elements out of the xml file,
and there are a ton of different elements in the xsd file.
Here is the question:
Can I create a new xsd file with only the elements I am interested in and only read those elements out of the xml file?
God Bless,
Jason
Programmer: A biological machine designed to convert caffeine into code. Developer: A person who develops working systems by writing and using software.
[ ^]
|
|
|
|
|
What do mean by load xml and load schema?
You can use a XSLT to extract the elements of interest and as long as the other elements are optional in the schema you could use it to validate the xml.
only two letters away from being an asset
|
|
|
|
|
Mark Nischalke wrote: What do mean by load xml and load schema?
I can create a new XMLReader with its schema and 'XMLRead.Read()' the entire file into an object.
Thanks for the hint on XSLT that looks like what I needed.
God Bless,
Jason
Programmer: A biological machine designed to convert caffeine into code. Developer: A person who develops working systems by writing and using software.
[ ^]
|
|
|
|
|
The correct way would be to define the schema file in such a way as the elements that you do not need are optional.
File Not Found
|
|
|
|
|
I think what you want to use is xpath.
|
|
|
|
|
If you're just trying to extract data from XML, you don't even need the schema. If you know the elements, you could just load the XML into an XmlDocument object and XPath query out what you need.
|
|
|
|
|
Hello, my cybrenetics Friends !!!
I have a listview, and o wan't to sort the list by clicking the Column header.
But i have 2 columns, and i wan't to sort the list by clicking the 2 column, so i can get the items sorted by the second raw.
Thanks!!!
One nation - underground
|
|
|
|
|
There are several ways you could do this. But you'll likely want to create an IComparer implementation to look at ListViewItems, or whatever type you stick in the ListViewItem's Tag property. When you add an event handler for the ListView's ColumnClick event, the ColumnClickEventArgs instance gives you the index of the column clicked. You can use that in your IComparer to determine how to sort objects. In your handler, update or create a new comparer and set the ListView's ListViewItemSorter property with the instance you updated or created. Google IComparer to see how to implement one, but it's not difficult.
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) {
IComparer sorter = new YOURICOMPARER(e.Column)
listView1.ListViewItemSorter = sorter;
listView1.Sort();
}
|
|
|
|
|
|
Hello,
when i install my application, i get a fatal error message " Fatal Non-UI Error. Could not write the error to the event log. Reason: Source ThreadException already exists on the local computer."
I dont get this message on al machine, i get this error only on couple of our sites..
this is my main
static void Main() <br />
{<br />
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);<br />
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);<br />
<br />
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);<br />
<br />
CSingleProcInstance singleProcInstance = new CSingleProcInstance();<br />
if(singleProcInstance.IsSingleInstance())<br />
Application.Run(new MainForm());<br />
<br />
singleProcInstance.Release();<br />
}<br />
the exception is in the AppDomain.CurrentDomain part of the code.,
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)<br />
{<br />
try<br />
{<br />
Exception ex = (Exception)e.ExceptionObject;<br />
string errorMsg = "An application error occurred. Please contact the adminstrator " +<br />
"with the following information:\n\n";<br />
<br />
if (!EventLog.SourceExists("ThreadException"))<br />
{<br />
EventLog.CreateEventSource("ThreadException", "Application");<br />
}<br />
<br />
EventLog myLog = new EventLog();<br />
myLog.Source = "ThreadException";<br />
myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);<br />
}<br />
catch (Exception exc)<br />
{<br />
try<br />
{<br />
MessageBox.Show("Fatal Non-UI Error",<br />
"Fatal Non-UI Error. Could not write the error to the event log. Reason: "<br />
+ exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);<br />
}<br />
finally<br />
{<br />
Application.Exit();<br />
}<br />
}<br />
}
Can someone suggest me how to go about this Error?
With Rgds,
Anil
|
|
|
|
|
I trying to learn C# and I've got two questions for someone.
1) In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong.
2) In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work?
Any help will be appreciated.
public class Car <br />
private int speed = 0;<br />
private int brake = 0;<br />
private int Accel = 0;<br />
private int Speed = 0;<br />
<br />
public int Speed<br />
{<br />
get<br />
{<br />
return speed;<br />
}<br />
set<br />
{<br />
speed = value;<br />
}<br />
}<br />
public int Brake<br />
{<br />
get<br />
{<br />
return brake;<br />
}<br />
set<br />
{<br />
brake = value;<br />
}<br />
}<br />
<br />
public Car(int speed, int brake, int accelerate)<br />
{<br />
accelerate = Accel;<br />
brake = Brake;<br />
speed = Speed;<br />
}<br />
<br />
public static int Accelerate() <br />
{<br />
int speed = 0;<br />
int counter = 1;<br />
Console.WriteLine();<br />
Console.WriteLine("Type in the initial speed of car.");<br />
string inputNumbers = Console.ReadLine();<br />
speed = int.Parse(inputNumbers);<br />
<br />
while (counter < 6)<br />
{<br />
speed += 5;<br />
counter++;<br />
}<br />
Console.WriteLine();<br />
Console.WriteLine("Top speed of the car is: {0}", speed);<br />
return speed;<br />
}<br />
}<br />
public static int Decelerate()<br />
{<br />
int brake = 0;<br />
int counter = 1;<br />
while (counter < 6)<br />
{<br />
brake += - 5;<br />
counter++;<br />
}<br />
Console.WriteLine();<br />
Console.WriteLine("Speed after braking is: {0} ", brake);<br />
return brake;<br />
}<br />
}
|
|
|
|
|
JMOdom wrote: private int Speed = 0;
JMOdom wrote: public int Speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
led mike
|
|
|
|