|
Thanks Simon,
I think the correct pattern should be, the derived class should only implement Dispose, but not Finalize, right?
regards,
George
|
|
|
|
|
You can implement Finalize too and in the Finalize() method just call the Dispose() method.
|
|
|
|
|
Thanks Zoltan,
You mean from base to derived class to most derived class, all implements Finalize method and in Finalize method call Dispose?
regards,
George
|
|
|
|
|
Hi gurus, I have this method in one of my classes:
public static Forum GetForumByID(int forumID)
{
Forum forum = null;
foreach (Forum theForum in GetForums())
{
if (theForum.ID == forumID)
{
forum = theForum;
break;
}
}
if (forum == null)
{
throw new ForumNotFoundException(forumID);
}
return forum;
}
Now, I thought I'd rewrite the method to be like that:
public static Forum GetForumByID(int forumID)
{
foreach (Forum forum in GetForums())
{
if (forum.ID == forumID)
return forum;
}
throw new ForumNotFoundException(forumID);
}
My question is, is there any thing wrong with that code? It's perfectly accepted by the compiler (as you know nothing is executed after an exception is thrown). Actually I never write such code, so this is going to be the first time but I want to know whether there's any problem with that code, also whether you believe it's a bad practice or coding style and why.
Your help is much appreciated ...
|
|
|
|
|
Waleed Eissa wrote: My question is, is there any thing wrong with that code?
Maybe. If not finding a forum is an "expected" condition then you shouldn't throw an "exception" for it.
led mike
|
|
|
|
|
Some ASP.NET controls requires this anti-pattern
|
|
|
|
|
I'm sorry, I didn't quite understand what you mean, could you please explain more?
|
|
|
|
|
an anti pattern is a case of BAD design. As opposed to a normal 'design pattern' which is a best practice way of doing something.
You know, every time I tried to win a bar-bet about being able to count to 1000 using my fingers I always get punched out when I reach 4....
-- El Corazon
|
|
|
|
|
Thanks for your answer dan, well, usually you should use exceptions for handling unexpected conditions which is true in my case (at least in my understanding), I'm not using exceptions to control the flow of my application ...
|
|
|
|
|
Sorry, it looks like Leppie and I were talking at cross purposes to you. His point was that exceptions for normal process flow is a relatively common anti-pattern and some ASP.net controls were written such that it was needed. That was mostly directed to Led Mike's post.
You know, every time I tried to win a bar-bet about being able to count to 1000 using my fingers I always get punched out when I reach 4....
-- El Corazon
|
|
|
|
|
There is no difference between the two implementations, but the second one is more concise and easier to read (= good).
All led meant about the exceptions is that exceptions should only be used in truly exceptional cases. If not being able to find a forum based on its id is an "expected" condition, then a better idea would be to return null and have the calling function check the return value.
Great article about using exceptions: http://codebetter.com/blogs/karlseguin/archive/2006/04/05/142355.aspx[^]
|
|
|
|
|
yes this is true for library code. Design for libraries is different in some respects from design for application level code since they must consider the fact that they don't have access to application level code to change it. Kent Beck has entire chapters on developing libraries in "Implementation Patterns".
led mike
|
|
|
|
|
Thanks for your reply, actually it's not an expected condition, it probably should never happen unless someone intentionally changes the query string of the page (it's a web application)
|
|
|
|
|
They are one and the same thing. For style, I prefer the 2nd listing, but either is acceptable.
|
|
|
|
|
I prefer the first, because it makes it clear why the exception is being thrown.
Pits fall into Chuck Norris.
|
|
|
|
|
Brady Kelly wrote: because it makes it clear why the exception is being thrown.
That should be documented instead
|
|
|
|
|
It is documented, by the guard statement.
|
|
|
|
|
If you're using the latest and greatest C# 3, I prefer this:
var forumWithId = (from f in GetForums() where f.ID == forumID select f).FirstOrDefault(); or if you prefer the method syntax,
var forumWithId = forums.FirstOrDefault(f => f.ID == forumID);
|
|
|
|
|
Hi,
the second snippet is fine.
Whether you end with a throw or just return null is up to you.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
The second is clearer, but breaking/returning out of a foreach is generally poor style; use it sparingly.
|
|
|
|
|
I don't usually do that but I'm only doing it here for the sake of performance (the value was found, return immediately) ...
|
|
|
|
|
Hi,
I am a first timer here so I hope this is the right post in the right place.
I am looking for some inspiration to build a textbox which can hold multiple values (a collection) semicolon seperated and get autocompletion support for each of the individual words. Just like the To, and CC fields in outlook
I thought such a control was commercialy or otherwise available I can't find anything like it.
Hope to get some help.
Derk
|
|
|
|
|
Hello all,
This is my first post. Sorry in advance for any stupid questions.
I would like to develop an application which restart a process on a remote computer. I have noticed that I would actually 2 applications: 1) a server (listener) on the remote target PC, 2) a client which will initiate and connect to the server and pass the name of the service to restart.
I have developed a tested the application on the server side:
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Diagnostics;<br />
using System.Threading;<br />
using System.ServiceProcess;<br />
<br />
namespace RestartProcess<br />
{<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
RestartService("MY SERVICE TO RESTART");<br />
}<br />
<br />
private static void RestartService (string name)<br />
{<br />
ServiceController service = new ServiceController(name);<br />
service.Stop();<br />
Thread.Sleep(1000);<br />
service.Start();<br />
Thread.Sleep(1000);<br />
}<br />
<br />
}<br />
}<br />
but I don't know how to connect to this process from the client side. Remoting?
Thanks for supplying an easy sample.
Regards,
|
|
|
|
|
bluescreen76 wrote: Thanks for supplying an easy sample.
Remoting is not easy to understand, but once you do it is not that hard unless you have to fiddle with the plumbing.
I suggest from an ease point of view to implement this via a web service.
|
|
|
|
|
Thanks for the reply, I assume this mean that the remote PC should have a web server installed. Must it be Microsoft IIS or can it be a free web server?
Thanks again
|
|
|
|