473,467 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

IList<> object initialization problem

I've got this in my code, that is, we're inside a class called Patient,
so a Patient can have Appointments:
private IList<Appointment> _appointments = null;
public IList<Appointment> Appointments
{
get
{
if (_appointments == null)
{
RetrievePatientAppointments();
}
return _appointments;
}
set
{
_appointments = value;
}
}

The "RetrievePatientAppointments" method queries database, obtains
records and tries to add Appointment objects in a foreach bucle. The
pain is that when it is bound to add an Appointment, fails, because it
is null (The initialization of the IList is to null if you see).

How could I avoid IList being null? How to initialize it?

Nov 17 '05 #1
5 13625
You can initialize it to a List<T>, so for you example:

private IList<Appointment> _appointments = new List<Appointment>();

Nov 17 '05 #2

"Lonifasiko" <ml*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
<snip>
public IList<Appointment> Appointments
{
get
{
if (_appointments == null)
{
RetrievePatientAppointments();
}
return _appointments;
}
set
{
_appointments = value;
}
}

<snip>

It is , in general, a bad idea to make database queries in properties.
Properties are better when the data is already on hand or easily accessible.
This case look like a GetAppointments() Method might be a better fit.

Bill
Nov 17 '05 #3
"Nick Parker (C# MVP)" <pa*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
You can initialize it to a List<T>, so for you example:

private IList<Appointment> _appointments = new List<Appointment>();


Umm... You wouldn't want to do that in the definition, as that way, it
would never be null, and RetrievePatientAppointments() will never be called.
Better to do it inside RetrievePatientAppointments(). Best would be to
initialize a local variable inside RetrievePatientAppointments(), and return
that List.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #4
Hi,

You have to initialize _appointments inside RetrievePatientAppointments :

void RetrievePatientAppointments()
{
_appointments = new List<AppointMeint>();
//query the db
}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Lonifasiko" <ml*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I've got this in my code, that is, we're inside a class called Patient,
so a Patient can have Appointments:
private IList<Appointment> _appointments = null;
public IList<Appointment> Appointments
{
get
{
if (_appointments == null)
{
RetrievePatientAppointments();
}
return _appointments;
}
set
{
_appointments = value;
}
}

The "RetrievePatientAppointments" method queries database, obtains
records and tries to add Appointment objects in a foreach bucle. The
pain is that when it is bound to add an Appointment, fails, because it
is null (The initialization of the IList is to null if you see).

How could I avoid IList being null? How to initialize it?

Nov 17 '05 #5
Sorry guys, I did not notice I could do List<Appointment> _appointments
= new List<Appointment>()

I was trying to create a "new interface".......simply a bad programming
day.......

Thanks folks.

Nov 17 '05 #6

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

Similar topics

13
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
4
by: Mark | last post by:
I have a COM object that calls into a C# Forms library. The library can throw exceptions and I want to handle the exceptions in COM. Adam Nathan wrote a Microsoft sponsored book titled .Net and Com...
5
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count;...
2
by: Lucian Wischik | last post by:
Does ReadOnlyCollection<T> really implement IList<T>, like it claims to do? ... When I right-click on ReadOnlyCollection and look at its definition, it says this: public class...
1
by: Michael Primeaux | last post by:
Why does the generic SortedList and generic SortedDictionary not define any virtual members? Thanks, Michael
6
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true...
2
by: Tom | last post by:
Hi, I have a simple Product Domain Entity class with just fields/properties. I have a IList<Productthat I need to search for a particular Product object based on a field/property ProductID. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.