Collections...

  • Thread starter Thread starter Colin Basterfield
  • Start date Start date
C

Colin Basterfield

Hi,

I have a custom collection, which is roughly this

public class SiteList : CollectionBase
{
bla bla bla code, bla bla!
}

This list holds Site object which have two members, SiteName and
SiteManager. This list gets passed back thro a web service so I have done
an XMLInclude(typeof(Site)) in the WS Method bit. Now I am trying to load
this into a DropDownList, and here is my problem.

If the list was an ArrayList then I could for example, do something like
this

object[] yearList = siteSummary.GetYearList();

ArrayList yearArrayList = new ArrayList(yearList);

for (int i = 0; i < yearArrayList.Count; i++)

ddlYear.Items.Add(yearArrayList.ToString());

So I was thinking that I could do something like this

object[] siteObjList = siteSummary.GetSiteList();

SiteList siteList = new SiteList(siteObjList); etc etc

But of course there is no overloaded method in my SiteList class that allows
one to pass an ICollection in, as is the case for ArrayList, so I figure
just create one, a la:

public SiteList(ICollection c)

{

}

Then I realised I have no idea what the code in here needs to do, apart from
add the elements of "c" into the list, so wondered how to proceed...

Any ideas would be great!

TIA

Colin B
 
Hi,

Maybe I am misunderstanding your question, but you are correct, all you need
to do is add the elements if the ICollection to your collection.

public List( ICollection c )
{
if ( c == null )
{
throw new ArgumentNullException("c");
}

foreach( Site site in c )
{
Add( site );
}
}

One thing you can take advantage of is that CollectionBase actually
implements IList. Instead of looping through the items to add them to the
combobox you can use databinding.

SiteList siteList = new SiteList(siteObjList);

ddlYear.DataSource = siteList;


Hope this helps
 
Hi Chris,

Thanks for your reply whilst I was waiting I had a play around and did it
slightly differently to you:

public SiteList(ICollection c)
{
foreach (object o in c)
Add(o as Site)
}

where Add is
public void Add(Site site)
{
InnerList.Add(site);
}

There are still some problems however, the code does this is thro a web
service and so does this

1. object[] siteObjList = siteSummary.GetSiteList();
2. SiteList siteList = new SiteList(siteObjList);
3. ddlSiteList.DisplayName = 'SiteName';
4. ddlSiteList.DataSource = siteList;

Now line 1 sets up the list of objects, and there is the correct data in
each element, which I can see using QuickWatch, i.e.

Name Value
c {System.Array}
|- 0 {AJHB.SiteSummary.Site}
| |--- System.Object {AJHB.SiteSummary.Site}
| |--- SiteManager "FRED BLOGGS"
| |--- SiteName "ACAPULCO"
|- 1 etc etc...
etc...thro' to 5

Now I pass this into the overloaded constructor for SiteList, and it
iterates thro the collection 6 times, which is the length of the list, this
comes back and after line 2 has executed, siteList.Count is 6, but if I use
DataSource/DataBind, or a for loop to load up the DropDownList, the app
complains that "Object reference not set to an instance of an object"

I have an Indexer set up in SiteList, which looks like this
public Site this[int index]

{

get { return (Site) InnerList[index]; }

set { InnerList[index] = (Site) value; }

}

So in the for loop scenario I can do

ddlSiteName.Items.Add(siteList.SiteName.ToString());

So this to me proves that the Web Service XML serialization is working, but
the list isn't really instantiated, so any ideas, would be great!

Many thanks for your input
Colin

Chris Taylor said:
Hi,

Maybe I am misunderstanding your question, but you are correct, all you need
to do is add the elements if the ICollection to your collection.

public List( ICollection c )
{
if ( c == null )
{
throw new ArgumentNullException("c");
}

foreach( Site site in c )
{
Add( site );
}
}

One thing you can take advantage of is that CollectionBase actually
implements IList. Instead of looping through the items to add them to the
combobox you can use databinding.

SiteList siteList = new SiteList(siteObjList);

ddlYear.DataSource = siteList;


Hope this helps

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Colin Basterfield said:
Hi,

I have a custom collection, which is roughly this

public class SiteList : CollectionBase
{
bla bla bla code, bla bla!
}

This list holds Site object which have two members, SiteName and
SiteManager. This list gets passed back thro a web service so I have done
an XMLInclude(typeof(Site)) in the WS Method bit. Now I am trying to load
this into a DropDownList, and here is my problem.

If the list was an ArrayList then I could for example, do something like
this

object[] yearList = siteSummary.GetYearList();

ArrayList yearArrayList = new ArrayList(yearList);

for (int i = 0; i < yearArrayList.Count; i++)

ddlYear.Items.Add(yearArrayList.ToString());

So I was thinking that I could do something like this

object[] siteObjList = siteSummary.GetSiteList();

SiteList siteList = new SiteList(siteObjList); etc etc

But of course there is no overloaded method in my SiteList class that allows
one to pass an ICollection in, as is the case for ArrayList, so I figure
just create one, a la:

public SiteList(ICollection c)

{

}

Then I realised I have no idea what the code in here needs to do, apart from
add the elements of "c" into the list, so wondered how to proceed...

Any ideas would be great!

TIA

Colin B

 
Hi Colin,

There is not enough information here, but I guess that the elements added to
the custom collections (SiteList) are getting added as null? If that is the
case, is it possible that the Site type in the SiteList(ICollection c)
constructor is not the same type that is in the object[] returned from the
call to GetSiteList().

One question, if I understand correctly, siteSummary.GetSiteList() is the
web service call. Why not let GetSiteList() return a SiteList rather than a
object[] or even Site[]?

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Colin Basterfield said:
Hi Chris,

Thanks for your reply whilst I was waiting I had a play around and did it
slightly differently to you:

public SiteList(ICollection c)
{
foreach (object o in c)
Add(o as Site)
}

where Add is
public void Add(Site site)
{
InnerList.Add(site);
}

There are still some problems however, the code does this is thro a web
service and so does this

1. object[] siteObjList = siteSummary.GetSiteList();
2. SiteList siteList = new SiteList(siteObjList);
3. ddlSiteList.DisplayName = 'SiteName';
4. ddlSiteList.DataSource = siteList;

Now line 1 sets up the list of objects, and there is the correct data in
each element, which I can see using QuickWatch, i.e.

Name Value
c {System.Array}
|- 0 {AJHB.SiteSummary.Site}
| |--- System.Object {AJHB.SiteSummary.Site}
| |--- SiteManager "FRED BLOGGS"
| |--- SiteName "ACAPULCO"
|- 1 etc etc...
etc...thro' to 5

Now I pass this into the overloaded constructor for SiteList, and it
iterates thro the collection 6 times, which is the length of the list, this
comes back and after line 2 has executed, siteList.Count is 6, but if I use
DataSource/DataBind, or a for loop to load up the DropDownList, the app
complains that "Object reference not set to an instance of an object"

I have an Indexer set up in SiteList, which looks like this
public Site this[int index]

{

get { return (Site) InnerList[index]; }

set { InnerList[index] = (Site) value; }

}

So in the for loop scenario I can do

ddlSiteName.Items.Add(siteList.SiteName.ToString());

So this to me proves that the Web Service XML serialization is working, but
the list isn't really instantiated, so any ideas, would be great!

Many thanks for your input
Colin

Chris Taylor said:
Hi,

Maybe I am misunderstanding your question, but you are correct, all you need
to do is add the elements if the ICollection to your collection.

public List( ICollection c )
{
if ( c == null )
{
throw new ArgumentNullException("c");
}

foreach( Site site in c )
{
Add( site );
}
}

One thing you can take advantage of is that CollectionBase actually
implements IList. Instead of looping through the items to add them to the
combobox you can use databinding.

SiteList siteList = new SiteList(siteObjList);

ddlYear.DataSource = siteList;


Hope this helps

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Colin Basterfield said:
Hi,

I have a custom collection, which is roughly this

public class SiteList : CollectionBase
{
bla bla bla code, bla bla!
}

This list holds Site object which have two members, SiteName and
SiteManager. This list gets passed back thro a web service so I have done
an XMLInclude(typeof(Site)) in the WS Method bit. Now I am trying to load
this into a DropDownList, and here is my problem.

If the list was an ArrayList then I could for example, do something like
this

object[] yearList = siteSummary.GetYearList();

ArrayList yearArrayList = new ArrayList(yearList);

for (int i = 0; i < yearArrayList.Count; i++)

ddlYear.Items.Add(yearArrayList.ToString());

So I was thinking that I could do something like this

object[] siteObjList = siteSummary.GetSiteList();

SiteList siteList = new SiteList(siteObjList); etc etc

But of course there is no overloaded method in my SiteList class that allows
one to pass an ICollection in, as is the case for ArrayList, so I figure
just create one, a la:

public SiteList(ICollection c)

{

}

Then I realised I have no idea what the code in here needs to do,
apart
from
add the elements of "c" into the list, so wondered how to proceed...

Any ideas would be great!

TIA

Colin B

 
Hi again Chris,

You are right in that there wasn't enough info here to help, but I guess I
didn't want people to get bored reading it all.

Basically the web service code calls an DB Backend function which returns a
SiteList like this:

public SiteList GetSiteList()

{

SiteSummary siteSummary = new SiteSummary();

SiteList siteList = siteSummary.GetSiteList();

return siteList;

}

Like I said before I put an XmlInclude in for the Site objects, which is
what the SiteList is filled with, so I know that each field is populated
correctly. The proxy I created with WSDL has this for GetSiteList,
obviously plus the Begin and End Get bits:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("www.offtheroof.c
o.nz/GetSiteList", RequestNamespace="www.offtheroof.co.nz",
ResponseNamespace="www.offtheroof.co.nz",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

public Site[] GetSiteList() {

object[] results = this.Invoke("GetSiteList", new object[0]);

return ((Site[])(results[0]));

}

It also has the serialization for the Site object:

/// <remarks/>

[System.Xml.Serialization.XmlTypeAttribute(Namespace="www.offtheroof.co.nz")
]

public class Site {


/// <remarks/>

public string SiteName;


/// <remarks/>

public string SiteManager;

}

This brings me onto your second point, which is a fair one, because where my
code falls over is in the Client app where it does this

1. object[] siteObjList = siteSummary.GetSiteList();
2. SiteList siteList = new SiteList(siteObjList);


Like I said if I look at SiteObjList I see the fields of each item in the
list set as I expect, but it doesn't seem to work once I try to get at the
elements of siteList.

Here's where the game is up for me, I couldn't get the syntax correct in
order to populate it directly. Given that the proxy shows that it returns
Site[] I tried

Site[] siteList = siteSummary.GetSiteList();

this complains that Site is defined in multiple places, so is using the
declaration from the DB Backend dll which I obviously reference. This is
why I moved to the object[], which compiles but fails at runtime.

It is 22:45 on Tuesday night here in NZ, so I have to sleep now, so won't be
able to respond to any further questions you put to me to help me until
tomorrow morning, in about 8 hours, so please don't give up on this thread
as I believe we are almost there.

Many thanks for your help once again, regards

Colin B




Chris Taylor said:
Hi Colin,

There is not enough information here, but I guess that the elements added to
the custom collections (SiteList) are getting added as null? If that is the
case, is it possible that the Site type in the SiteList(ICollection c)
constructor is not the same type that is in the object[] returned from the
call to GetSiteList().

One question, if I understand correctly, siteSummary.GetSiteList() is the
web service call. Why not let GetSiteList() return a SiteList rather than a
object[] or even Site[]?

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Colin Basterfield said:
Hi Chris,

Thanks for your reply whilst I was waiting I had a play around and did it
slightly differently to you:

public SiteList(ICollection c)
{
foreach (object o in c)
Add(o as Site)
}

where Add is
public void Add(Site site)
{
InnerList.Add(site);
}

There are still some problems however, the code does this is thro a web
service and so does this

1. object[] siteObjList = siteSummary.GetSiteList();
2. SiteList siteList = new SiteList(siteObjList);
3. ddlSiteList.DisplayName = 'SiteName';
4. ddlSiteList.DataSource = siteList;

Now line 1 sets up the list of objects, and there is the correct data in
each element, which I can see using QuickWatch, i.e.

Name Value
c {System.Array}
|- 0 {AJHB.SiteSummary.Site}
| |--- System.Object {AJHB.SiteSummary.Site}
| |--- SiteManager "FRED BLOGGS"
| |--- SiteName "ACAPULCO"
|- 1 etc etc...
etc...thro' to 5

Now I pass this into the overloaded constructor for SiteList, and it
iterates thro the collection 6 times, which is the length of the list, this
comes back and after line 2 has executed, siteList.Count is 6, but if I use
DataSource/DataBind, or a for loop to load up the DropDownList, the app
complains that "Object reference not set to an instance of an object"

I have an Indexer set up in SiteList, which looks like this
public Site this[int index]

{

get { return (Site) InnerList[index]; }

set { InnerList[index] = (Site) value; }

}

So in the for loop scenario I can do

ddlSiteName.Items.Add(siteList.SiteName.ToString());

So this to me proves that the Web Service XML serialization is working, but
the list isn't really instantiated, so any ideas, would be great!

Many thanks for your input
Colin

Chris Taylor said:
Hi,

Maybe I am misunderstanding your question, but you are correct, all
you
need
to do is add the elements if the ICollection to your collection.

public List( ICollection c )
{
if ( c == null )
{
throw new ArgumentNullException("c");
}

foreach( Site site in c )
{
Add( site );
}
}

One thing you can take advantage of is that CollectionBase actually
implements IList. Instead of looping through the items to add them to the
combobox you can use databinding.

SiteList siteList = new SiteList(siteObjList);

ddlYear.DataSource = siteList;


Hope this helps

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Hi,

I have a custom collection, which is roughly this

public class SiteList : CollectionBase
{
bla bla bla code, bla bla!
}

This list holds Site object which have two members, SiteName and
SiteManager. This list gets passed back thro a web service so I
have
done
an XMLInclude(typeof(Site)) in the WS Method bit. Now I am trying
to
load
this into a DropDownList, and here is my problem.

If the list was an ArrayList then I could for example, do something like
this

object[] yearList = siteSummary.GetYearList();

ArrayList yearArrayList = new ArrayList(yearList);

for (int i = 0; i < yearArrayList.Count; i++)

ddlYear.Items.Add(yearArrayList.ToString());

So I was thinking that I could do something like this

object[] siteObjList = siteSummary.GetSiteList();

SiteList siteList = new SiteList(siteObjList); etc etc

But of course there is no overloaded method in my SiteList class that
allows
one to pass an ICollection in, as is the case for ArrayList, so I figure
just create one, a la:

public SiteList(ICollection c)

{

}

Then I realised I have no idea what the code in here needs to do, apart
from
add the elements of "c" into the list, so wondered how to proceed...

Any ideas would be great!

TIA

Colin B


 
Hi!

I just tried something and it seems to work. I took out the reference to
the DB backend dll, and then used

Site[] siteList = siteSummary.GetSiteList();

I used the for loop to populate my dropdownlist and it worked, so your
second point was valid!

I can't seem to get the DataSource thing working, but found this article on
the web which probably answers this question too, check it out:

http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/webservicewrapper.mspx

We may well be done now, unless you have anything more to add on this one,
so thanks once again Chris.

Cheers
Colin
Chris Taylor said:
Hi Colin,

There is not enough information here, but I guess that the elements added to
the custom collections (SiteList) are getting added as null? If that is the
case, is it possible that the Site type in the SiteList(ICollection c)
constructor is not the same type that is in the object[] returned from the
call to GetSiteList().

One question, if I understand correctly, siteSummary.GetSiteList() is the
web service call. Why not let GetSiteList() return a SiteList rather than a
object[] or even Site[]?

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Colin Basterfield said:
Hi Chris,

Thanks for your reply whilst I was waiting I had a play around and did it
slightly differently to you:

public SiteList(ICollection c)
{
foreach (object o in c)
Add(o as Site)
}

where Add is
public void Add(Site site)
{
InnerList.Add(site);
}

There are still some problems however, the code does this is thro a web
service and so does this

1. object[] siteObjList = siteSummary.GetSiteList();
2. SiteList siteList = new SiteList(siteObjList);
3. ddlSiteList.DisplayName = 'SiteName';
4. ddlSiteList.DataSource = siteList;

Now line 1 sets up the list of objects, and there is the correct data in
each element, which I can see using QuickWatch, i.e.

Name Value
c {System.Array}
|- 0 {AJHB.SiteSummary.Site}
| |--- System.Object {AJHB.SiteSummary.Site}
| |--- SiteManager "FRED BLOGGS"
| |--- SiteName "ACAPULCO"
|- 1 etc etc...
etc...thro' to 5

Now I pass this into the overloaded constructor for SiteList, and it
iterates thro the collection 6 times, which is the length of the list, this
comes back and after line 2 has executed, siteList.Count is 6, but if I use
DataSource/DataBind, or a for loop to load up the DropDownList, the app
complains that "Object reference not set to an instance of an object"

I have an Indexer set up in SiteList, which looks like this
public Site this[int index]

{

get { return (Site) InnerList[index]; }

set { InnerList[index] = (Site) value; }

}

So in the for loop scenario I can do

ddlSiteName.Items.Add(siteList.SiteName.ToString());

So this to me proves that the Web Service XML serialization is working, but
the list isn't really instantiated, so any ideas, would be great!

Many thanks for your input
Colin

Chris Taylor said:
Hi,

Maybe I am misunderstanding your question, but you are correct, all
you
need
to do is add the elements if the ICollection to your collection.

public List( ICollection c )
{
if ( c == null )
{
throw new ArgumentNullException("c");
}

foreach( Site site in c )
{
Add( site );
}
}

One thing you can take advantage of is that CollectionBase actually
implements IList. Instead of looping through the items to add them to the
combobox you can use databinding.

SiteList siteList = new SiteList(siteObjList);

ddlYear.DataSource = siteList;


Hope this helps

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Hi,

I have a custom collection, which is roughly this

public class SiteList : CollectionBase
{
bla bla bla code, bla bla!
}

This list holds Site object which have two members, SiteName and
SiteManager. This list gets passed back thro a web service so I
have
done
an XMLInclude(typeof(Site)) in the WS Method bit. Now I am trying
to
load
this into a DropDownList, and here is my problem.

If the list was an ArrayList then I could for example, do something like
this

object[] yearList = siteSummary.GetYearList();

ArrayList yearArrayList = new ArrayList(yearList);

for (int i = 0; i < yearArrayList.Count; i++)

ddlYear.Items.Add(yearArrayList.ToString());

So I was thinking that I could do something like this

object[] siteObjList = siteSummary.GetSiteList();

SiteList siteList = new SiteList(siteObjList); etc etc

But of course there is no overloaded method in my SiteList class that
allows
one to pass an ICollection in, as is the case for ArrayList, so I figure
just create one, a la:

public SiteList(ICollection c)

{

}

Then I realised I have no idea what the code in here needs to do, apart
from
add the elements of "c" into the list, so wondered how to proceed...

Any ideas would be great!

TIA

Colin B


 
Back
Top