|
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
|
|
|
|
|
It can be any web server. Have the web server host a web service, typically written in Java or ASP.NET. Then you can use standard HTTP requests to talk to the web service.
|
|
|
|
|
Guys,
How do we copy a set of data from the web and paste it in Excel sheet, Programatically in C#?
e.g.
If i go to
http://www.eurofound.europa.eu/eiro/2006/03/articles/no0603029i.htm[^]
I want to copy this table 1 data and then paste it in Excel.
In other words, does any one know how to parse the html data and save it in excel, Programatically in C#?
Thanks for your expertise.
modified on Tuesday, April 15, 2008 10:50 AM
|
|
|
|
|
Copy and paste in notepad, then in notepad copy and paste in Excel ...
|
|
|
|
|
Thanks Waleed.
I bet i did nt ask how to do that programatically in C# (assumed it to be understood.)
So does any one know how to parse the html data and save it in excel, Programatically in C#?
|
|
|
|
|
Well, you said copy and paste, you don't programmatically copy and paste, you import/export/parse .. etc .. this is why I was confused .. anyway, you have to parse the page (it's also called html scraping I believe) .. you can use mshtml to access the DOM of the page .. I have written a tool recently that does exactly this, read data from a table in an html page and save it to a table in a database but it only works with MySQL but I believe you could change the code easily, anyway, tell me if you want the code ...
regards
|
|
|
|
|
Thanks Waleed for helping out here. Now i know the right technical terms for what i want to acheive here
If you can send me the code for your tool, i will really appreciate that man.
ytb
|
|
|
|
|
Sure, I uploaded the code to my website .. you may download it from http://www.waleedeissa.com/temp/HTM2MySQL.zip[^]
BTW, you could just replace MySQL with OleDb and export to Excel using OleDb and this connection string:
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES;\"", excelFilePath)
Just change HDR=YES to HDR=No if you don't have headers in your Excel sheet ...
modified on Wednesday, April 16, 2008 3:20 AM
|
|
|
|
|
B Waleed,
The link doesnt seem to be active.
Can you kindly double check?
Thanks
|
|
|
|
|
You're right, sorry, I fixed the link try it again...
I also added a missing file, so if you already downloaded the file, then please download it again...
|
|
|
|
|
Arite,
I have written the problem scenario below.
Can you help me with Step 2? How do i make my application launch the browser with username and password already entered in it?
Sample Scenario:
Step 1 - I want to launch a webpage (www.hotmail.com) using C# Application.
Step 2 - I want my application to enter the username and password in the fields and then click the 'Sign In' button.
Therefore when i launch this application, it takes me directly into the page where data is sitting (Emails)
Here is the code that is working for Step 1
string url = "http://login.live.com/login.srf?wa=wsignin1.0&rpsnv=10&ct=1208358013&rver=4.5.2130.0&wp=MBI&wreply=http:%2F%2Fmail.live.com%2Fdefault.aspx&id=64855";<br />
System.Diagnostics.Process proc = new System.Diagnostics.Process();<br />
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(url);<br />
proc.StartInfo = startInfo;<br />
proc.Start();
|
|
|
|