I've got a strange bug with Listbox.DataSource...

Z

Zoury

Hi there! :O)

I have a WebService that contains the following struct definition. this
struct is defined right after the WebService class, ie :
//***
namespace mynamespace
{
public class MyWebService : WebService {// code here..}
public struct Record
{
// fields
private Int32 _id;
private string _tableName;

// constructors
public Record(Int32 id, string tableName)
{
_id = id;
_tableName = tableName;
}

// public properties
public string TableName
{
get {return _tableName;}
set {_tableName = value;}
}
public Int32 ID
{
get {return _id;}
set {_id = value;}
}
}
//***

this is the class definition of the struct in reference.cs :
//***
/// <remarks/>

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public class Record {

/// <remarks/>
public string TableName;

/// <remarks/>
public int ID;
}
//***




In a window application i must load a listbox from a Record[] type,
so i did something like this :
//***
private void FillList(Record[] records)
{
// lbRecords is a ListBox
lbRecords.DataSource = records;
lbRecords.DisplayMember = "TableName";
lbRecords.ValueMember = "ID";
}
//***

in this case I got an ArgumentException on the last line of code.
Additionnal information : Could not bind to the new display
member.

PLUS, right after the exception occured, i click [Break], then over the
mouse cursor over DisplayMember and ValueMember to see what there are set
to. Strangely, DisplayMember contains "ID" and ValueMember contains ""...


so i decided to try something else... :
//***
private void FillList(Record[] records)
{
// lbRecords is a ListBox
lbRecords.DisplayMember = "TableName";
lbRecords.ValueMember = "ID";
lbRecords.DataSource = records;
}
//***

This way, no exceptions are thrown.. but, once the last line of code
executes, the DisplayMember value gets replaced by "" and the only thing
appearing in the listbox is the type information of Record which is probably
caused by the fact the listbox calls .ToString() on each members of the
Array.



Can anybody see what is wrong with this code ?!
I mean, "TableName" is properly spelled.... is there a problem with using a
struct definition from a WebService ?

Thanks a lot for reading me!
 
J

Justin Rogers

Yeah, this kinda sucks. What you need is an adapter that surfaces your two
fields as properties. I generally call these by a rather nasty name each time
I have to generate them by hand ;-)

class or struct RecordWrapper {
private Record record;
public RecordWrapper(Record record) { this.record = record; }
public string TableName { get { return this.record.TableName; } }
public int Id = { get { return this.record.Id; } }
}


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Zoury said:
Hi there! :O)

I have a WebService that contains the following struct definition. this
struct is defined right after the WebService class, ie :
//***
namespace mynamespace
{
public class MyWebService : WebService {// code here..}
public struct Record
{
// fields
private Int32 _id;
private string _tableName;

// constructors
public Record(Int32 id, string tableName)
{
_id = id;
_tableName = tableName;
}

// public properties
public string TableName
{
get {return _tableName;}
set {_tableName = value;}
}
public Int32 ID
{
get {return _id;}
set {_id = value;}
}
}
//***

this is the class definition of the struct in reference.cs :
//***
/// <remarks/>

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public class Record {

/// <remarks/>
public string TableName;

/// <remarks/>
public int ID;
}
//***




In a window application i must load a listbox from a Record[] type,
so i did something like this :
//***
private void FillList(Record[] records)
{
// lbRecords is a ListBox
lbRecords.DataSource = records;
lbRecords.DisplayMember = "TableName";
lbRecords.ValueMember = "ID";
}
//***

in this case I got an ArgumentException on the last line of code.
Additionnal information : Could not bind to the new display
member.

PLUS, right after the exception occured, i click [Break], then over the
mouse cursor over DisplayMember and ValueMember to see what there are set
to. Strangely, DisplayMember contains "ID" and ValueMember contains ""...


so i decided to try something else... :
//***
private void FillList(Record[] records)
{
// lbRecords is a ListBox
lbRecords.DisplayMember = "TableName";
lbRecords.ValueMember = "ID";
lbRecords.DataSource = records;
}
//***

This way, no exceptions are thrown.. but, once the last line of code
executes, the DisplayMember value gets replaced by "" and the only thing
appearing in the listbox is the type information of Record which is probably
caused by the fact the listbox calls .ToString() on each members of the
Array.



Can anybody see what is wrong with this code ?!
I mean, "TableName" is properly spelled.... is there a problem with using a
struct definition from a WebService ?

Thanks a lot for reading me!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top