开发者

Migrate C to C# problem!

I try to convert C lib to C# and got stuck in this code:

typedef struct _Symbol
{
    unsigned char value;
   开发者_开发技巧 unsigned char count;
    unsigned char under;
    unsigned char left, right;
} Symbol;

// [...]

void function(Symbol * symbol) {

...

if (! symbol -> right) break; // So, right is equivalent to bool in C#?

symbol += symbol -> right; // I have no idea what this code will do.

...

}

Please help me out.


In C# there is no -> operator. To reference a struct member you use . operator. Also, you can't use any expression with if sentences. You must use a proper boolean expression:

if (symbol.right == 0)
    break;

Also, I wouldn't try to convert line-by-line a C program into a C# program. They are based on different paradigms, so you would probably need to learn a little bit of object orientation and how C# manages memory before attempting to translate code.


if (! symbol -> right) break; says if right is zero then break

symbol += symbol -> right; this is performing pointer arithmetic, you'll need to understand why so you can recode it into C#. (assuming symbol is of type Symbol*)


In C++ any object that has a comparison operator, and can be compared against '0' can be used in an expression in a conditional. If the expression equals 0 then the expression will evaluate to false, otherwise the expression will evaluate to true.

So: if(!symbol->right) will be true if symbol->right is 0.

The following

symbol += symbol->right 

is incrementing the symbol pointer by the value stored in symbol->right.


Try this:

struct Symbol
{
Char value;
int  count; // if your counting, use an integer, not a character
int  under; // not sure that is supposed to represent
int left; 
int right;
}

I'm not sure what you want or need the right and left data members to do. It looks like you wanted to at least increment them, so I left them as integers. In which case you can't just test it using if (symbol.left), but rather you have to use if (symbol.left > 0) etc...

Also C# doesn't use the -> operator, but the . operator exclusively.

In .NET all types cannot just be cast into other types willy nilly. So you can't convert a character (Char in C#) into a bool, or an int.

If you want to keep using C, and still take advantage of .NET you can use the managed compiler /clr and compile all your C Code. But you will have to learn a few extra language keywords, and program a little different.

In general if you are really new to C# and .NET I highly suggest you get two books. The first book is on .NET by Jeffrey Richter (CLR via C#). It's published by Microsoft Press, and is excellent. The other book can or should be C# specific. Charles Petzold has a good introductory book to C# called Programming in the key of C#, that is pretty basic for the language.


left and right are defined in the struct:

unsigned char left, right;

It is not the wrong, right "right" but left,right right :)

And its equivalent in C# is not bool.


The line

if (!symbol->right) break

checks if the character held in the variable right is null (equals '\0'). So, if the character is equal to 0, it will break, otherwise it won't.


if (! symbol -> right) break;

I believe this is boolean NOT operation on char in int context. Eg: if (int)right = 0, this is true. Otherwise, it's false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜