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

converting string to method, templates question

Problem:
I am looking to reduce my code size because I have many very simliar
functions e.g.:

private uint GenerateAcquisitionID(AcquisitionType[] c)
{
uint max = 0;
foreach (AcquisitionType a in c)
{
uint n = Convert.ToUInt32(a.acquisitionID);
if (n max) max = n;
}
return max + 1;
}

private uint GenerateContentID(ContentType[] c)
{
uint max = 0;
foreach (ContentType a in c)
{
uint n = Convert.ToUInt32(a.contentID);
if (n max) max = n;
}
return max + 1;
}

I'm not au faix with templates, yet, is it possible to do somehting
like this with templates, or woudl this jsut make it more inefficient?

private uint GenerateID<T>(T t, string field) where T :
IEnumerable<T>
{
uint max = 0;
foreach (T a in t)
{

uint n = Convert.ToUInt32(a.field); // how to do
this?
if (n max) max = n;
}
return max + 1;
}

Feb 19 '07 #1
6 3978
You *could* use reflection for
uint n = Convert.ToUInt32(a.field); // how to do
uint n= Convert.ToUInt32(a.GetType().GetProperty(field).Ge tValue(a, null));

But if performance is critical, I'd stuck with the special cases.

<da****@gcoders.comha scritto nel messaggio
news:11**********************@p10g2000cwp.googlegr oups.com...
Problem:
I am looking to reduce my code size because I have many very simliar
functions e.g.:

private uint GenerateAcquisitionID(AcquisitionType[] c)
{
uint max = 0;
foreach (AcquisitionType a in c)
{
uint n = Convert.ToUInt32(a.acquisitionID);
if (n max) max = n;
}
return max + 1;
}

private uint GenerateContentID(ContentType[] c)
{
uint max = 0;
foreach (ContentType a in c)
{
uint n = Convert.ToUInt32(a.contentID);
if (n max) max = n;
}
return max + 1;
}

I'm not au faix with templates, yet, is it possible to do somehting
like this with templates, or woudl this jsut make it more inefficient?

private uint GenerateID<T>(T t, string field) where T :
IEnumerable<T>
{
uint max = 0;
foreach (T a in t)
{

uint n = Convert.ToUInt32(a.field); // how to do
this?
if (n max) max = n;
}
return max + 1;
}

Feb 19 '07 #2
thanks. Glad to know i'm not doign completely the wrong thing :D

Feb 19 '07 #3
<da****@gcoders.comwrote:
I am looking to reduce my code size because I have many very simliar
functions e.g.:

private uint GenerateAcquisitionID(AcquisitionType[] c)
{
uint max = 0;
foreach (AcquisitionType a in c)
{
uint n = Convert.ToUInt32(a.acquisitionID);
if (n max) max = n;
}
return max + 1;
}

private uint GenerateContentID(ContentType[] c)
{
uint max = 0;
foreach (ContentType a in c)
{
uint n = Convert.ToUInt32(a.contentID);
if (n max) max = n;
}
return max + 1;
}
What you could do is use an anonymous delegate for the conversion,
keeping the rest of the logic in one place. Here's a short but complete
example:

using System;

class Test
{
static void Main()
{
string[] strings = {"Hello", "Jon", "123456"};

Console.WriteLine (MaxPlusOne
(strings, delegate (string x) { return (uint)x.Length; } ));
}

private static uint MaxPlusOne<T(T[] items,
Converter<T,uintconverter)
{
uint max = 0;
foreach (T item in items)
{
uint n = converter(item);
if (n max) max = n;
}
return max + 1;
}
}

--
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
Feb 19 '07 #4
wow, i would never have thought of doign it this way. I'm just going
to read up on the converter object you suggested. Your approach seems
elegant and efficient :D.
Finally I have found a reason to use templates in my code :D
thanks

Feb 19 '07 #5
sorry, just one last thing, so is this how i would implement my
function:

private uint GenerateAcquisitionID(AcquisitionType[] c)
{
return MaxPlusOne (c, delegate(AcquisitionType x) {return
(string)x.acquisitionID;});
}

because I get this error:
Argument '2': cannot convert from 'anonymous method' to
'System.Converter<AcquisitionType,uint>'

am i making a silly mistake?

Feb 19 '07 #6
<da****@gcoders.comwrote:
sorry, just one last thing, so is this how i would implement my
function:

private uint GenerateAcquisitionID(AcquisitionType[] c)
{
return MaxPlusOne (c, delegate(AcquisitionType x) {return
(string)x.acquisitionID;});
}

because I get this error:
Argument '2': cannot convert from 'anonymous method' to
'System.Converter<AcquisitionType,uint>'

am i making a silly mistake?
You need to call the conversion from String to uint:

delegate (AcquisitionType x)
{ return Convert.ToUInt32(x.acquisitionID); }

If *all* your properties are going to return strings, you could use a
Converter<T,stringinstead, and call Convert.ToUInt32 in the
MaxPlusOne method.

--
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
Feb 19 '07 #7

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

Similar topics

1
by: Bill Sneddon | last post by:
I am using an XML file produced by doing a save-as in Excel. The file has content that looks like one of these three examples lines: <Cell ss:StyleID="s24"><ss:Data ss:Type="String"...
1
by: Neil Zanella | last post by:
Hello, The MSDN documentation does not seem to mention whether the Replace string method returns a new string or modifies the existing string in place and then returns it. I would like to know...
2
by: WRH | last post by:
Hello Is there a C# string method or short algorithm equivalent to CString SpanExcluding?
10
by: CodeRazor | last post by:
Is there a string method that allows gives you the number of times a substring appears in your string. Looping through my string performing IndexOf("substring",startPos), seems like overkill. ...
1
by: Erik Veldkamp | last post by:
Hi there, When I use javascript I can call the alert(string) method, to show an alert window. How can I do the same when I use C# script? Thanks for your help
19
by: John | last post by:
Hi How can I convert a string to long value? Most objects have a toString method but strings don't have a toLong method. Thanks Regards
6
by: LCD | last post by:
This a rather simple question for all you studs out there! Please help me with this. I have a string = "Please help me", and I want to convert this into it's hex equivalent. How do I do it, I...
4
by: sh | last post by:
The IndexOf string method returns the position of the first occurrence of a character in a string. Is there a method to find the x occurrence of a character, for example, the 28th occurrence of a...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, what is the use-case of parameter "start" in string's "endswith" method? Consider the following minimal example: a = "testing" suffix="ing" a.endswith(suffix, 2) Significance of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.