87bb826b11aa8dc35bcc7436043e0f7e

Is there a better way to surround an array of byte through a prop? Instead of a class can I use a struct?
ps: Customers devs want to see index of array at sight, then array initializing b[16]{99,101,..} is not allowed.

public partial class Attivazioni
{
            public byte[] CodiceRichiesta
            {
                get { return GetCodiceRichiesta(); }
            }

            private byte[] GetCodiceRichiesta()
            {
                byte[] b = new byte[16];
                b[0] = 99;
                b[1] = 101;
                b[2] = 115;
                b[3] = 96;
                b[4] = 105;
                b[5] = 111;
                b[6] = 110;
                b[7] = 97;
                b[8] = 108;
                b[9] = 101;
                b[10] = 99;
                b[11] = 112;
                b[12] = 113;
                b[13] = 100;
                b[14] = 100;
                b[15] = 100;
                return b;
            }

            public byte[] CodiceModuloGestionale
            {
                get { return GetCodiceModuloGestionale(); }
            }

            private byte[] GetCodiceModuloGestionale()
            {
                byte[] b = new byte[16];
                b[0] = 103;
                b[1] = 101;
                b[2] = 115;
                b[3] = 116;
                b[4] = 105;
                b[5] = 111;
                b[6] = 110;
                b[7] = 97;
                b[8] = 108;
                b[9] = 101;
                b[10] = 111;
                b[11] = 112;
                b[12] = 113;
                b[13] = 114;
                b[14] = 115;
                b[15] = 116;
                return b;
            } 
}

Refactorings

No refactoring yet !

72f36daa501cf8f5bb861210edd9232d

Moonshield

March 4, 2010, March 04, 2010 01:11, permalink

1 rating. Login to rate!

I used static property so you dont have to create an instance everytime. I moved the array creation in a static constructor so we don't have to recreate the same array everytime someone access the property. Also, the property use a private set so the collection can be changed from outside but its values could be changed though. If you dont want the values to be changed, just moved your array initialization inside your property, so you will end up with only 2 properties instead of 2 properties and 2 methods. Hope it helps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] rich = Attivazioni.CodiceRichiesta;
            byte[] gest = Attivazioni.CodiceModuloGestionale;
        }
    }

    public partial class Attivazioni
    {
        public static byte[] CodiceRichiesta { get; private set; }
        public static byte[] CodiceModuloGestionale { get; private set; }

        static Attivazioni()
        {
            // CodiceRichiesta
            byte[] aRich = new byte[16];
            aRich[0] = 99;
            aRich[1] = 101;
            aRich[2] = 115;
            aRich[3] = 96;
            aRich[4] = 105;
            aRich[5] = 111;
            aRich[6] = 110;
            aRich[7] = 97;
            aRich[8] = 108;
            aRich[9] = 101;
            aRich[10] = 99;
            aRich[11] = 112;
            aRich[12] = 113;
            aRich[13] = 100;
            aRich[14] = 100;
            aRich[15] = 100;
            CodiceRichiesta = aRich;

            // CodiceModuloGestionale
            byte[] aGest = new byte[16];
            aGest[0] = 103;
            aGest[1] = 101;
            aGest[2] = 115;
            aGest[3] = 116;
            aGest[4] = 105;
            aGest[5] = 111;
            aGest[6] = 110;
            aGest[7] = 97;
            aGest[8] = 108;
            aGest[9] = 101;
            aGest[10] = 111;
            aGest[11] = 112;
            aGest[12] = 113;
            aGest[13] = 114;
            aGest[14] = 115;
            aGest[15] = 116;
            CodiceModuloGestionale = aGest;
        }      
    }
}

Your refactoring





Format Copy from initial code

or Cancel