|
In answer to your second question, I'd suggest using the first style, which is more concise and clearer for readers. It clearly says "DoA(), DoB if A is successful, catch and handle A errors, catch and handle B errors". It's certainly cleaner IMO.
|
|
|
|
|
CodingYoshi wrote: create my own custom exception
Yes.
CodingYoshi wrote: simply create a new Exception object
Never create an Exception object. Only something derived from Exception.
CodingYoshi wrote: create a new Exception object and add my own message to it and link the inner exception to it.
Where is this other Exception object coming from that you are putting in the inner exception? it isn't in the code you showed.
Basically, create a custom exception class or use an existing specific exception if one exists. For example, I'll often create a DalException, or an excption for a specific part of the model in my application, e.g. a SalesException or BookingException. I won't create one for absolutely every exentuality, but I use them in logical groupings.
CodingYoshi wrote: Or should I throw ApplicationException?
I'm not a fan of the ApplicationException, it is too generic. You can derive from it if you want.
In terms of how much code to put in your try block, that depends on the individual situation.
If the two exceptions are distinct, as in your example, then I don't really see a problem with the first way you showed it. (i.e one try block.)
I would suggest that if the two method calls can potentially produce the same kind of exception and you need to differenciate which method call produced the exception then your second example would be best.
|
|
|
|
|
I want to be set the Default value of a property in a class to be serialized, i have found this is easy enough on simple types but i want to set the default values of some complex properties like
public class Script
{
...
[DefaultValue("Hello World")]
public string Name { get; set; }
//How do i do this?
[DefaultValue(...)]
public Information Info { get; set; }
...
}
I'm not to sure on how to do this any suggestions would be appreciated
Jordan.
|
|
|
|
|
[DefaultValue(typeof(ClassName), "ValueAsDisplayedInPropertyGrid")]
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
That doesn't work as the value in the property grid is just "Scripts.Information" is there anyway i can change this?
|
|
|
|
|
Have a look at the solution Mika suggested below. If that doesn't work for you - then you may need to get more in depth and use TypeConverters. I wrote an article here[^] a while ago about this. I didn't really explain the more advanced latter bits of the code in the article (I must get around to sorting that!) but you should be able to figure it out from the code.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
|
I come from VB. Now I’m trying to learn C++. Can someone help me find good tutorials for someone without a Visual Compiler? Im using Dev-C++ 4.9.7.0
|
|
|
|
|
|
this is C# forum.
I Love KongFu~
|
|
|
|
|
I come from VB. Now I’m trying to learn C++ now. Can someone help me find good tutorials for someone without a Visual Compiler? Im using Dev-C++ 4.9.7.0
|
|
|
|
|
I suggest you read the title of this forum, then go to the right forum.
Hint: there are at least 2 forums on C++ depending on your exact needs.
|
|
|
|
|
Hello,
I would like to call a method multiple times by a count. For example:
If count = 6 call method 6 times.
Thank you so much for the help.
RB
|
|
|
|
|
for[^]
You need to read very basic c# book.
|
|
|
|
|
1.
for(int i=0; i< count; i++) myPopularMethod(withPossiblySomeArguments);
2.
or create a delegate and an event; add the delegate count times to the event and call the event.
|
|
|
|
|
He could also use the reverse-singleton pattern?
I are troll
|
|
|
|
|
|
Int control= count;
Whiskey:
if (control== 0)
goto Foxtrot;
else
goto Tango;
Tango:
MyMethodCall();
control--;
goto Whiskey;
FoxTrot:
Edit: Improved code.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
modified on Thursday, January 8, 2009 3:38 PM
|
|
|
|
|
I see everyone's using goto after yesterday's discussion!
int control = 5;
while (control > 0)
{
MyMethodCall();
control--;
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
That is such a bad use of goto...
for (int i=0; i<6; i++)
MyMethodCall();
|
|
|
|
|
Let's hope count is positive now.
|
|
|
|
|
Hi,
labels too deserve meaningful names, they add to the readability of the source code; so I would suggest:
uint control= count;
WhileThereMayBeMore:
if (control== 0)
goto FinallyWhenDone;
else
goto ThisIsWhereWeCallTheMethod;
ThisIsWhereWeCallTheMethod:
MyMethodCall();
control--;
goto WhileThereMayBeMore;
FinallyWhenDone:
BTW: why is it all examples seem to use PascalCase labels, they are local after all?
(e.g. read MSDN on the goto keyword)
|
|
|
|
|
My labels do have meaningful names when looked at collectively. Perhaps I was too subtle.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Sure they do, however you could combine individual and collective meaning, yielding the best of both worlds.
|
|
|
|
|
Hmm, I'm sure you could fit some XML in there, keep the counter in a database table, or even create a custom CountNotYetAchieved Exception class. I'd even settle for a string counter and Int32.Parse it on each iteration. Come on guys, integer counters are soooo yesterday! Here's my real version:
while(count-- > 0) MyMethod();
One other thing is the else technically is unnecessary in the Whiskey label...
Last, you could spice it up with if(!(control != 0)) , that would make it really nice!
Keep It Simple Stupid! (KISS)
|
|
|
|