|
static T[] CommaSeparatedStringToIEnumerable<T>(string values)<br />
{<br />
List<string> list = new List<string>(values.Split(','));<br />
List<T> ret = new List<T>();<br />
<br />
foreach(string value in list)<br />
ret.Add(ConvertValue<T>(value));<br />
<br />
return ret.ToArray();<br />
}<br />
<br />
static T ConvertValue<T>(object value)<br />
{<br />
Converter<object, T> convert =<br />
new Converter<object, T>(delegate(object val) { return (T)val; });<br />
<br />
switch(typeof(T).ToString())<br />
{<br />
case "System.Int16":<br />
case "System.Int32":<br />
int RetInt;<br />
if(int.TryParse(value.ToString(), out RetInt))<br />
return convert(RetInt);<br />
break;<br />
<br />
case "System.Single":<br />
float RetFloat;<br />
if(float.TryParse(value.ToString(), out RetFloat))<br />
return convert(RetFloat);<br />
break;<br />
<br />
case "System.Double":<br />
double RetDouble;<br />
if(double.TryParse(value.ToString(), out RetDouble))<br />
return convert(RetDouble);<br />
break;<br />
<br />
default:<br />
return convert(value);<br />
}<br />
<br />
return convert(value);<br />
}
only two letters away from being an asset
|
|
|
|
|
Many thanks Mark, that works a treat!!
Jim
|
|
|
|
|
Here's an elegant way to do it with using C# 2's yield iterators, generics, and delegate inference:
public static IEnumerable<T> CommaSeperatedToIEnumerable<T>(string commaSeperatedValues, Converter<string, T> converter)
{
foreach (string value in commaSeperatedValues.Split(','))
{
yield return converter(value);
}
}
Consuming code would look like this:
IEnumerable<int> myInts = CommaSeperatedSTringToIEnumerable<int>("1,2,3,4324,3423", int.Parse);
Powerful, fun, elegant, works on all types of T. Pretty efficient too, since the results will be computed only as you ask for the values in a foreach.
|
|
|
|
|
Thanks Judah that is quite cool. I'm really enjoying learning the new language features in .NET 2.0!
Jim
|
|
|
|
|
Damn, I always forget about the yield keyword, then get reminded, then forget all about it again.
only two letters away from being an asset
|
|
|
|
|
The real trick of the above solution, of course, is the Converter<int, T> delegate; without it, we'd have to resort back to the a bunch of if/elses using hard coded types. That's no fun.
The yield stuff is really nice. LINQ uses it heavily; you can imagine the LINQ 'where' keyword actually uses a function underneath the hood:
public static IEnumerable<T> Where<T>(IEnumerable<T> items, Predicate<T> criteria)
{
foreach(T item in items)
{
if(criteria(item)) yield return item;
}
}
Of course, with current C# 2 syntax, consuming this is a little ugly:
int[] ints = {5, 10, 15, 20};
IEnumerable<int> evenIntegers = Linq.Where(ints, delegate(int i) { return i % 2 == 0; });
C# 3 makes this a little better with lambda expressions:
IEnumerable<int> evenIntegers = Linq.Where(ints, i => i % 2 == 0);
C# 3 improves it further by extension methods: taking static methods like our Where method and making them appear to be instance methods:
IEnumerable<int> evenIntegers = ints.Where(i => i % 2 == 0);
Better, but still a little odd. The "language integrated" part of LINQ adds some new keywords to C# 3:
IEnumerable<int> evenIntegers = from ints where i % 2 == 0 select i;
Pretty cool stuff; builds heavily on C# 2's delegate inference, anonymous methods, and yielding iterators.
And lastly, it's rather laborious to type IEnumerable<int> all over:
var evenIntegers = from ints where i % 2 == 0 select i;
|
|
|
|
|
Hi
the following code use reflection in order to convert string items to value
types using the static function TryParse.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace CMDLineTest
{
class Program
{
static List<t> CommaSeparatedStringToIEnumerable<t>(string s)
{
List<t> values = new List<t>();
Type targetConversionType = typeof(T);
if (targetConversionType.IsValueType)
{
Type targetConversionTypeOutParamType = Type.GetType(targetConversionType.FullName + @"&");
if(targetConversionTypeOutParamType == null)
throw new Exception("");
MethodInfo tryParseMethodInfo = targetConversionType.GetMethod("TryParse", new Type[] { typeof(string), targetConversionTypeOutParamType });
if(tryParseMethodInfo != null)
{
string[] stringsToConvert = s.Split(',');
T item = default(T);
object[] methodParams = new object[2];
foreach (string str in stringsToConvert)
{
methodParams[0] = str;
methodParams[1] = item;
if (((bool)tryParseMethodInfo.Invoke(null, methodParams)) == true)
values.Add((T)methodParams[1]);
}
}
}
return values;
}
static void Main(string[] args)
{
List<int> intList = CommaSeparatedStringToIEnumerable<int>("1,2,3,4,5");
foreach (int item in intList)
Console.WriteLine(item);
}
}
}
|
|
|
|
|
Hi Friends, can anybody suggest what are the good books for learning C#.NET. I am interested in developing WEB SERVICES in C#.NET
I have knowledge in C, C++, VC++
Suggest some good books using which I can master c#.NET
Thanks ....
poojaaa
|
|
|
|
|
Professional C# Web Services: Building .NET Web Services with ASP.NET and .NET Remoting (Paperback) by wrox publication.
by Andrew Krowczyk (Author), Zach Greenvoss (Author), Christian Nagel (Author), Ashish Banerjee (Author), Thiru Thangarathinam (Author), Aravind Corera (Author), Chris Peiris (Author), Brad Maiani (Author).
Regards,
Satips.
|
|
|
|
|
|
|
|
Does any body know how to capture audio an video form webcam and stream it direcly on to tapi Call, with out first storing it on the disk.
CallieClearence
|
|
|
|
|
hi friends..
while creating a new project in .NET 2 we have an options called Windows Apllication,Windows Control Library,class Library etc..well working on my previous project i used class library to create components in my project..
"Windows Control Library" suggests that we are going to create user controls
but we can do that thing also if we can go for class library(by right clicking class library and thn ADD-->New Item-->user Contrls)..thn whats the diff.
|
|
|
|
|
Well, there's none in reality. The only difference being what you get in the initial project, i.e. either an empty class or a barebones control class. They both compile to .DLL format.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi friends,
plz help me
i am a web developer and now i want to write a desktop application.
the dackend is "ms sql 2000" and front end is "Visual C# 2005".,but i didn\t find any control to connect to ms sql 2000
plz help
prince
|
|
|
|
|
You can use SqlConnection object to connect.
|
|
|
|
|
any control to connect.. if u can suggest me any sampple project then it will be great
prince
|
|
|
|
|
There is no control but you could do something like this:
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "...";
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "your sql sp Here";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = ...;
SqlDataReader rdr = cmd.ExecuteReader();
You could look for samples here on codeproject.
|
|
|
|
|
If you type your question in to google, you're bound to find a ton of articles on this.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
you can use SQLDataSource control to connect with your db..
or u can use the classes to connect (through code)
u can search on google too. for this
|
|
|
|
|
Excel.Workbook theWorkbook = this.Excelobj.Workbooks.Open(strInputFileName, 0, false, 5,
"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,0, true,false,false);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet excelWorksheet = (Excel.Worksheet)sheets.get_Item(1);
Excel.Range tempRange = (Excel.Range) excelWorksheet.get_Range("C1","IV1");
System.Array tempArray = (System.Array)tempRange.Cells.Value2;
string[] exchangeData = ConvertToStringArray(tempArray);
//Determining the Excel Cell data range
int totalExcCount = exchangeData.Length;
string remarkCellName = convIndexToCellName(totalExcCount+3);
string lastExcCellName = convIndexToCellName(totalExcCount+2);
//Creating a new column "Remarks" in the excel.
Excel.Range remarkRange = (Excel.Range) excelWorksheet.get_Range(remarkCellName + "1",remarkCellName + "1");
string strRemark = "Remarks";
Object[] args1 = new Object[1];
args1[0] = strRemark;
remarkRange.GetType().InvokeMember("Value",BindingFlags.SetProperty,null,remarkRange,args1);
Now as above when writing in excel
I want that I should be able to write "Remarks" with some different color say red
how can i do that
Thanks
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|
|
the get_range method itself has few poperties...
Try out the Font property and thereby you can set the color for the font!!
Gautham
|
|
|
|
|
Ankit,
Here is the Solution:
Call this line
remarkRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
and this will set the Font color to RED.
Manoj
|
|
|
|
|
To fill the color for One cell,
((Excel.Range)worksheet.Cells[row, column]).Interior.Color = ColorTranslator.ToOle(color) ;
To fill the color for range.
worksheet.get_Range("A1","A2").Interior.Color = ColorTranslator.ToOle(color);
Hope to catch well!!!
|
|
|
|
|