Solved

Porting from C to C#

Posted on 2011-09-18
9
356 Views
Last Modified: 2012-05-12
Hi,

I started porting some code from C to C# and have become stuck on a couple of things.

The examples are from a header file

No idea how to convert the following one over or what it could be doing.
        /* macros */
        #define gaiaGetPoint(xy,v,x,y)	\
				        {*x = xy[(v) * 2]; \
				         *y = xy[(v) * 2 + 1];}

        #define gaiaSetPoint(xy,v,x,y)	\
				        {xy[(v) * 2] = x; \
				         xy[(v) * 2 + 1] = y;}

Open in new window


And

I believe a struct is supposed to be converted into a public class with each of its properties/variables set to public as well, is this correct?
        typedef struct gaiaPointStruct
        {
            /* an OpenGis POINT */
	        double X;		/* X,Y coordinates */
	        double Y;
	        double Z;		/* Z coordinate */
	        double M;		/* M measure */
	        int DimensionModel;	/* (x,y), (x,y,z), (x,y,m) or (x,y,z,m) */
	        struct gaiaPointStruct *Next;	/* for double-linked list */
	        struct gaiaPointStruct *Prev;	/* for double-linked list */
        } gaiaPoint;[i] [b]//What is this piece[/b][/i]
        typedef gaiaPoint *gaiaPointPtr;[i] [b]//What is this piece[/b][/i]

Open in new window


here is my attempt on the last one: (although I have no idea on the last line or what the "gaiaPoint" behind the last curly bracket is)
    public class gaiaPointStruct
    {
        /* an OpenGis POINT */
	    public double X;		/* X,Y coordinates */
	    public double Y;
	    public double Z;		/* Z coordinate */
	    public double M;		/* M measure */
	    public int DimensionModel;	/* (x,y), (x,y,z), (x,y,m) or (x,y,z,m) */
	    public gaiaPointStruct Next;	/* for double-linked list */
	    public gaiaPointStruct Prev;	/* for double-linked list */
    } gaiaPoint;
    typedef gaiaPoint *gaiaPointPtr;

Open in new window


If someone could give me some help I would most appreciate it!
Regards Hayden
0
Comment
Question by:AssetFX
  • 6
  • 3
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36556418

C# does not support macros - so it is a problem
here are soime suggestions:

http://stackoverflow.com/questions/6228045/convert-c-define-macro-to-c

http://stackoverflow.com/questions/1656098/c-to-c-conversion-of-define-macros

http://www.gamedev.net/topic/255134-converting-c-macros-to-c/


struct  should be translated as a class - seems reasonable
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36556432


This is about the eqivalent of typedef:

http://stackoverflow.com/questions/161477/equivalent-of-typedef-in-c


and this a useful place fro poting C to C#:


http://msdn.microsoft.com/en-us/library/yyaad03b%28v=vs.90%29.aspx

The typedef keyword. In C++, typedef is used to create shorter or more convenient names for types that have already been declared. In C#, the using directive provides this capability.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36556435
After the closing brace - this is I guess the actual name of the instance of variable of this type

So equivanet should be declaration of the variable of the type of this class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36556451

And these are some recommendations when to use structs which are still supported in C#, though not so  common as in C:

http://www.codeproject.com/KB/cs/structs_in_csharp.aspx


When to use structs

 Here's when structs are better:

    You want your type to look and feel like a primitive type.
    You create a lot of instances, use them briefly, and then drop them. For e.g., within a loop.
    The instances you create are not passed around a lot.
    You don't want to derive from other types or let others derive from your type.
    You want others to operate on a copy of your data (basically pass by value semantics).

Here's when not to use structs:

    The size of the struct (the sum of the sizes of its members) gets large. The reason is that beyond a particular size, the overhead involved in passing it around gets prohibitive. Microsoft recommends that the size of a struct should ideally be below 16 bytes, but it really is up to you. In case your struct has reference types as members, make sure you don't include the size of instances of reference types, just the size of the references.
    You create instances, put them in a collection, iterate and modify elements in the collection. This will result in a lot of boxing/unboxing as FCL Collections operate on System.Object. Every addition will involve a boxing operation, and every modification will involve an unboxing followed by a boxing operation.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 47

Expert Comment

by:for_yan
ID: 36557430



Actually gaiaPoint after the braces is the name of the type   struct gaiaPointStruct,
otherwise in C you in ever declaration you'll need to type "struct gaiaPointStruct"
(see detailed explanation here
http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c
in the accepted solution:



-------------------------
      

In C++, there is no difference. It's a holdover from C, in which it made a difference.

In C, there are two different namespaces of types: a namespace of struct/union/enum tag names and a namespace of typedef names. If you just said

struct Foo { ... };
Foo x;

You would get a compiler error, because Foo is only defined in the tag namespace. You'd have to declare it as

struct Foo x;

Any time you want to refer to a Foo, you'd always have to call it a struct Foo. This gets annoying fast, so you can add a typedef:

struct Foo { ... };
typedef struct Foo Foo;

Now both struct Foo (in the tag namespace) and just plain Foo (in the typedef namespace) both refer to the same thing, and you can freely declare objects of type Foo without the struct keyword.

-----------------------

).



Maybe your code in C# will look something like that; at least it compiles:
(of course you'll need to modify those places where these macros were used)

using System;

using MyApplication1;

namespace MyApplication1
{

    public class gaiaPoint
    {
        /* an OpenGis POINT */
	    public double X;		/* X,Y coordinates */
	    public double Y;
	    public double Z;		/* Z coordinate */
	    public double M;		/* M measure */
	    public int DimensionModel;	/* (x,y), (x,y,z), (x,y,m) or (x,y,z,m) */
	    public gaiaPoint Next;	/* for double-linked list */
	    public gaiaPoint Prev;	/* for double-linked list */

        public static void  gaiaSetPoint(double [] xy, int v, double x, double y)	{
				        xy[(v) * 2] = x;
				         xy[(v) * 2 + 1] = y;
                        }
         public static double gaiaGetPointX(double [] xy, int v){	
				        return (xy[(v) * 2]);
    }
          public static double gaiaGetPointY(double [] xy, int v){	
				        return (xy[(v) * 2 + 1]);
    }
		
		        
         }



    } 



    class MyProgram
    {
        static void Main(string[] args)
        {
            gaiaPoint gaiaPointPtr;

        }
    }

Open in new window

0
 

Author Comment

by:AssetFX
ID: 36559110
Hi For Yan

thanks for your help! With the C#, don't I need to tell the function that I want to bring in the xy array as a reference, to be able to bring that points value back to where it's being called from?

And to keep things the same and following the logic of the original I should be able to do the same "ref" with the x and y in gaiaSetPoint(), sending back x and y within the same function? I admit it is not as clean, but when converting and finding where these are called later could be easier.

public static void gaiaGetPoint(ref double [] xy, int v, double x, double y)
{
    xy[(v) * 2] = x;
    xy[(v) * 2 + 1] = y;
}

public static void gaiaSetPoint(double [] xy, int v, ref double x, ref double y)
{
    x = (xy[(v) * 2]);
    y = (xy[(v) * 2 + 1]);
}

Open in new window

0
 

Author Comment

by:AssetFX
ID: 36559181
Hi for_yan

just another quick question, are sure that the lines within the functions are assigned the right way? As I noticed you've swapped them around from what was in the C code.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 100 total points
ID: 36565044
I thought it makes sense that in getPoint() we retrieve the indiviual values form the array
in setPoint we take indiviual values and assign them to the array element

and that's how it was in C code - when you set the point you assign the indiviual values x and y to the
elemenst of the array. In your code above when you setPoint you assign vaues of elements of the array to  x and y
I think they should be renamed in your code

With the rest I agree, you can use ref and keep them void, and it is probably better this way
0
 

Author Closing Comment

by:AssetFX
ID: 36565576
For_Yan brilliant help thanks very much!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now