How do I accomplish typesafe value checking - without typedefs

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
 
L

Lasse Vågsæther Karlsen

Dave said:
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 am interested in knowing, after seing your example, why you can't use
enums.
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++.

Then you need to use an existing construct, or build a preprocessor.
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??

What you are doing looks like an enum. What's wrong with an enum?

I see that what you mean by "typesafe" really means "I'm going to fail
silently for values I don't know". In my book that doesn't spell safe.
 
D

Dave

I am interested in knowing, after seing your example, why you can't use
enums.
enums are not types, so i cannot pass as enum into the generic class.
I.e can't do ClientRetType<enum>. bummer as the internal code to
ClientRetType() class is cleaner handling enums rather then ints.

I suspect there is a better way to emulate the old C++ behavior of:
"typedef enum {RED, BLUE, GREEN} Color"; than what I'm doing though!
Then you need to use an existing construct, or build a preprocessor.





What you are doing looks like an enum. What's wrong with an enum?

answered above.
I see that what you mean by "typesafe" really means "I'm going to fail
silently for values I don't know". In my book that doesn't spell safe.

yes i know... i only scratched a quick example together. I'm
basically trying to pass types around the way I was able to in C++.
I.e. I miss being able to do: typedef enum {RED, BLUE, GREEN} Color;
in code, then using the type Color all over the place!

thanks, dave
 
J

Jon Skeet [C# MVP]

Dave said:
enums are not types, so i cannot pass as enum into the generic class.

Enums certainly are types. For instance:

using System.Collections.Generic;

enum Foo
{
Bar, Baz
}

class Test
{
static void Main()
{
List<Foo> list = new List<Foo>();

list.Add(Foo.Bar);
}
}

I don't know whether that helps in your particular situation, but I
thought it would be worth clearing up :)
 
D

Dave

Enums certainly are types. For instance:

using System.Collections.Generic;

enum Foo
{
    Bar, Baz

}

class Test
{
    static void Main()
    {
        List<Foo> list = new List<Foo>();

        list.Add(Foo.Bar);
    }

}

I don't know whether that helps in your particular situation, but I
thought it would be worth clearing up :)

yeah that helps for typedef enum scenario nicely. thanks for the
syntax.
 

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

Top