473,404 Members | 2,114 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,404 software developers and data experts.

creating a ToString method for a Generic List

I'm sure this is something pretty trivial but can't seem to figure out how
to do it.

I'm using Generic Lists in quite a few places in my app and for some of them
want to be able to create a ToString method that would return a formatted
string array.

For example, I might have a simple class as follows:

Class Foo

Public ItemIndex as short
Public Description as string

End Class

There would be a List object containing instances of this class e.g.

Dim MyFoos as List (Of Foo)

now say for populating a drop-down list I want to display items in the
following format:

1 : Item A
2: Item B
3: Item C
etc.

I would like to be able to populate the dropdown list (ddl) using the
AddRange() method, so I'd need something like
ddl.Items.AddRange(MyFoos.ToString).

How would I implement this method? I've tried creating my own NumberedList
class which inherits from Generic.List but couldn't see how I could access
the list itself.

Thanks in advance
Nov 21 '05 #1
2 3540
Richard Bysouth wrote:
I'm sure this is something pretty trivial but can't seem to figure out how
to do it.

I'm using Generic Lists in quite a few places in my app and for some of them
want to be able to create a ToString method that would return a formatted
string array.

For example, I might have a simple class as follows:

Class Foo

Public ItemIndex as short
Public Description as string

End Class

There would be a List object containing instances of this class e.g.

Dim MyFoos as List (Of Foo)

now say for populating a drop-down list I want to display items in the
following format:

1 : Item A
2: Item B
3: Item C
etc.

I would like to be able to populate the dropdown list (ddl) using the
AddRange() method, so I'd need something like
ddl.Items.AddRange(MyFoos.ToString).

How would I implement this method? I've tried creating my own NumberedList
class which inherits from Generic.List but couldn't see how I could access
the list itself.

Thanks in advance


I could be very wrong about this as I haven't done it myself, but I
believe you need to add the ToString method in your Foo class, not the
list. The addrange will go through each item in your list and to do
ToString method on it.

Chris
Nov 21 '05 #2
Chris

Thanks a lot! Think i was making too much of it. By implementing a ToString
method on the class itself, I was then able to use the List's ToArray
method...

ddl.Items.AddRange(MyFoos.ToArray)

Cheers :-)

"Chris" wrote:
Richard Bysouth wrote:
I'm sure this is something pretty trivial but can't seem to figure out how
to do it.

I'm using Generic Lists in quite a few places in my app and for some of them
want to be able to create a ToString method that would return a formatted
string array.

For example, I might have a simple class as follows:

Class Foo

Public ItemIndex as short
Public Description as string

End Class

There would be a List object containing instances of this class e.g.

Dim MyFoos as List (Of Foo)

now say for populating a drop-down list I want to display items in the
following format:

1 : Item A
2: Item B
3: Item C
etc.

I would like to be able to populate the dropdown list (ddl) using the
AddRange() method, so I'd need something like
ddl.Items.AddRange(MyFoos.ToString).

How would I implement this method? I've tried creating my own NumberedList
class which inherits from Generic.List but couldn't see how I could access
the list itself.

Thanks in advance


I could be very wrong about this as I haven't done it myself, but I
believe you need to add the ToString method in your Foo class, not the
list. The addrange will go through each item in your list and to do
ToString method on it.

Chris

Nov 21 '05 #3

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

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
6
by: Paul Welter | last post by:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
5
by: Max2006 | last post by:
Hi, When I run this program: static void Main(string args) {
8
by: Andrus | last post by:
Code below causes error in class definition line .....Isolator<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Myapp.Isolator<T>.GetEnumerator()'...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...
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...

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.