Converting Delphi code

M

Mel Weaver

Hello,
I looking for different ideas on how to convert this delphi code to c#,
mostly the constant array.

procedure PhysDmgSymbol(sym : string; yr: integer);
type
d = record
s : String;
r : array[1..4] of double;
end;

const a1 : array[0..19]of d = ((s:'01';r:(1.55,1.18,1.55,1.18)),
(s:'02';r:(1.55,1.18,2.15,1.50)),
(s:'03';r:(1.55,1.18,2.85,1.60)),
(s:'04';r:(1.55,1.18,2.95,1.70)),
(s:'05';r:(1.55,1.18,3.40,1.80)),
(s:'06';r:(1.55,1.18,3.60,1.95)),
(s:'07';r:(2.15,1.50,3.90,2.00)),
(s:'08';r:(2.85,1.60,4.20,2.10)),
(s:'10';r:(3.51,1.80,4.90,2.20)),
(s:'11';r:(3.90,2.00,5.20,2.30)),
(s:'12';r:(4.90,2.20,5.80,2.40)),
(s:'13';r:(5.80,2.40,6.00,2.50)),
(s:'14';r:(6.60,2.80,6.60,2.80)),
(s:'15';r:(7.60,3.15,7.00,3.00)),
(s:'16';r:(9.00,3.40,7.60,3.15)),
(s:'17';r:(10.30,4.40,8.00,3.30)),
(s:'18';r:(0.00,0.00,9.00,3.40)),
(s:'19';r:(0.00,0.00,9.70,3.60)),
(s:'20';r:(0.00,0.00,10.30,4.60)),
(s:'21';r:(0.00,0.00,10.90,4.60)));
var
i : integer;
begin
for i := low(a1) to high(a1) do
begin
if sym = a1.s then
begin
nCompFactor := ifthen(yr > 1989,a1.r[3],a1.r[1]);
nCollFactor := ifthen(yr > 1989,a1.r[4],a1.r[2]);
exit;
end;
end;

end;


Thanks
Mel
 
J

Jani Järvinen [MVP]

Mel,
I looking for different ideas on how to convert this delphi code to c#,
mostly the constant array.

const a1 : array[0..19]of d = ((s:'01';r:(1.55,1.18,1.55,1.18)),

(s:'02';r:(1.55,1.18,2.15,1.50)),
...

What you need here is a C# struct to match the Delphi record, and a public
constructor for the struct, like this:

------------------
public struct d
{
public string s;
public double[] r;

public d(string s, double[] r)
{
this.s = s;
this.r = r;
}
}
------------------

Next, you can declare a readonly field as an array of "d" inside your C#
class. The trick is to remember to call the constructor of the struct "d",
passing in the parameters. Like this:

------------------
public class TestCSharp
{
private readonly d[] a1 = {
new d("01", new double[] {1.55, 1.18, 1.55, 1.18}),
new d("02", new double[] {1.55, 1.18, 2.15, 1.50})
// etc...
};
}
------------------

That should do it. Two general suggestions for the conversion:

- use better and longer variable names, "d" or "a1" are not very describing
- get rid of "macro like" function like "ifthen", convert these to proper if
statements instead

Hope this helps!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
M

Mel

Thank You

Mel

Jani Järvinen said:
Mel,
I looking for different ideas on how to convert this delphi code to c#,
mostly the constant array.

const a1 : array[0..19]of d = ((s:'01';r:(1.55,1.18,1.55,1.18)),

(s:'02';r:(1.55,1.18,2.15,1.50)),
...

What you need here is a C# struct to match the Delphi record, and a public
constructor for the struct, like this:

------------------
public struct d
{
public string s;
public double[] r;

public d(string s, double[] r)
{
this.s = s;
this.r = r;
}
}
------------------

Next, you can declare a readonly field as an array of "d" inside your C#
class. The trick is to remember to call the constructor of the struct "d",
passing in the parameters. Like this:

------------------
public class TestCSharp
{
private readonly d[] a1 = {
new d("01", new double[] {1.55, 1.18, 1.55, 1.18}),
new d("02", new double[] {1.55, 1.18, 2.15, 1.50})
// etc...
};
}
------------------

That should do it. Two general suggestions for the conversion:

- use better and longer variable names, "d" or "a1" are not very
describing
- get rid of "macro like" function like "ifthen", convert these to proper
if statements instead

Hope this helps!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top