473,772 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

<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

Feb 24 '06 #1
4 1925
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.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 - <sk***@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
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.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:
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.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 - <sk***@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

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.Teache rs.Count==0)

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

--
Jon Skeet - <sk***@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
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.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:
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.Teache rs.Count==0)

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

--
Jon Skeet - <sk***@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

Feb 24 '06 #5

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

Similar topics

8
2288
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 from being a "language internist" (on Python or anything else) so go easy on me if this is stupid - it just seemed quite elegant to me as a relative newbie in town :-) I also havent got a clue whether this would be easy or even possible
28
3464
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 <!> in the original MSDN documentation, but this is (although it is working) unfortunately non-validating gibberish. So I fooled around trying to find a way to make it valid. And voila: <!--><!><!-->
5
2371
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 have been working with the conditional
1
5009
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 "MyField1" is True. No problem, I just need to write "Expression is" and " = -1 "
6
12142
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 : DateTime.Now; } When the condition is false, I want to return null. If true, I want to return the current date/time.
3
1967
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, but the RescheduledFromDate does. In general, only a GigDate will be supplied, but if a Gig is rescheduled, the GigDate is copied to RescheduledFromDate, and the GigDate then becomes the "new" date. I'm using a DataList instead of a Grid because...
17
1784
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>" & Recordset1.Fields.Item("profile1").Value%> </td>
5
1699
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
2904
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
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.