|
Derive new classes from existing exceptions. Use ApplicationException as a base, not Exception.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
Christian Graus wrote: Use ApplicationException as a base, not Exception.
Not really. From MSDN's Best Practices for Handling Exceptions[^]:
For most applications, derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.
/ravi
|
|
|
|
|
Hi,
I am working through the Singleton pattern from www.dofactory.com, and I am not understanding the code at the bottom. I do not understand why they check twice for instance == null, please can some one help explain:
private static object syncLock = new object();
if (instance == null)
{
lock (syncLock)
{
if (instance == null)
{
instance = new LoadBalancer();
}
}
}
Actually I don't understand anything ob the above mentioned code..
Thanks
Brendan
|
|
|
|
|
.NET Enthusiast wrote: I do not understand why they check twice for instance == null
That's called Double checked locking[^].
|
|
|
|
|
Thanks I will check out that link..
|
|
|
|
|
|
Ah yes. The double lock check - which is unnecessary in C#. This same code can be accomplished using the static keyword:
public sealed class LoadBalancer()
{
private static readonly LoadBalancer _instance = new LoadBalancer();
private LoadBalancer() {}
public static LoadBalancer Instance
{
get { return _instance ; }
}
} The key here is that the CLR initializes the _instance member the first time this class is used. As you can see, this code is significantly simpler, and it solves the same problem in a different fashion - plus it's slightly more efficient.
|
|
|
|
|
This holds for single threaded applications only.
|
|
|
|
|
Assume you want to allow only one instance of the LoadBalancer class. The usual Singleton Pattern assures exactly that, except that it holds for single threaded applications only.
Assume you have more than one threads (e.g. A and B), both working with LoadBalancer. Then it could happen that, while thread A is just creating an instance of the LoadBalancer, the thread scheduler decides thread B to run which also wants to create an instance of LoadBalancer. This results in two instances of the LoadBalancer to be created. That's why you have to lock the instanciation so that only one thread at a time can create the instance.
|
|
|
|
|
what is the difference between Compile Time Polymorphism & Run Time Polymorphism ?
|
|
|
|
|
See here.[^]
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
|
|
|
|
|
That was simply awesome
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
yup. My favorite cluebat for trivial homework questions. It's much quicker than writing pseudocode to bludgeon them, although I try not to use it too often.
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
|
|
|
|
|
I use it too, as do others.
Kevin
|
|
|
|
|
wow
good answer, deserved my 5
Yusuf
|
|
|
|
|
|
Yowza! You scared the poor guy away!
/ravi
|
|
|
|
|
Ravi Bhavnani wrote: Yowza! You scared the poor guy away!
Nope... see a few posts above
|
|
|
|
|
Ram_Bytes wrote: what is the difference between Compile Time Polymorphism & Run Time Polymorphism
The same as Overloading and Overriding have.
I hope, you know what these two terms mean.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Polymorphism means that the certain functions can take different forms (take different actions), depending on how they are called.
With compile-time polymorphism, some of these actions can be determined at compile-time such as method overloading, operator overloading, and templates. The compiler knows how to build and call each instance of the method because they are all clearly defined in the code. This is also known as static polymorphism, static binding, or early binding.
Sometimes run-time polymorphism has to be used because the form that the method will take cannot be determined until run-time, such as virtual functions and function overriding through inheritance. This is also known as dynamic polymorphism, dynamic binding, or late binding.
By the way, in Java, method overloading and method overriding are both implemented during run-time only.
Enjoy,
Robert C. Cartaino
|
|
|
|
|
Ram_Bytes wrote: what is the difference between Compile Time Polymorphism & Run Time Polymorphism ?
<code>Compile </code>Time Polymorphism
<code>Run </code>Time Polymorphism
led mike
|
|
|
|
|
How do I change the datagrid.readonly property at run time
Sree
|
|
|
|
|
Double postingwont help you do that.
When do you want to change this attribute??
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
Why two Posts?
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
unlike C/++ you can't just cast const away anytime you find it annoying.
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
|
|
|
|