converting string to method, templates question

  • Thread starter Thread starter damian
  • Start date Start date
D

damian

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;
}
 
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.

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;
}
 
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;
}
}
 
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
 
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?
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top