Setting multiple Index properties in a class

287 views Asked by At

I am trying use indexer to solve array property in the class. Please let me know how to handle based on below code sample. Index based property should be able to update another property internally.

Legacy Structure:

Record LegacyRec type basicRecord  
    3 TOTALPAID num(13,2) ;
    3 TABLEOC2 char(85) ; // ONE OCCUR OF DATA - Below items are part of TABELOC2   
        4 DUEDATEX char(10) ;     
        4 ACCTX char(6) ;     
        4 TAXX num(9,2) ;     
        4 PENALTYX num(9,2) ;    
        4 INTERESTX num(9,2) ;
        4 FEESX num(9,2) ; 
        4 TOTALPAIDX num(13,2) ; 
        4 SEQNUMX smallint ; 
        4 BILLNUMX char(8) ; 
        4 FILEDDATEX char(10) ;
    3 TABLEKEY char(12) ; // TABLEKEY    
        4 TABLEID char(8) ;     
        4 TABLECODE char(4) ; 
    3 TABLEALL char(29750) ; // UP TO 400 OCCURS    
        4 TABLEOC1 char(85) [350] ; // INDEXED REDEFINE OF ABOVE  
    3 TABLESCREEN char(1275) ; // UP TO 15 OCCURS - It holds the data below array 
        4 TABLESCR char(61) [15] ; // INDEXED REDEFINE OF ABOVE      
            5 DUEDATES char(10) ;       
            5 ACCTS char(6) ;       
            5 TAXS num(9,2) ; 
            5 SEQNUMS smallint ;       
            5 CNT int ;
            5 BILLNUMS char(10) ;       
            5 FILEDDATES char(20) ;
end

Front Code: (Cannot change but I can some additonal function calls in case needed)

decimal x; 
string y;

LegacyRec XYZ1 = new LegacyRec();
XYZ1.TABLEOC2 = "ABCDEFGHIJXYZPQR  1234.78   67    4567.89  755553.55  56  63744.6 1RTY     ZXCVBNMIOP";

x = XYZ1.PENALTYX;
y = XYZ1.DUEDATEX;
y = XYZ1.FILEDDATEX;
XYZ1.PENALTYX = 111.11M;
XYZ1.SEQNUMX = 34;
XYZ1.FILEDDATEX = "1234567890";

XYZ1.TABLEOC1[2]= "RRRR";
XYZ1.bdate = 10;
XYZ1.iitem[1] = 123;
XYZ1.il6[2] = "RTRRRTY";
for (int i = 1; i < XYZ1.TABLESCR.Count(); i++)
    XYZ1.TABLESCR[i] = "";

XYZ1.TABLESCR[6] = "10/10/2010A5670056.89    231000AAA          20200202";
y = XYZ1.TAXS[6] ; //should show 56.89

.NET class //My current Structure Missing Indexer. I have problem setting up array of array property using Indexer.

public class LegacyRec: basicRecord
{
    public decimal TOTALPAID { get; set; }

    public string TABLEOC2 { get; set; } //Main field
    public string DUEDATEX { 
        get { return getPartialData(TABLEOC2, 1, 10).ToString(); } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 1, 10, value).ToString(); } } //Sub Fields
    public string ACCTX { 
        get { return getPartialData(TABLEOC2, 11, 6).ToString(); } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 11, 6, value).ToString(); } } //Sub Fields
    public decimal TAXX { 
        get { try { return decimal.Parse(getPartialData(TABLEOC2, 17, 9).ToString()); } catch { return 0; } }  
        set { TABLEOC2 = setPartialData(TABLEOC2, 11, 6, value).ToString(); } } //Sub Fields
    public decimal PENALTYX { 
        get { try { return decimal.Parse(getPartialData(TABLEOC2, 26, 9).ToString()); } catch { return 0; } } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 26, 9, value).ToString(); } } //Sub Fields
    public decimal INTERESTX { 
        get { try { return decimal.Parse(getPartialData(TABLEOC2, 35, 9).ToString()); } catch { return 0; } }  
        set { TABLEOC2 = setPartialData(TABLEOC2, 35, 9, value).ToString(); } } //Sub Fields
    public decimal FEESX { 
        get { try { return decimal.Parse(getPartialData(TABLEOC2, 44, 9).ToString()); } catch { return 0; } } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 44, 9, value).ToString(); } } //Sub Fields
    public decimal TOTALPAIDX { 
        get { try { return decimal.Parse(getPartialData(TABLEOC2, 53, 13).ToString()); } catch { return 0; } } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 53, 13, value).ToString(); } } //Sub Fields
    public int SEQNUMX {
        get { try { return int.Parse(getPartialData(TABLEOC2, 66, 2).ToString()); } catch { return 0; } } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 66, 2, value).ToString(); } } //Sub Fields
    public string BILLNUMX { 
        get { return getPartialData(TABLEOC2, 68, 8).ToString(); } 
        set { TABLEOC2 = setPartialData(TABLEOC2, 68, 8, value).ToString(); } } //Sub Fields
    public string FILEDDATEX {            
        get { return getPartialData(TABLEOC2, 76, 10).ToString(); }             
        set { TABLEOC2 = setPartialData(TABLEOC2, 76, 10, value).ToString(); } } //Sub Fields 

    public string TABLEALL { get; set; } // UP TO 400 OCCURS
    private string[] _TABLEOC1;

    public string this[int index]  //not working
    {
    get { return _TABLEOC1[index]; }
    set { _TABLEOC1[index] = value; TABLEALL = setPartialData(_TABLEOC1[index], index*10, 10, value).ToString(); //not working at this time}
    }

    public string[] TABLEOC1 {
        get { return _TABLEOC1; } 
        set { TABLEALL = setPartialData(TABLEOC1[index], index*, 85, value).ToString(); } } // INDEXED REDEFINE OF ABOVE [350]

    private string[] _TABLESCR;
    public string[] TABLESCR { get { return _TABLESCR; } set { string s = "123"; } } //Main array with TableScreen                
    public string[] DUEDATES { 
        get from TABLESCR[index]; 
        set TABLESCR particular Index; }
    public string[] ACCTS { 
        get from TABLESCR[index]; 
        set TABLESCR particular Index; }
    public decimal[] TAXS {
        get from TABLESCR[index]; 
        set TABLESCR particular Index;}
    public int[] SEQNUMS {
        get from TABLESCR[index]; 
        set TABLESCR particular Index;}
    public int[] CNT {
        get from TABLESCR[index]; 
        set TABLESCR particular Index;}
    public string[] BILLNUMS {
        get from TABLESCR[index]; 
        set TABLESCR particular Index;}
    public string[] FILEDDATES {
        get from TABLESCR[index]; 
        set TABLESCR particular Index;}
}    
2

There are 2 answers

1
Jim Mischel On BEST ANSWER

The short answer to your question is that you have to create arrays to do that.

It's complicated because you're trying to do data overlays in a way that C# just doesn't support well. For example, you have:

XYZ1.TABLEOC2 = "ABCDEFGHIJXYZPQR  1234.78   67    4567.89  755553.55  56  63744.6 1RTY     ZXCVBNMIOP";
x = XYZ1.PENALTYX;

And XYZ1.PENALTYX is one of the numeric fields in that string. You can make it work okay for a single record like TABLEOC2, but when you get into arrays it gets rather difficult. C# just doesn't have the concept of overlaying numbers on top of strings. Trying to make it do that will require that you define a TABLESCR class that is implicitly assignable from string, and also has properties for ACCTS, TAXS, etc. You could then have an array of those TABLESCR instances and index into that.

Your TABLESCR class would look something like:

public class TABLESCR
{
    private string _data;
    public TABLESCR(string data)
    {
        _data = data;
    }

    public static implicit operator TABLESCR(string s)
    {
        return new TABLESCR(s);
    }

    public string DUEDATES
    {
        get { return ... }
        set { // set the value in the _data string }
    }
}

It's hard to say if you want a struct or a class there. There are drawbacks to each, but I'd suggest a class. The constructor for your basicRecord would probably want to initialize the array with a bunch of empty TABLESCR instances.

In your basicRecord, you'd have:

public TABLESCR[] TABLESCREEN = new TABLESCR[15];

TABLEALL is pretty easy, just an array of strings:

public string[] TABLEALL = new string[350];

Then you can reference TABLESCREEN[1] and TABLEALL[1], etc.

Understand, that doesn't exactly duplicate the memory layout that you had, but it duplicates the functionality. You can use custom import and export logic to convert this from and to whatever data storage format you're using.

0
user1810535 On

Record VTDMW01-NEW type basicRecord 3 TABLEALL char(29750) ; // UP TO 400 OCCURS 4 TABLEOC1 char(85) [350] ; // INDEXED REDEFINE OF ABOVE 3 TABLESCREEN char(1275) ; // UP TO 400 OCCURS 4 TABLESCR char(85) [15] ; // INDEXED REDEFINE OF ABOVE 5 DUEDATES char(10) ; 5 ACCTS char(6) ; 5 TAXS num(9,2) ; 5 PENALTYS num(9,2) ; 5 INTERESTS num(9,2) ; 5 FEESS num(9,2) ; 5 TOTALPAIDS num(13,2) ; 5 SEQNUMS smallint ; 5 BILLNUMS char(8) ; 5 FILEDDATES char(10) ; 3 TABLEOC2 char(85) ; // ONE OCCUR OF DATA 4 DUEDATEX char(10) ; 4 ACCTX char(6) ; 4 TAXX num(9,2) ; 4 PENALTYX num(9,2) ; 4 INTERESTX num(9,2) ; 4 FEESX num(9,2) ; 4 TOTALPAIDX num(13,2) ; 4 SEQNUMX smallint ; 4 BILLNUMX char(8) ; 4 FILEDDATEX char(10) ; end

public class Vtdmw01New : basicRecord { public String tableall // UP TO 400 OCCURS { get { return getString(0, 29750); } set { setString(0, 29750, value); } } public StringArray tableoc1 // INDEXED REDEFINE OF ABOVE { get { return getStringArray(0, 85, 350, 85); } set { setStringArray(0, 85, 350, 85, value); } } public String tablescreen // UP TO 400 OCCURS { get { return getString(29750, 1275); } set { setString(29750, 1275, value); } } public StringArray tablescr // INDEXED REDEFINE OF ABOVE { get { return getStringArray(29750, 85, 15, 85); } set { setStringArray(29750, 85, 15, 85, value); } } public StringArray duedates { get { return getStringArray(29750, 10, 15, 85); } set { setStringArray(29750, 10, 15, 85, value); } } public StringArray accts { get { return getStringArray(29760, 6, 15, 85); } set { setStringArray(29760, 6, 15, 85, value); } } public DecimalArray taxs { get { return getDecimalArray(29766, 9, 2, 15, 85); } set { setDecimalArray(29766, 9, 2, 15, 85, value); } } public DecimalArray penaltys { get { return getDecimalArray(29775, 9, 2, 15, 85); } set { setDecimalArray(29775, 9, 2, 15, 85, value); } } public DecimalArray interests { get { return getDecimalArray(29784, 9, 2, 15, 85); } set { setDecimalArray(29784, 9, 2, 15, 85, value); } } public DecimalArray feess { get { return getDecimalArray(29793, 9, 2, 15, 85); } set { setDecimalArray(29793, 9, 2, 15, 85, value); } } public DecimalArray totalpaids { get { return getDecimalArray(29802, 13, 2, 15, 85); } set { setDecimalArray(29802, 13, 2, 15, 85, value); } } public BinaryArray seqnums { get { return getBinaryArray(29815, 2, 15, 85); } set { setBinaryArray(29815, 2, 15, 85, value); } } public StringArray billnums { get { return getStringArray(29817, 8, 15, 85); } set { setStringArray(29817, 8, 15, 85, value); } } public StringArray fileddates { get { return getStringArray(29825, 10, 15, 85); } set { setStringArray(29825, 10, 15, 85, value); } } public String tableoc2 // ONE OCCUR OF DATA { get { return getString(31025, 85); } set { setString(31025, 85, value); } } public String duedatex { get { return getString(31025, 10); } set { setString(31025, 10, value); } } public String acctx { get { return getString(31035, 6); } set { setString(31035, 6, value); } } public Decimal taxx { get { return getDecimal(31041, 9, 2); } set { setDecimal(31041, 9, 2, value); } } public Decimal penaltyx { get { return getDecimal(31050, 9, 2); } set { setDecimal(31050, 9, 2, value); } } public Decimal interestx { get { return getDecimal(31059, 9, 2); } set { setDecimal(31059, 9, 2, value); } } public Decimal feesx { get { return getDecimal(31068, 9, 2); } set { setDecimal(31068, 9, 2, value); } } public Decimal totalpaidx { get { return getDecimal(31077, 13, 2); } set { setDecimal(31077, 13, 2, value); } } public int seqnumx { get { return getBinary(31090, 2); } set { setBinary(31090, 2, value); } } public String billnumx { get { return getString(31092, 8); } set { setString(31092, 8, value); } } public String fileddatex { get { return getString(31100, 10); } set { setString(31100, 10, value); } } }

public StringArray getStringArray(int i)
    {
        return getStringArray(getOffSet(i, ColumnSize[i]), ColumnSize[i], ArrayLength, 0, i);
    }

public class StringArray : AnyArray
    {
        public String this[int i]
        {
            get { return rec.getString(offset + disp * (i - 1), len); }
            set { rec.setString(offset + disp * (i - 1), len, value); }
        }

        public StringArray(basicRecord rec, int offset, int len, int ArrayLen, int Disp = 0, int ColumnNo = 0)
            : base(rec, offset, len, ArrayLen, Disp, ColumnNo)
        {
            //do nothing. Basclass constructor handles this
        }

        public bool Find(string compare)
        {
            for (int i = 1; i <= arrayLen + 1; i++)
            {
                if (this[i].Trim() == compare.Trim())
                    return true;
            }
            return false;
        }
    }

    public class AnyArray
    {
        protected int offset;
        protected int len;
        protected int arrayLen;
        protected int disp;
        protected basicRecord rec;
        public int columnNo;

        public AnyArray()
        {
        }

        public AnyArray(basicRecord rec, int offset, int len, int ArrayLen, int Disp = 0, int ColumnNo = 0)
        {
            this.rec = rec;
            this.offset = offset;
            this.len = len;
            this.arrayLen = ArrayLen;
            this.disp = (Disp == 0) ? len : Disp;
            this.columnNo = ColumnNo;
        }
    }