D
Dave
I've spent much of today researching how best to get around C# not
supporting typedefs. I relied on them quite a lot in C++ and finally
broke down today testing out how best to emulate them. (I toyed with
using enum types within ClientRetType<> but this fell apart as enums
are not types, was the caller syntax was bad.)
I'm aware that the 'using' primitive only has file scope, and #include
directives are not allowed in C#. I require the new typedef emulator
to be of application scope as it was in C++.
This example shows how I only want to allow passing typesafe values,
and with predetermined values. This code is crude, and calling with
parms of new ClientRetType<int>(1) is nondescript and ugly.
I'm wondering if there is a more elegant way of doing what I'm
after??
public class ClientRetType<T>
{
public const short RequestAttempted = 1;
public const short RequestNotAttempted = 2;
public const short NotApplicable = 3;
public ClientRetType(T ElmtArg)
{
int retVal;
if (Int32.TryParse(Convert.ToString(ElmtArg), out retVal))
switch ((int)Convert.ToInt32(ElmtArg))
{
case RequestAttempted:
case RequestNotAttempted:
case NotApplicable:
Element = ElmtArg;
break;
default:
Element = default(T);
// error handling...
break;
}
else
{
// error handling...
}
}
public T Element;
}
supporting functions:
public static void MyIntFunction(ClientRetType<int> val)
{
Console.WriteLine("Value: [{0}]", val.Element);
}
public static void MyStringFunction(ClientRetType<string> val)
{
Console.WriteLine("Value: [{0}]", val.Element);
}
with callers:
MyIntFunction(new ClientRetType<int>(1));
MyIntFunction(new ClientRetType<int>(4));
MyStringFunction(new ClientRetType<string>("abc"));
output:
Value: [1]
Value: [0]
Value: []
thanks, dave
supporting typedefs. I relied on them quite a lot in C++ and finally
broke down today testing out how best to emulate them. (I toyed with
using enum types within ClientRetType<> but this fell apart as enums
are not types, was the caller syntax was bad.)
I'm aware that the 'using' primitive only has file scope, and #include
directives are not allowed in C#. I require the new typedef emulator
to be of application scope as it was in C++.
This example shows how I only want to allow passing typesafe values,
and with predetermined values. This code is crude, and calling with
parms of new ClientRetType<int>(1) is nondescript and ugly.
I'm wondering if there is a more elegant way of doing what I'm
after??
public class ClientRetType<T>
{
public const short RequestAttempted = 1;
public const short RequestNotAttempted = 2;
public const short NotApplicable = 3;
public ClientRetType(T ElmtArg)
{
int retVal;
if (Int32.TryParse(Convert.ToString(ElmtArg), out retVal))
switch ((int)Convert.ToInt32(ElmtArg))
{
case RequestAttempted:
case RequestNotAttempted:
case NotApplicable:
Element = ElmtArg;
break;
default:
Element = default(T);
// error handling...
break;
}
else
{
// error handling...
}
}
public T Element;
}
supporting functions:
public static void MyIntFunction(ClientRetType<int> val)
{
Console.WriteLine("Value: [{0}]", val.Element);
}
public static void MyStringFunction(ClientRetType<string> val)
{
Console.WriteLine("Value: [{0}]", val.Element);
}
with callers:
MyIntFunction(new ClientRetType<int>(1));
MyIntFunction(new ClientRetType<int>(4));
MyStringFunction(new ClientRetType<string>("abc"));
output:
Value: [1]
Value: [0]
Value: []
thanks, dave