Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New type of data.

Status
Not open for further replies.

ern297

Programmer
Nov 28, 2002
13
ES
How can I define a new type of data? Sorry if the ask is a bit silly but I came of C++ and I don't know which is the equivalent of 'typedef'.
 

Try with TypeBuilder.CreateType() ---you can read more informations about this method in MSDN.

Hope this help,

Mary
 
I think that this is the correct way, but I don't understand very well the MSDN examples. I only want to do something as

typedef MY_INT int;

Could you help me with any example?

Thanks,

Ernesto.
 
Ernesto -

There's no preprocessor in .NET. Well, that's not entirely true ... you can declare precompiler variables (DEBUG, TRACE, etc. are some of the ones the compiler already knows about).

But all the stuff you used to do in C++ where you're do a typedef to create your own "type" (sortof) doesn't exist - you'll have to create either a struct (for value types) or a class (for reference types). This is because C# is more of a "pure" implementation of an OO language than C++ is (no legacy C code to worry about).

I've seen typdefs used to provide a cross-platform way to hide the underlying datatypes. With the .NET type system that isn't required anymore ... all the .NET languages know about the same fundamental types. If you're calling out to a module written in another language (like MicroFocus COBOL or something), the types provided are rich enough that you shouldn't have any problems matching up with what the other module expects.

Chip H.
 
Ok,

I understand all that theory, but I have the next in C++

/******/
typedef struct
{
int var1;
byte var2[8];
char *pVar3;
}
myStruct;

void myFunction(myStruct var1, int var 2, etc...)
/******/

How can I define this structure in C#?. I use
[DllImport(...)] for import the function, but I have an error when I pass the parameters (I don't define correctly the structure).

Can you help me?

Thanks.
 

You can look for "struct" in MSDN, C#.
And a short sample:

public struct Point
{
public int x, y;

public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}

Hope this help,
Mary
 
Code:
typedef struct
{
  int var1;
  byte var2[8];
  char *pVar3;
}
myStruct;
becomes:
Code:
public struct myStruct {
  int16 var1;
  byte(8) var2;
  string Var3;
}
myStruct is then a valid type to use to pass to functions, whatever.

Note that I used int16 for the C/C++ int type. You'll have to look at your old compiler to see what width an int is before deciding what to use in C# (int16, int32, int64)

I've seen int defined as 32-bit, and long also defined as 32-bits wide, which is perfectly legal under ANSI C, since byte <= int <= long

Chip H.
 
The sentence

byte(8) var2;

isn't correct in C#. Do you know how can I get this array with dimension 8? (if it's possible, without use the constructor).

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top