473,503 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

from C++ to C#

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
Nov 16 '05 #1
6 1143
Sorry, that should have been...

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

Newsgroupie

Nov 16 '05 #2

"newsgroupie" wrote...
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

Nov 16 '05 #3
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,
};
*/

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4

"Bjorn Abelli" wrote...

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

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
Nov 16 '05 #5
Thanks guys!

Those give me something to think over

Newsgroupie
Nov 16 '05 #6
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
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2748
by: |-|erc | last post by:
Hi! Small challenge for you. The index.php uses this file and calls layout(). Take a look at www.chatty.net this file draws the chat login box on the right. I traced the CHAT button it submits...
16
2063
by: Fuzzyman | last post by:
Hello, To create a classic (old style) class, I write : class foo: pass To do the equivalent as a new style class, I write : class foo(object):
2
2977
by: nitin | last post by:
g++ -c $INCLUDES ActualPEResult.cpp In file included from /usr/include/c++/3.2.2/backward/iostream.h:31, from /home/pradeepks/Linux_Porting/dcpfrontier/dcpdev/dcp_components/trap...
4
19966
by: susmita_ganguly | last post by:
Hi I am trying to upgrade from oracle 8i to oracle 9i on the same server ..I don't know much abt migration . Can anyone help me out. Thanks. Susmita
2
3190
by: cs168 | last post by:
Hi I am new in ASP programming so I do use the very basic and simple way to do all my stuff. Now I do really got stuck at how can I loop thru the calculation for all my selection.. My full code is as...
7
8821
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
7
11600
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
1
1915
by: RSH | last post by:
Im trying to create a stored procedure of the following code. I want to set it so that I have to send the first 4 variables (@DB, @BackUpFile,@TestDB,@RestoreFile). I am having trouble when i try...
20
2469
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
6
17139
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.