C's "typedef" into C# equivalent -- please help

A

almurph

Hi,

I want to put the following line of code:

using ItemNo = System.Int32;


into a static "Utilities" class so that it is available for all other
classes in my program. For example:

static class Utilities
{
using ItemNo = System.Int32;

}


But I get the following error message: "Invalid token 'using' in
class, struct, or interface member declaration"
Essentially what I am trying to do is reverse engineer the C language
keyword "typedef" into C#. I need this to be available in several
classes and don't wnat to have to declare it in each class at the
namespace scope.

Would really appreciate any "nice" ways of achieving this. Any
comments/suggestions and code-samples most appreciated.

Thanks,
Al.
 
D

David Anton

Your solution is the closest you can get to a typedef in C#, but as you've
found, you must include the 'using' alias in every file that you need it.
There's no way to 'return' a namespace from a common piece of code.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++
 
G

Göran Andersson

Hi,

I want to put the following line of code:

using ItemNo = System.Int32;


into a static "Utilities" class so that it is available for all other
classes in my program. For example:

static class Utilities
{
using ItemNo = System.Int32;

}


But I get the following error message: "Invalid token 'using' in
class, struct, or interface member declaration"
Essentially what I am trying to do is reverse engineer the C language
keyword "typedef" into C#. I need this to be available in several
classes and don't wnat to have to declare it in each class at the
namespace scope.

Would really appreciate any "nice" ways of achieving this. Any
comments/suggestions and code-samples most appreciated.

Thanks,
Al.

There is no simple way of hiding a data type like that. Things like
defines and typedefs was intentionally left out of the C# language
because they tend to be used to hide what's actually going on in the code.

You can either include the using directive in each file where you need
it, or you can create a real data type that encapsulates an int.
 
A

almurph

There is no simple way of hiding a data type like that. Things like
defines and typedefs was intentionally left out of the C# language
because they tend to be used to hide what's actually going on in the code..

You can either include the using directive in each file where you need
it, or you can create a real data type that encapsulates an int.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Hi Goran,

I like the "or you can create a real data type that encapsulates an
int." option. As I'm new to C#.NET can you give me an example please?

Cheers,
Al.
 
G

Göran Andersson

I like the "or you can create a real data type that encapsulates an
int." option. As I'm new to C#.NET can you give me an example please?

Here's an implementation of a struct encapsulating an int, that supports
comparing and implicit conversion to and from int. As it implements the
GetHashCode() and Equals<ItemNo>() it can for example be used as a key
in a Dictionary. Depending on how you want to use it, you may want to
implement other interfaces and override other methods, like for example
the ToString method.

public struct ItemNo : IComparable<ItemNo>, IEquatable<ItemNo> {

private int _value;

public ItemNo(int value) {
_value = value;
}

public static implicit operator int(ItemNo no) {
return no._value;
}

public static implicit operator ItemNo(int value) {
return new ItemNo(value);
}

public override int GetHashCode() {
return _value.GetHashCode();
}

#region IComparable<ItemNo> Members

public int CompareTo(ItemNo other) {
return _value.CompareTo(other._value);
}

#endregion

#region IEquatable<ItemNo> Members

public bool Equals(ItemNo other) {
return _value == other._value;
}

#endregion
}

Usage exmample:

ItemNo no = 42;
 

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

Similar Threads

Typedef 1
C++ v C# 17
C# Equivalent to C/C++ static variable in a function? 5
Typedef in Managed C++ 1
Compilers, parsing, etc... 3
Implementing an IDispatch Interfac in C# DLL 0
C# confusion 4
C header to c# 2

Top