Click here to Skip to main content
15,898,538 members
Home / Discussions / C#
   

C#

 
GeneralRe: New in .NET 2.0 Pin
Blumen17-May-07 3:27
Blumen17-May-07 3:27 
GeneralRe: New in .NET 2.0 Pin
Christian Graus17-May-07 3:30
protectorChristian Graus17-May-07 3:30 
GeneralRe: New in .NET 2.0 Pin
Judah Gabriel Himango17-May-07 5:18
sponsorJudah Gabriel Himango17-May-07 5:18 
AnswerRe: New in .NET 2.0 Pin
Russell Jones17-May-07 3:52
Russell Jones17-May-07 3:52 
AnswerRe: New in .NET 2.0 Pin
Nissim Salomon17-May-07 5:24
Nissim Salomon17-May-07 5:24 
Questionhow to get textbox control? Pin
newtocsharp17-May-07 3:14
newtocsharp17-May-07 3:14 
AnswerRe: how to get textbox control? Pin
Judah Gabriel Himango17-May-07 5:47
sponsorJudah Gabriel Himango17-May-07 5:47 
QuestionHelp needed to convert function to a generic function Pin
Jim Taylor17-May-07 3:11
Jim Taylor17-May-07 3:11 
I have a function that a function that takes in a string like "1,2,3,4324,3423". I need to return an array of a type that I specifiy using that string so in this case it would return an array of ints or shorts etc like { 1, 2, 3, 4324, 3423 }.
The code I wrote to do this is below ...
   <br />
usage: int[] myInts = (int[])CommaSeparatedStringToIEnumerable("1,2,3,4324,3423", typeof(int));     <br />
<br />
public static IEnumerable CommaSeparatedStringToIEnumerable(string str, <br />
Type destinationType)<br />
        {<br />
            List<string> list = new List<string>(str.Split(','));<br />
            list.RemoveAll(delegate(string s) { return s.Trim().Length == 0 ;});<br />
            IEnumerable output;<br />
            if (destinationType == typeof(int))<br />
                output = list.ConvertAll<int>(delegate(string s) { return Convert.ToInt32(s.Trim()); }).ToArray();<br />
            else if (destinationType == typeof(short))<br />
                output = list.ConvertAll<short>(delegate(string s) { return Convert.ToInt16(s.Trim()); }).ToArray();<br />
            else if (destinationType == typeof(byte))<br />
                output = list.ConvertAll<byte>(delegate(string s) { return Convert.ToByte(s.Trim()); }).ToArray();<br />
            else if (destinationType == typeof(bool))<br />
                output = list.ConvertAll<bool>(delegate(string s) { return Convert.ToBoolean(s.Trim()); }).ToArray();<br />
            else if (destinationType == typeof(string))<br />
                output = list.ConvertAll<string>(delegate(string s) { return s.Trim(); }).ToArray();<br />
            else<br />
                throw new NotSupportedException("Type not supported.");<br />
            return output;<br />
        }

I can't help thinking that this could be acheived using a generic function like
public static T[] CommaSeparatedStringToArray(string str)
but I've been round the houses trying to do this. Any ideas, good articles would be appreciated.



Jim

AnswerRe: Help needed to convert function to a generic function Pin
Not Active17-May-07 4:12
mentorNot Active17-May-07 4:12 
GeneralRe: Help needed to convert function to a generic function Pin
Jim Taylor17-May-07 4:59
Jim Taylor17-May-07 4:59 
AnswerRe: Help needed to convert function to a generic function Pin
Judah Gabriel Himango17-May-07 5:11
sponsorJudah Gabriel Himango17-May-07 5:11 
GeneralRe: Help needed to convert function to a generic function Pin
Jim Taylor17-May-07 5:34
Jim Taylor17-May-07 5:34 
GeneralRe: Help needed to convert function to a generic function Pin
Not Active17-May-07 6:20
mentorNot Active17-May-07 6:20 
GeneralRe: Help needed to convert function to a generic function Pin
Judah Gabriel Himango17-May-07 7:15
sponsorJudah Gabriel Himango17-May-07 7:15 
AnswerRe: Help needed to convert function to a generic function Pin
Nissim Salomon20-May-07 0:19
Nissim Salomon20-May-07 0:19 
QuestionWhat are the Books on C#..?? Pin
IamPoojaa17-May-07 2:39
IamPoojaa17-May-07 2:39 
AnswerRe: What are the Books on C#..?? Pin
Sathesh Sakthivel17-May-07 2:51
Sathesh Sakthivel17-May-07 2:51 
GeneralRe: What are the Books on C#..?? Pin
IamPoojaa17-May-07 3:08
IamPoojaa17-May-07 3:08 
GeneralRe: What are the Books on C#..?? Pin
Sathesh Sakthivel17-May-07 3:12
Sathesh Sakthivel17-May-07 3:12 
AnswerRe: What are the Books on C#..?? Pin
Kevin McFarlane17-May-07 3:13
Kevin McFarlane17-May-07 3:13 
QuestionTAPI Multmedia transmission Pin
CallieClearence17-May-07 2:21
CallieClearence17-May-07 2:21 
QuestionDiff b/w Windows Control Library and Class Library Pin
Rahul8317-May-07 0:52
Rahul8317-May-07 0:52 
AnswerRe: Diff b/w Windows Control Library and Class Library Pin
Pete O'Hanlon17-May-07 1:54
mvePete O'Hanlon17-May-07 1:54 
QuestionHow to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
prince_ppy17-May-07 0:42
prince_ppy17-May-07 0:42 
AnswerRe: How to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
andre_swnpl17-May-07 0:44
andre_swnpl17-May-07 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.