from C++ to C#

  • Thread starter Thread starter newsgroupie
  • Start date Start date
N

newsgroupie

Hi Newsgroupies!

I'm currently trying to port an old C++ application of mine into C#
(webforms). But how can I do this...

typedef struct
{
double fParam1;
double fParam2;
double fParam3;
double fParam4;
} EXAMPLE_STRUCT;

const <classname>::sm_sData =
{
0.1234,
1.2345,
2.3456,
3.4567,
};

....in C#? I appreciate that structs, typedefs and globals ain't allowed
in C#.

And how can you define global constants required throughout the
application say like the universal gravitational constant for example?


Many thanks in advance,

newsgroupie
 
Sorry, that should have been...

const EXAMPLE_STRUCT <classname>::sm_sData =
{
0.1234,
1.2345,
2.3456,
3.4567,
};

Newsgroupie
 
Off the top of my head:
Regards,
Jeff

============= UNTESTED CODE ============
using System;

namespace TestStruct1
{
// immutable structure
struct ExampleStruct
{
public readonly double fParam1;
public readonly double fParam2;
public readonly double fParam3;
public readonly double fParam4;

public ExampleStruct(double fParam1, double fParam2, double fParam3,
double fParam4)
{
this.fParam1= fParam1;
this.fParam2= fParam2;
this.fParam3= fParam3;
this.fParam4= fParam4;
}
}

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private ExampleStruct es;
public Class1(ExampleStruct es)
{
this.es= es;
}
public void Print()
{
System.Console.WriteLine(es.fParam1);
System.Console.WriteLine(es.fParam2);
System.Console.WriteLine(es.fParam3);
System.Console.WriteLine(es.fParam4);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
ExampleStruct es= new ExampleStruct(
0.1234,
1.2345,
2.3456,
3.4567);
// es.fParam1= 2; // build error as expected
Class1 c1= new Class1(es);
c1.Print();
System.Console.ReadLine();
}
}
}
/*typedef struct
{
double fParam1;
double fParam2;
double fParam3;
double fParam4;
} EXAMPLE_STRUCT;

const <classname>::sm_sData =
{
0.1234,
1.2345,
2.3456,
3.4567,
};
*/
 
...
I'm currently trying to port an old C++ application of mine into C#
(webforms). But how can I do this...

typedef struct
{
double fParam1;
double fParam2;
double fParam3;
double fParam4;
} EXAMPLE_STRUCT;

const <classname>::sm_sData =
{
0.1234,
1.2345,
2.3456,
3.4567,
};

...in C#? I appreciate that structs, typedefs
and globals ain't allowed in C#.

Well, structs exist, though they're not the same as i C++. In C# you define
types the same way throughout the system, with structs or classes.

If you by "typedefs not allowed" mean as aliases for different types, you
can use "using" instead.
And how can you define global constants required
throughout the application say like the universal
gravitational constant for example?

As for global constants...

As long as they're or "public const", they can be accessed from anywhere...

However, there's a few restrictions on constants which make it not 100 %
convertible from your C++ code, but that's maybe not a problem, if you
redefine your task into ".NET" and more object-oriented.

There are probably better solutions to this, but to give an example:
---
namespace Globals
{
public struct EXAMPLE_STRUCT
{
public double fParam1;
public double fParam2;
public double fParam3;
public double fParam4;

public EXAMPLE_STRUCT(double p1, double p2, double p3, double p4)
{
fParam1 = p1;
fParam2 = p2;
fParam3 = p3;
fParam4 = p4;
}
}

public class Structs
{
// const can only be assigned "strings" or "primitives"
// or other values reachable at compiletime

public static readonly EXAMPLE_STRUCT sm_sData =
new EXAMPLE_STRUCT ( 0.1234, 1.2345, 2.3456, 3.4567 );
}
}
---

Now you can reach your struct and its elements with:

using Globals;
...
// in some code:
double d = Globals.sm_sData.fParam1;
...


Just to give a possible example.

// Bjorn A
 
using Globals;
...
// in some code:
double d = Globals.sm_sData.fParam1;
...

Sorry, missed the name of the class here, should be:


using Globals;
...
// in some code:
double d = Globals.Structs.sm_sData.fParam1;
...

// Bjorn A
 
Epilogue

I ended up doing it this way!

Thanks again for all your help guys!

namespace ExampleSpace
{
public class Example
{
public static readonly Example sm_s1 = new Example(1.0,
2.0, 3.0, 4.0);
public static readonly Example sm_s2 = new
Example(10.0, 20.0, 30.0, 40.0);

public Example(double f1, double f2, double f3, double
f4)
{
// TODO, or not TODO, that is the question...

return;
}

// Etc., Etc., Etc...
}

// and so and so forth!
}


Newsgroupie, UK
 
Back
Top