473,386 Members | 1,754 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,386 software developers and data experts.

Outputting generic list to array

I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employeeemp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and organization
properties.

Any help is appreciated!
Mar 14 '07 #1
5 18147
Try this code.

Employee[] empArray = emp.ToArray();

Since emp is a List<Employee>, emp.ToArray returns an array of
Employees, not strings.

================
Clay Burch
Syncfusion, Inc
Mar 14 '07 #2
MarkAurit <Sa**********@discussions.microsoft.comwrote:
I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employeeemp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and organization
properties.
If you want to convert it to an array of strings, you'll have to
convert each Employee to a string individually, e.g. with ToString().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '07 #3
I can certainly do that. But then what is the purpose of ToArray()?

"Jon Skeet [C# MVP]" wrote:
MarkAurit <Sa**********@discussions.microsoft.comwrote:
I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employeeemp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and organization
properties.

If you want to convert it to an array of strings, you'll have to
convert each Employee to a string individually, e.g. with ToString().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '07 #4

"SandpointGuy" <Sa**********@discussions.microsoft.comwrote in message
news:78**********************************@microsof t.com...
>I can certainly do that. But then what is the purpose of ToArray()?
converting from List<Tto T[].

Of course, you can do (not compile-tested):

string[] empArray = Array.Convert<Employee,string>(emp.ToArray,
delegate(Employee from) { return from.ToString(); });
>
"Jon Skeet [C# MVP]" wrote:
>MarkAurit <Sa**********@discussions.microsoft.comwrote:
I need to output a Generic List to an array, but Ive been unable to.

The list looks like:
List<Employeeemp = new List<Employee>();

When I use ToArray() with it:
string[] empArray = emp.ToArray();
I get the error message:
Cannot implicitly convert type 'NgItIts.AD2Service.Ad2Employee[]' to
'string[]'
The online docs show a GetRange(), but Im unable to get to work.

The Employee class has useraccount, name, emailaddress, and
organization
properties.

If you want to convert it to an array of strings, you'll have to
convert each Employee to a string individually, e.g. with ToString().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 15 '07 #5
yes, it's working! thanks

tested with:

string[] empArray = Array.ConvertAll<Employee, string>(emp.ToArray(), delegate(Employee from) { return from.ToString(); });

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Mar 18 '07 #6

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

Similar topics

2
by: Richard Bysouth | last post by:
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...
11
by: ZenRhapsody | last post by:
Has anyone done any performance testing between new generic Lists and single dimensional arrays? I really like the code flexibility the List provides since I don't know how many items I will...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
1
by: Miesha.James | last post by:
Hello, I'm trying to rewrite visual c++ code into visual c++ .NET code and I've run across a problem with storing objects into a list. Here;s an example of the code I have: ref struct...
2
by: shapper | last post by:
Hello, I have a generic list as follows: Dim categories As Generic.List(Of Enumeration.Category) Category is an enumeration as follows: Public Enum Category Book Document
3
by: Arcadefreaque | last post by:
I'm realizing how little I know about C++ today as I try to convert some functionality from VB.Net to C++. I have a procedure that I need to pass a List of objects to, and cannot see how to do...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: DR | last post by:
System.Collections.Generic.List<intmyList = new System.Collections.Generic.List<int>(100); Does this preallocate 100 integers? Also, is there any way to preallocate an array of objects all at...
2
by: Chris Kennedy | last post by:
I am getting a generic list of objects from a webservice. The list is converted to an array by this process. How do I convert the array back to a list and is there any way of finding an object...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.