473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.AddRa nge(MyFoos.ToSt ring).

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 3558
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.AddRa nge(MyFoos.ToSt ring).

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.AddRa nge(MyFoos.ToAr ray)

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.AddRa nge(MyFoos.ToSt ring).

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
7958
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' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
6
17182
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 BindingFlag that will only get the Generic one? Here is an example ... public class UserTest { public ArrayList GetCollection() { return new ArrayList(); }
10
2321
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 call to work? Exists is a method within the Swatch class NOT the SwatchPanel class. Is this possible? Suggestions would be very welcome.
16
2382
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 or another (could also be 3) different web servers to use these Web Services. The Web Services are identical on all the machines. I tried just changing the URL of the Web Services and cannot make it work. I then decided to create 2 identical web...
7
5523
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, would return the value of obj because it returns the index of the item at position obj. Thoughts I had were to let the default method perform the default behavior and then create an overload as IndexOf(int index, ProductSearchCriteria criteria)...
9
5849
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
3
15018
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 Cannot implicitly convert type 'System.Collections.Generic.List<Prsym.ComboPopulation.ComboInfo>' to 'System.Collections.Generic.List<T> '
5
4116
by: Max2006 | last post by:
Hi, When I run this program: static void Main(string args) {
8
2537
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()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'. I tried to change method signature to
0
9645
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
9480
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,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
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,...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.