首页 > 解决方案 > C#:无法将字符串转换为字符串 [] []

问题描述

我对我的代码有疑问。情况如下:我正在处理许多具有相同长度的数据数组。然后我想建立一个具有特定阵营的第二个版本。

我有以下代码:

string[][][] cuentasHFMPreFinal = new string[nroCuenta.Length][][];
string anoCubo = "'2019";
string escenarioCubo = "Control";
string versionCubo = "Version Vigente";
for (int i = 0, j = 0, k = 0; i < nroCuenta.Length && j < nroCuenta.Length && k < nroCuenta.Length; i++, j++, k++){
cuentasHFMPreFinal[i] = anoCubo;
}

这个想法是为新数组提供特定的值(它将有更多的维度,这是一个简短的例子)。结束数组将以如下方式结束:

cuentasHFM [anoCubo][escenarioCubo][versionCubo][....][lastVar]

错误出现在cuentasHFMPreFinal[i] = anoCubo部分,给出标题错误。

完整代码是:

 static void Main(string[] args)
        {
            //-----------------------------------------------------------------------
            //ARCHIVO CUENTAS AUTOMATICAS
            //obtener direccion de archivo
            string pathCuentasAuto = @"C:\DescargaHFM\data\cuentas_auto.txt";
            pathCuentasAuto = Path.GetFullPath(pathCuentasAuto);

            //leer lineas archivo y pasar a arreglo
            string[] cuentasAuto = File.ReadAllLines(pathCuentasAuto);

            //-----------------------------------------------------------------------
            //ARCHIVO HFM CHILE
            //obtener direccion de archivo
            string pathHFM = @"C:\DescargaHFM\data\HFM.txt";
            pathHFM = Path.GetFullPath(pathHFM);

            //leer lineas archivo y pasar a arreglo
            string[] hfm = File.ReadAllLines(pathHFM);

            //obtener separador tab
            string separadorTab = "\t";

            //separar lineas por separadores
            string[][] camposHFM = new string[hfm.Length][];
            for (int i = 0; i < hfm.Length; i++) {
                camposHFM[i] = hfm[i].Split(separadorTab.ToCharArray());
            }

            //arreglo con periodo(meses)
            string[] mesesHFM = new string[camposHFM.Length];
            for (int i = 0; i < camposHFM.Length; i++)
            {
                string mesPeriodo = camposHFM[i][0];
                switch (mesPeriodo){
                    case "Per01":
                        mesPeriodo = "Jan";
                        break;
                    case "Per02":
                        mesPeriodo = "Feb";
                        break;
                    case "Per03":
                        mesPeriodo = "Mar";
                        break;
                    case "Per04":
                        mesPeriodo = "Apr";
                        break;
                    case "Per05":
                        mesPeriodo = "May";
                        break;
                    case "Per06":
                        mesPeriodo = "Jun";
                        break;
                    case "Per07":
                        mesPeriodo = "Jul";
                        break;
                    case "Per08":
                        mesPeriodo = "Aug";
                        break;
                    case "Per09":
                        mesPeriodo = "Sep";
                        break;
                    case "Per10":
                        mesPeriodo = "Oct";
                        break;
                    case "Per11":
                        mesPeriodo = "Nov";
                        break;
                    case "Per12":
                        mesPeriodo = "Dec";
                        break;
                }

                mesesHFM[i] = mesPeriodo;
            }


            //otener cantidad total y cantidad gl
            double[] QTotalUSGAP = new double[hfm.Length];
            double[] QTotalGL = new double[hfm.Length];
            double[] QTotalResta = new double[hfm.Length];

            for (int i = 0; i < camposHFM.Length; i++) {
                QTotalUSGAP[i] = Double.Parse(camposHFM[i][3]);
                QTotalGL[i] = Double.Parse(camposHFM[i][4]);
                QTotalResta[i] = QTotalUSGAP[i] - QTotalGL[i];
            }

            //obtener numero cuenta
            string[] nroCuenta = new string[hfm.Length];

            /*Formula Excel:
             *SI(IZQUIERDA(C2;4)="ACCT";MED(C2;5;LARGO(C2)-3);IZQUIERDA(C2;ENCONTRAR("_";C2)-1))
            */
            for (int i = 0; i < camposHFM.Length; i++) {
                nroCuenta[i] = camposHFM[i][2];
                if (nroCuenta[i].Substring(0, 4) == "ACCT") {
                    nroCuenta[i] = nroCuenta[i].Substring(5, (nroCuenta[i].Length - 3));
                }
                else {
                    //int indexFind = nroCuenta[i].IndexOf('_');
                    //nroCuenta[i] = nroCuenta[i].Substring(0, (indexFind - 1));
                    nroCuenta[i] = nroCuenta[i].Substring(0, nroCuenta[i].Length);
                }
            }

            //comprobar existencia de nroCuenta en cuentasAuto
            string[] existeNroCuenta = new string[nroCuenta.Length];
            for (int i = 0; i < nroCuenta.Length; i++){
                if (cuentasAuto.Contains(nroCuenta[i])){
                    existeNroCuenta[i] = "TRUE";
                }
                else{
                    existeNroCuenta[i] = "FALSE";
                }
            }

            //armar arreglo pre final
            string[][][] cuentasHFMPreFinal = new string[nroCuenta.Length][][];
            string anoCubo = "'2019";
            string escenarioCubo = "Control";
            string versionCubo = "Version Vigente";
            string monedaCubo = "CLP";
            string ubgCubo = "Generico UBG";
            string ajusteICCubo = "Ajuste 99";
            string organizacionCubo = "Generico";

            for (int i = 0, j = 0, k = 0; i < nroCuenta.Length && j < nroCuenta.Length && k < nroCuenta.Length; i++, j++, k++){
                cuentasHFMPreFinal[i] = anoCubo;


            }
        }

标签: c#

解决方案


使用类、结构或元组来实现您想要实现的目标。


推荐阅读