tags:

views:

118

answers:

4

Im trying to convert some Delphi code to c# and I've come across a problem...

In Delphi I've decalared a new type

Type TData = Array of Extended

where I can access the result of functions returning this type with statements like

Function TMyObject.ReadData:TData;
begin
...
end;

Data := MyObject.ReadData;
Result = Data[7] + Data[12]

now if I had intially declared this as Array of Single then I could change this one line to change the precision. (which I may have to do shortly to lower it to Double so that any results exactly match the c# version.

so the question is can I do something similar in c#? something like

class Data : Double[]

although not exactly like this since it doesn't compile, or would I do

class DataEntry : Double
...
public DataEntry[] Read
{
...
}
+2  A: 

You can wrap an array of Double in a class and provide index properties for it.

Eric
+1 Composition not inheritance.
rtalbot
+4  A: 

The closest you can get in C# is

using DataEntry = System.Double;

Put this at the top of each file.

SLaks
`double` is actually shorter and clearer than `DataEntry` so there's no point in doing this, but +1 for providing the type alias syntax.
Darin Dimitrov
@Darin, it has the benefit of single-point-of-definition.
Henk Holterman
Presumably the point is that DataEntry is an alias and double isn't. If you use double in lots of places then you have to change it in lots. If you use DataEntry in lots of places then you only need to change the above line. Though I've not done this much so may be wrong...
Chris
@Henk, I don't understand *single-point-of-definition*. `double` has also a single point of definition which is the `System` namespace.
Darin Dimitrov
@Darin: Imagine there are also (lots of) functions like `DataEntry Add(DataEntry a, DataEntry b)`
Henk Holterman
+1  A: 

It sounds like you want something similar to a C/C++ typedef, which unfortunately I don't think is supported in C#. You can make local alias within a single source file (see below), but the alias won't be visible anywhere else.

To create a local alias, add this at the top of your source file:

using Data = System.Double;
Charlie
+1  A: 

No, C# has no direct way of doing that.

You could however wrap that array (and all or most code that uses it) in a generic class:

class MyClass<T>
{
   private T[] Mydata;

}
Henk Holterman