Re: Object instantiation in C#



Aha - got it. I never thought of initialising to null. As I think I've
mentioned before, Jesse Liberty's book doesn't seem to cover null at all.
Many thanks for that.
--
Dave


"Bill Butler" wrote:

> "Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:2C4B357F-1521-4CFA-95A5-04F01E8073DB@xxxxxxxxxxxxxxxx
>
> > I have a class MyClass that throws exceptions. It could throw one in the
> > constructor. If it throws I want to call another method on it (maybe
> > request
> > status)
>
> When an UNCAUGHT exception occurs in a constructor, the object is not
> created.
> Also the assignment never takes place ,so your reference will still be
> pointing to whatever it was pointing to before.
>
> Lets use this class for these examples:
>
> class Bad
> {
> public int X;
> public Bad(int x)
> {
> if (x > 3)
> throw new Exception("(x > 3)");
> X = x;
> }
> }
> -------------------------------------------------------
> Example 1: // similar to your example
> public static void Test1()
> {
> Bad bad = null; // This was what the compiler was complaining
> about
> try
> {
> bad = new Bad(5);
> }
> catch (Exception ex)
> {
> if (bad == null)
> Console.WriteLine("(bad == null)");
> else
> Console.WriteLine("bad.X = {0}", bad.X);
> }
> }
> -------------------------------------
> Output:
> (bad == null)
> =====================================================
> Example 2: // Add a loop
>
> public static void Test2()
> {
> Bad bad = null; // This was what the compiler was complaining
> about
>
> for (int i = 0; i < 10; i++)
> {
> try
> {
> bad = new Bad(i);
> }
> catch (Exception ex)
> {
> if (bad == null)
> Console.WriteLine("(bad == null)");
> else
> Console.WriteLine("bad.X = {0}", bad.X);
> }
> }
> }
> -------------------------------------
> Output:
> bad.X = 3
> bad.X = 3
> bad.X = 3
> bad.X = 3
> bad.X = 3
> bad.X = 3
> ==========================================================
>
>
> Hope this Helps
> Bill
>
>
>
.



Relevant Pages

  • Re: Object instantiation in C#
    ... > I have a class MyClass that throws exceptions. ... When an UNCAUGHT exception occurs in a constructor, ... pointing to whatever it was pointing to before. ...
    (microsoft.public.dotnet.languages.csharp)
  • Trouble with non-default constructors
    ... I have a class that needs a non-default constructor, ... class MyClass ... int Value; ... But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to share data?
    ... > int getdata; ... Why is readdata() public? ... Your constructor copies the values ...
    (comp.programming)
  • Re: Trouble with non-default constructors
    ... > I have a class that needs a non-default constructor, ... > class MyClass ... > void GetValue{return Value); ... > But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Trouble with non-default constructors
    ... In article, Peter Venis ... >I have a class that needs a non-default constructor, ... >class MyClass ... >But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)