PC Review


Reply
Thread Tools Rate Thread

converting string to method, templates question

 
 
damian@gcoders.com
Guest
Posts: n/a
 
      19th Feb 2007
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;
}

 
Reply With Quote
 
 
 
 
Laura T.
Guest
Posts: n/a
 
      19th Feb 2007
You *could* use reflection for
> uint n = Convert.ToUInt32(a.field); // how to do


uint n= Convert.ToUInt32(a.GetType().GetProperty(field).GetValue(a, null));

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

<(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> 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;
> }
>



 
Reply With Quote
 
damian@gcoders.com
Guest
Posts: n/a
 
      19th Feb 2007
thanks. Glad to know i'm not doign completely the wrong thing

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Feb 2007
<(E-Mail Removed)> wrote:
> 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,uint> converter)
{
uint max = 0;
foreach (T item in items)
{
uint n = converter(item);
if (n > max) max = n;
}
return max + 1;
}
}

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
damian@gcoders.com
Guest
Posts: n/a
 
      19th Feb 2007
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 .
Finally I have found a reason to use templates in my code
thanks



 
Reply With Quote
 
damian@gcoders.com
Guest
Posts: n/a
 
      19th Feb 2007
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?

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Feb 2007
<(E-Mail Removed)> wrote:
> 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,string> instead, and call Convert.ToUInt32 in the
MaxPlusOne method.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a string to an int - Easy Question robert.b.walden@gmail.com Microsoft C# .NET 4 6th Sep 2006 02:29 PM
Question on converting char* into System.String Yan Vinogradov Microsoft C# .NET 4 28th Oct 2004 02:28 PM
Simple String.Format method question Burak Microsoft VB .NET 5 16th Jun 2004 05:35 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jay B. Harlow [MVP - Outlook] Microsoft C# .NET 7 1st Aug 2003 06:03 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Mikael Jansson Microsoft C# .NET 0 31st Jul 2003 08:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 AM.