Generic way of writing this enum code? - CSharp
This is a discussion on Generic way of writing this enum code? - CSharp ; 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;
...
-
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
-
Re: Generic way of writing this enum code?
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]
- mvp@spam.guard.caspershouse.com
<codefragment@googlemail.com> wrote in message
news:1193669815.596690.161790@22g2000hsm.googlegroups.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
>
-
Re: Generic way of writing this enum code?
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
<codefragment@googlemail.com> wrote in message
news:1193669815.596690.161790@22g2000hsm.googlegroups.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
>
-
Re: Generic way of writing this enum code?
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:ekCOb3jGIHA.5980@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]
> - mvp@spam.guard.caspershouse.com
>
>
> <codefragment@googlemail.com> wrote in message
> news:1193669815.596690.161790@22g2000hsm.googlegroups.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
>>
>
>
-
Re: Generic way of writing this enum code?
Yep, thanks for all the replies. I've put the code in and its working
happily
Similar Threads
-
By Application Development in forum Java
Replies: 0
Last Post: 10-10-2007, 12:28 PM
-
By Application Development in forum Java
Replies: 2
Last Post: 09-03-2007, 01:33 PM
-
By Application Development in forum Java
Replies: 6
Last Post: 09-02-2007, 03:49 PM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 03-02-2006, 07:36 PM
-
By Application Development in forum Object
Replies: 1
Last Post: 01-06-2005, 12:35 PM