Is it a valid Object Instance?...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kerem Gümrükcü

    #1

    Is it a valid Object Instance?...

    Hi,

    how can i find out whether a object is a valid instance or not?

    I have a object called objCF (base class is a Form) and i want to
    find out whether this object is a valid insatnce or a null reference.

    This must be accomplished in C#...

    Thanks in advance...


  • Carlos J. Quintero [.NET MVP]

    #2
    Re: Is it a valid Object Instance?...

    if (objCF != null)
    {
    // Valid instance
    }

    --

    Best regards,

    Carlos J. Quintero

    MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
    You can code, design and document much faster.
    Free resources for add-in developers:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



    "Kerem Gümrükcü" <kareem114@hotm ail.com> escribió en el mensaje
    news:usdTOE5XFH A.1152@tk2msftn gp13.phx.gbl...[color=blue]
    > Hi,
    >
    > how can i find out whether a object is a valid instance or not?
    >
    > I have a object called objCF (base class is a Form) and i want to
    > find out whether this object is a valid insatnce or a null reference.
    >
    > This must be accomplished in C#...
    >
    > Thanks in advance...
    >[/color]


    Comment

    • Kerem Gümrükcü

      #3
      Re: Is it a valid Object Instance?...

      Hi Carlos,

      this was also my first idea, but it does not work. let me describe what i
      want to do:

      I have a class called "ConfigurationF orm" and i have a class (beside a lot
      of other classes ;-) ) called
      MainForm. Inside the MainForm's class there is a dynamic (private)
      ConfigurationFo rm Object. This
      Object will be initialized inside the MainForms Constructor, but the form is
      still invisible until it
      will be raised visible with "cf.Show(); ". This is done by Menu from the
      MainForm or from a TrayIcons
      Menu in the TaskBar. Both share the same code routine. When i open the the
      CF(Configuratio nForm)
      with cf.Show() and close it later with cf.Close(), the pointer or in our
      case the reference will be no more
      valid to the cf Object. So far so good! It would be easy always to open a
      new instance of the CF and
      make it visible with cf.Show(). I cant use cf.Visible because of some
      special needs of the application.

      Its somethink like this i want to do, but how, because the "null"-Thing does
      not work, i always get a false
      as return value:

      if(cf == null){

      cf = new ConfigurationFo rm();
      cf.Show();

      }else{

      if(cf.Visible == false){
      cf.Visible == true; // possibly cf.Show();
      }
      }



      This "cf == null" thing does not work! I always get a false as return value,
      even when i use this expression on a valid (instanciated) cf object
      Ok, i could handle this with try--catch, but this is not a clean solution
      anway.


      Any more ideas........an ybody?


      Thanx in advance...


      Kerem Gümrükcü



      Comment

      • Carlos J. Quintero [.NET MVP]

        #4
        Re: Is it a valid Object Instance?...

        If you always close the form by code using cf.Close() then you can set cf to
        null too and your test will work always. Otherwise, you can try testing the
        cf.IsDisposed property:

        if (cf == null)
        {
        cf = new Form2();
        cf.Show();
        }
        else if (cf.IsDisposed)
        {
        cf = new Form2();
        cf.Show();
        }

        --

        Best regards,

        Carlos J. Quintero

        MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
        You can code, design and document much faster.
        Free resources for add-in developers:
        MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


        "Kerem Gümrükcü" <kareem114@hotm ail.com> escribió en el mensaje
        news:ODMFb65XFH A.2128@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Hi Carlos,
        >
        > this was also my first idea, but it does not work. let me describe what i
        > want to do:
        >
        > I have a class called "ConfigurationF orm" and i have a class (beside a lot
        > of other classes ;-) ) called
        > MainForm. Inside the MainForm's class there is a dynamic (private)
        > ConfigurationFo rm Object. This
        > Object will be initialized inside the MainForms Constructor, but the form
        > is still invisible until it
        > will be raised visible with "cf.Show(); ". This is done by Menu from the
        > MainForm or from a TrayIcons
        > Menu in the TaskBar. Both share the same code routine. When i open the the
        > CF(Configuratio nForm)
        > with cf.Show() and close it later with cf.Close(), the pointer or in our
        > case the reference will be no more
        > valid to the cf Object. So far so good! It would be easy always to open a
        > new instance of the CF and
        > make it visible with cf.Show(). I cant use cf.Visible because of some
        > special needs of the application.
        >
        > Its somethink like this i want to do, but how, because the "null"-Thing
        > does not work, i always get a false
        > as return value:
        >
        > if(cf == null){
        >
        > cf = new ConfigurationFo rm();
        > cf.Show();
        >
        > }else{
        >
        > if(cf.Visible == false){
        > cf.Visible == true; // possibly cf.Show();
        > }
        > }
        >
        >
        >
        > This "cf == null" thing does not work! I always get a false as return
        > value,
        > even when i use this expression on a valid (instanciated) cf object
        > Ok, i could handle this with try--catch, but this is not a clean solution
        > anway.
        >
        >
        > Any more ideas........an ybody?
        >
        >
        > Thanx in advance...
        >
        >
        > Kerem Gümrükcü
        >
        >
        >[/color]


        Comment

        • Kerem Gümrükcü

          #5
          Re: Is it a valid Object Instance?...

          Yes, it was this disposed thing what i was looking for!
          Thank you very much...


          Best Regards

          Kerem Gümrükcü


          Comment

          Working...