Conditional Expression Testing for Nulls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a

    #1

    Conditional Expression Testing for Nulls

    I'm having trouble testing a custom object. I've tried many different
    approaches. One is shown below.

    The XML below shows the state of the object and I'm trying to test for that
    state, ie there are NO classes in the Classes collection (Classes) of the
    custom object.

    The conditional expression returns the error shown below it...

    =============== =============== =============== ==========XML returned by
    Custom Object

    <ArrayOfTeacher s xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http ://www.w3.org/2001/XMLSchema">

    <Teacher Id="1" Name="Bob" >

    <Classes />

    </Teacher>

    </ArrayOfTeachers >

    =============== =============== =============== ==========Condi tional Expression

    if (Profile.Teache rs[0].Classes[0] == null) // fails

    =============== =============== =============== ========== Error message

    Index was out of range. Must be non-negative and less than the size of the
    collection.
    Parameter name: index

    Source Error:



    Line 277: get
    Line 278: {
    Line 279: return (Class)List[index];
    Line 280: }
    Line 281: }





    Any ideas on how to test for No Classes in the Class collection? I can post
    the custom object if that would help.

    Thanks,

    Paul

  • Jon Skeet [C# MVP]

    #2
    Re: Conditional Expression Testing for Nulls

    a <a@discussions. microsoft.com> wrote:[color=blue]
    > I'm having trouble testing a custom object. I've tried many different
    > approaches. One is shown below.
    >
    > The XML below shows the state of the object and I'm trying to test for that
    > state, ie there are NO classes in the Classes collection (Classes) of the
    > custom object.
    >
    > The conditional expression returns the error shown below it...[/color]

    <snip>

    How about

    if (Profile.Teache rs[0].Classes.Count= =0)

    It's hard to say exactly whether that'll work or not without knowing
    what the type of the property "Classes" is, but it's worth a try...

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • a

      #3
      Re: Conditional Expression Testing for Nulls

      Jon:

      I tried that, as well as everything else, but no go.

      The Count method does not exist on the Teahcers object, just on the Teacer
      object.

      I wondered if there was a problem with the indexer because I'm using this
      inside a dropdownlist, but I don't think that is the problem. It may have
      more to do with the way that I structured the custom object. Since this is
      my first custom object, I definitely don't know what I'm doing.

      I'll include the custom object. It's purpose is to has Teachers who have
      Classes that in turn have Students.

      Any other ideas?

      Thanks
      ---------------------------------------------------------------------------

      using System;
      using System.Collecti ons;
      using System.Xml.Seri alization;//[XmlIgnore] // XmlIgnore is need for
      proper nesting of the xml output

      namespace Teachers1
      {
      #region Student
      Object+++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++

      [Serializable()]
      public class Student
      {
      #region Private members (Fields) +++++++++++++++ +++++

      int id;
      string name;

      #endregion -------------------------------------------

      #region Public Accessors (Properties) ++++++++++++++

      [XmlAttribute]
      public int Id
      {
      get { return this.id; }
      set { this.id = value; }
      }

      [XmlAttribute]
      public string Name
      {
      get { return this.name; }
      set { this.name = value; }
      }

      #endregion -------------------------------------------

      #region Constructors +++++++++++++++ +++++++++++++++ +++

      // Empty constructor
      public Student()
      { }

      // Constructor 2
      public Student(int Id, string Name)
      {
      this.id = Id;
      this.name = Name;
      }

      #endregion -------------------------------------------
      }

      #endregion

      [Serializable()]
      public class Students : CollectionBase
      {
      #region Indexer +++++++++++++++ +++++++++++++++ +++

      public Student this[int index]
      {
      set
      {
      List[index] = value;
      }
      get
      {
      return (Student)List[index];
      }
      }

      #endregion ------------------------------------

      #region Public Methods +++++++++++++++ +++++++++++

      public int Add(Student value)
      {
      return List.Add(value) ;
      }
      public int IndexOf(Student value)
      {
      return List.IndexOf(va lue);
      }
      public void Insert(int index, Student value)
      {
      List.Insert(ind ex, value);
      }
      public void Remove(Student value)
      {
      List.Remove(val ue);
      }
      public bool Contains(Studen t value)
      {
      return List.Contains(v alue);
      }

      #endregion -------------------------------------------

      #region Constructors +++++++++++++++ +++++++++++++++ +++

      public Students()
      { }

      #endregion -------------------------------------------
      }



      #region Class
      Object+++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++

      [Serializable()]
      public class Class
      {
      #region Private members (Fields) +++++++++++++++ +++++

      int id;
      string name;
      Teachers1.Stude nts students = new Teachers1.Stude nts();

      #endregion -------------------------------------------

      #region Public Accessors (Properties) ++++++++++++++

      [XmlAttribute]
      public int Id
      {
      get { return id; }
      set { id = value; }
      }
      [XmlAttribute]
      public string Name
      {
      get { return name; }
      set { name = value; }
      }

      //[XmlAttribute] can't use on Complex Type
      public Teachers1.Stude nts Students
      {
      get { return this.students; }
      set { this.students = value; }
      }

      #endregion -------------------------------------------

      #region Constructors +++++++++++++++ +++++++++++++++ +++

      // empty constructor
      public Class()
      { }

      // Full Constructor
      public Class(int Id, string Name, Teachers1.Stude nts
      Students)
      {
      this.id = Id;
      this.name = Name;
      this.students = Students;
      }

      #endregion -------------------------------------------
      }

      #endregion

      [Serializable()]
      public class Classes : CollectionBase
      {
      #region Indexer +++++++++++++++ +++++++++++++++ +++

      public Class this[int index]
      {
      set
      {
      List[index] = value;
      }
      get
      {
      return (Class)List[index];
      }
      }

      #endregion ------------------------------------

      #region Public Methods +++++++++++++++ +++++++++++

      public int Add(Class value)
      {
      return List.Add(value) ;
      }
      public int IndexOf(Class value)
      {
      return List.IndexOf(va lue);
      }
      public void Insert(int index, Class value)
      {
      List.Insert(ind ex, value);
      }
      public void Remove(Class value)
      {
      List.Remove(val ue);
      }
      public bool Contains(Class value)
      {
      return List.Contains(v alue);
      }

      #endregion -------------------------------------------

      #region Constructors +++++++++++++++ +++++++++++++++ +++

      public Classes()
      { }

      #endregion -------------------------------------------
      }



      #region Teacher
      Object+++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++

      [Serializable()]
      public class Teacher
      {
      #region Private members (Fields) +++++++++++++++ +++++

      private int id;
      private string name;
      Teachers1.Class es classes = new Teachers1.Class es();

      #endregion -------------------------------------------

      #region Public Accessors (Properties) ++++++++++++++
      //[XmlAttribute(At tributeName = "id", Namespace =
      "urn:schema s-microsoft-com:xml-updategram")]
      [XmlAttribute]
      public int Id
      {
      get { return id; }
      set { id = value; }
      }
      [XmlAttribute]
      public string Name
      {
      get { return name; }
      set { name = value; }
      }

      //[XmlAttribute] can't use on Complex Type
      public Teachers1.Class es Classes
      {
      get { return this.classes; }
      set { this.classes = value; }
      }

      #endregion -------------------------------------------

      #region Constructors +++++++++++++++ +++++++++++++++ +++

      // Default Constructor
      public Teacher()
      { }

      // Constructor 2
      public Teacher(int id, string Name, Teachers1.Class es
      Classes)
      {
      this.id = Id;
      this.name = Name;
      this.classes = Classes;
      }


      #endregion -------------------------------------------
      }

      #endregion

      [Serializable()]
      public class Teachers : CollectionBase
      {
      #region Private members (Fields) +++++++++++++++


      #endregion --------------------------------------

      #region Indexer +++++++++++++++ +++++++++++++++ +++

      public Teacher this[int index]
      {
      set
      {
      List[index] = value;
      }
      get
      {
      return (Teacher)List[index];
      }
      }

      #endregion ------------------------------------

      #region Public Methods +++++++++++++++ +++++++++++

      public int Add(Teacher value)
      {
      return List.Add(value) ;
      }
      public int IndexOf(Teacher value)
      {
      return List.IndexOf(va lue);
      }
      public void Insert(int index, Teacher value)
      {
      List.Insert(ind ex, value);
      }
      public void Remove(Teacher value)
      {
      List.Remove(val ue);
      }
      public bool Contains(Teache r value)
      {
      return List.Contains(v alue);
      }
      #endregion --------------------------------------

      #region Constructors +++++++++++++++ +++++++++++++

      public Teachers() { }

      #endregion ---------------------------------------
      }
      }


      =============== =============== =============== =

      "Jon Skeet [C# MVP]" wrote:
      [color=blue]
      > a <a@discussions. microsoft.com> wrote:[color=green]
      > > I'm having trouble testing a custom object. I've tried many different
      > > approaches. One is shown below.
      > >
      > > The XML below shows the state of the object and I'm trying to test for that
      > > state, ie there are NO classes in the Classes collection (Classes) of the
      > > custom object.
      > >
      > > The conditional expression returns the error shown below it...[/color]
      >
      > <snip>
      >
      > How about
      >
      > if (Profile.Teache rs[0].Classes.Count= =0)
      >
      > It's hard to say exactly whether that'll work or not without knowing
      > what the type of the property "Classes" is, but it's worth a try...
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Conditional Expression Testing for Nulls

        a <a@discussions. microsoft.com> wrote:[color=blue]
        > I tried that, as well as everything else, but no go.
        >
        > The Count method does not exist on the Teahcers object, just on the Teacer
        > object.[/color]

        I wasn't calling it on either of those though - I was calling it on a
        Classes object, which does have a Count method.

        If you want to know whether there are any *teachers*, use

        if (Profile.Teache rs.Count==0)

        Otherwise, the code I gave should show whether the first teacher has 0
        classes.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • a

          #5
          Re: Conditional Expression Testing for Nulls

          Jon:

          OK, I tested what you suggested and I evidently have another problem (it may
          be with the indexer).

          I tried this code to build all the objects at the same time and I get the
          same error as previously posted. Only the first object is built.


          Profile.Teacher s.Add(new Teachers1.Teach er());
          // Add New Teacher to Profile
          Profile.Teacher s[0].Classes.Add(ne w Teachers1.Class ());
          // Add New Class
          Profile.Teacher s[0].Classes[0].Students.Add(n ew
          Teachers1.Stude nt()); // Add New Student

          ----------------------------------------------------------------

          <?xml version="1.0" encoding="utf-16"?>
          <ArrayOfTeacher s xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
          <Teacher Id="0" Name="Bob">
          <Classes />
          </Teacher>
          </ArrayOfTeachers >

          I'm going to try to restructure my code to see if I can avoid this, if I
          can't, I'll ask another question later, but thank you for verifying that
          other code for me.

          Paul
          =============== =============== ===============

          "Jon Skeet [C# MVP]" wrote:
          [color=blue]
          > a <a@discussions. microsoft.com> wrote:[color=green]
          > > I tried that, as well as everything else, but no go.
          > >
          > > The Count method does not exist on the Teahcers object, just on the Teacer
          > > object.[/color]
          >
          > I wasn't calling it on either of those though - I was calling it on a
          > Classes object, which does have a Count method.
          >
          > If you want to know whether there are any *teachers*, use
          >
          > if (Profile.Teache rs.Count==0)
          >
          > Otherwise, the code I gave should show whether the first teacher has 0
          > classes.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          > If replying to the group, please do not mail me too
          >[/color]

          Comment

          Working...