473,320 Members | 2,088 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.

Generic way of writing this enum code?

Hi
The methods below are identical except they take as arguments
different enum types.
There must be a better way of writing this?

ta
public string ConcatValues(params EnumType1 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType1 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

public string ConcatValues(params EnumType2 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType2 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

etc

Oct 29 '07 #1
4 1832
You can use generics to do this:

public string ConcatValues<T>(params T[] types)
{
// Check to make sure that T is an enumeration.
if (!typeof(T).IsEnum)
{
// Throw an exception.
throw new InvalidOperationException("The type parameter T must be an
enumeration type.");
}

// Create the string builder.
StringBuilder sb = new StringBuilder();

foreach (T type in types)
{
// Append.
sb.Append((int) type);
sb.Append(",");
}

// Remove the comma, if necessary.
if (sb.Length 0)
{
// Remove the comma.
sb.Remove(sb.Length - 1, 1);
}

// Return the string.
return sb.ToString();
}

Note that you have to check to make sure that the type T is an
enumeration at runtime, since the compiler will not allow you to create a
constraint against System.Enum (which all enumerations derive from).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<co**********@googlemail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
Hi
The methods below are identical except they take as arguments
different enum types.
There must be a better way of writing this?

ta
public string ConcatValues(params EnumType1 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType1 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

public string ConcatValues(params EnumType2 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType2 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

etc

Oct 29 '07 #2
Hey,

Just add a GenericType and change your EnumTypes1[] to the generic type and
access them that way -

<code>
public string ConcatValues<GenericType>(params GenericType[] types)
{

}
</code>

You might also be interested in:
Generic Parameters :
http://msdn2.microsoft.com/en-us/lib...x2(VS.80).aspx
Generics : http://msdn2.microsoft.com/en-us/lib...7t(VS.80).aspx

HTH
Brendon

<co**********@googlemail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
Hi
The methods below are identical except they take as arguments
different enum types.
There must be a better way of writing this?

ta
public string ConcatValues(params EnumType1 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType1 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

public string ConcatValues(params EnumType2 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType2 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

etc
Oct 29 '07 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ek**************@TK2MSFTNGP04.phx.gbl...
You can use generics to do this:

public string ConcatValues<T>(params T[] types)
{
// Check to make sure that T is an enumeration.
if (!typeof(T).IsEnum)
{
// Throw an exception.
throw new InvalidOperationException("The type parameter T must be
an enumeration type.");
}

// Create the string builder.
StringBuilder sb = new StringBuilder();

foreach (T type in types)
{
// Append.
sb.Append((int) type);
This line won't compile, because there's no constraint making T convertible
to an integer. Add a constraint "where T : IConvertible" and use
"type.ToInt32(null)".
sb.Append(",");
}

// Remove the comma, if necessary.
if (sb.Length 0)
{
// Remove the comma.
sb.Remove(sb.Length - 1, 1);
}

// Return the string.
return sb.ToString();
}

Note that you have to check to make sure that the type T is an
enumeration at runtime, since the compiler will not allow you to create a
constraint against System.Enum (which all enumerations derive from).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<co**********@googlemail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>Hi
The methods below are identical except they take as arguments
different enum types.
There must be a better way of writing this?

ta
public string ConcatValues(params EnumType1 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType1 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

public string ConcatValues(params EnumType2 [] types)
{
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (EnumType2 type in types)
{
if (first)
{
first = false;
}
else
{
sb.Append(",");
}
sb.Append((int)type);
}

return sb.ToString();
}

etc


Oct 29 '07 #4
Yep, thanks for all the replies. I've put the code in and its working
happily

Oct 31 '07 #5

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

Similar topics

3
by: Claire | last post by:
I have a large record with many enumerated fields. The record is stored in a file and the fields have to be extracted. I validate the data as it's read, but there's so many tests similar to the...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
0
by: Zoran Stipanicev | last post by:
Hi! The question is product of laziness. I want to write generic operators for matrix computation which would adapt to different types of matrices i.e. when adding square matrix and lower...
2
by: Stipanicev | last post by:
I want to write generic operators for matrix computation which would adapt to different types (shapes) of matrices i.e. when adding square matrix and lower triangle matrix it would add only...
3
by: Frederick Gotham | last post by:
For objects, we have "void*" as the generic pointer type. For instance: enum ParamType { Int, Double }; void Func(void *const p,ParamType const pt) { switch (pt) { case Int: *(int*)p = 42;...
1
by: shapper | last post by:
Hello, I have an Enum and a Generic.List(Of Enum) 1 Public Enum Mode 2 Count 3 Day 4 Month 5 End Enum
2
by: shapper | last post by:
Hello, I have an Enum and a Generic.List(Of Enum) 1 Public Enum Mode 2 Count 3 Day 4 Month 5 End Enum
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
1
by: dgraper | last post by:
Friends: I'm an ex-VB programmer with a new job in an all C# shop. No problems the past few months thinking/converting between the two, but I just ran into a problem translating some working VB...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: 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)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.