1

I'd like to be able to set a datatype for an entire application with a single using clause. My solution contains a bunch of classes, each in its own .cs file, each of which contains the statement:

using ptype = System.Double; // or System.Decimal, etc

By declaring subsequent variables as ptype, I can change from computing in decimal, double, float, by changing this one line of code. Typecasting is equally automatic:

e.Position_full_mid[i_row] = (ptype)((decimal)this_Position + ((decimal)d_Position / 2M));

Where, in this case the division arithmetic must be performed with Decimal accuracy, then the quotient is explicitly cast to ptype. It's nice to have this point of control, but the using clause only has scope for the file it lives in (even though it's declared outside the class). It doesn't appear in the identical namespace in other files; I have to declare it each time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace procC
{
    using ptype = System.Double;

    class Commutation_Proc
    {

        [System.Serializable]
        public class input_data_val // magnetics data parsed from the FEMM results datafiles
        {
            public int Positions_Count_repeat = 0;  //total number of positions in the range equaling <json>PositionStart to equaling <json>PositionRepeat.
            public int Positions_Count_unique = 0;  //total number of positions in the range equaling <json>PositionStart through less than <json>PositionRepeat. (Positions_Count_repeat - 1)
            public ptype[] Position_raw, Position_full; //[Position Index] = double, containing position of the force-measured "moving group", where Position Index is "r_index" and the value is "x_mm" or "r_deg"
            public ptype[,,,,] Current_raw, Current_full; //[File index, Position Index, Current / Phase Index, Winding Sector, Winding Series] = double. For Commutated data, use Phase index; this resolves down rows where "phase_idx" is index of winding polarization pattern phase rotation

So far, I haven't found anyplace at the Solution level to define ptype just once, so changing the representation (like switching from Double to Decimal) means editing the

using ptype = System.<whatever>;

statement in a dozen classes (and counting). Can this be defined globally?

  • 3
    No, there is no way to define a type alias globally. – madreflection Mar 11 '20 at 01:54
  • Is there a reason you need to alias System.Double as ptype – Zakk Diaz Mar 11 '20 at 01:59
  • You might look into generics if you want to create methods that receive and/or return different types depending on the caller – Rufus L Mar 11 '20 at 01:59
  • 1
    @Alexei: I think this would be a better duplicate: https://stackoverflow.com/questions/57341751/how-can-i-create-an-alias-for-a-class-globally-in-unity because it's about type aliases, not namespace imports. – madreflection Mar 11 '20 at 02:00
  • @madreflection - thanks - added. It's really hard to find something useful for "C# using global" :) – Alexei Levenkov Mar 11 '20 at 02:01
  • @Zakk Diaz, I'm crunching numbers big time and want an easy way of auditing the tradeoffs between double, float and decimal types. By declaring ptype as the data type for sensitive code, I can change the whole thing with one line and more easily evaluate if the slower 'decimal' precision is worthwhile. – Graham Gunderson Apr 02 '20 at 02:29
  • You want to use generics then and make a base class of input_data_val which will let you use the same class for many types such as input_data_val or input_data_val – Zakk Diaz Apr 02 '20 at 17:16

0 Answers0