473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Conditional Expression Testing for Nulls

a
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

<ArrayOfTeachers 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>

================================================== =====Conditional Expression

if (Profile.Teachers[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

Feb 24 '06 #1
4 1910
a <a@discussions.microsoft.com> wrote:
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...


<snip>

How about

if (Profile.Teachers[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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #2
a
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.Collections;
using System.Xml.Serialization;//[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(value);
}
public void Insert(int index, Student value)
{
List.Insert(index, value);
}
public void Remove(Student value)
{
List.Remove(value);
}
public bool Contains(Student value)
{
return List.Contains(value);
}

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

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

public Students()
{ }

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

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

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

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

#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.Students 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.Students
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(value);
}
public void Insert(int index, Class value)
{
List.Insert(index, value);
}
public void Remove(Class value)
{
List.Remove(value);
}
public bool Contains(Class value)
{
return List.Contains(value);
}

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

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

public Classes()
{ }

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

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

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

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

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

#region Public Accessors (Properties) ++++++++++++++
//[XmlAttribute(AttributeName = "id", Namespace =
"urn:schemas-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.Classes 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.Classes
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(value);
}
public void Insert(int index, Teacher value)
{
List.Insert(index, value);
}
public void Remove(Teacher value)
{
List.Remove(value);
}
public bool Contains(Teacher value)
{
return List.Contains(value);
}
#endregion --------------------------------------

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

public Teachers() { }

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

"Jon Skeet [C# MVP]" wrote:
a <a@discussions.microsoft.com> wrote:
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...


<snip>

How about

if (Profile.Teachers[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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 24 '06 #3
a <a@discussions.microsoft.com> wrote:
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 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.Teachers.Count==0)

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #4
a
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.Teachers.Add(new Teachers1.Teacher());
// Add New Teacher to Profile
Profile.Teachers[0].Classes.Add(new Teachers1.Class());
// Add New Class
Profile.Teachers[0].Classes[0].Students.Add(new
Teachers1.Student()); // Add New Student

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

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTeachers 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:
a <a@discussions.microsoft.com> wrote:
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 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.Teachers.Count==0)

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

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

Feb 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
5
by: John Baker | last post by:
Hi: I have a field that i wish to use the conditional format capability on, and for some reason it wont work. The field is a a text box: =- I would like to make it red when negative, and...
1
by: GGerard | last post by:
Hello Is there a way to use a variable in the Conditional Formatting of a Textbox? Example : I want the background of a textbox in a continuous form to change color when the value of...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
3
by: Danny Tuppeny | last post by:
Hi all, I've got a DataList that's bound to a datasource with two columns (well, two that matter). One is called GigDate, and one is called RescheduledFromDate. GigDate doesn't allow NULLs,...
17
by: raj chahal | last post by:
Hi there I need to be able to print on screen when I check if a value within a db <td> <% if ((Recordset1.Fields.Item("profile1").Value) <> "") then response.Write"Pro1<br>" &...
5
by: A.M | last post by:
Hi, I am using Python 2.4. I read the PEP 308 at: http://www.python.org/dev/peps/pep-0308/
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.