无法从另一个班级访问列表

时间:2014-01-30 15:04:50

标签: c# list class

我正在尝试访问List中的Class,因此我可以在另一个Insert语句中使用这些值。我的想法是,我使用List使用excel电子表格中的值填充for loop(发生在LegalTransactionList Class中)。然后我将把这些值和insert它们放到数据库表中(在Helpers Class中进行)。我在lstOfTran.AccountNumber上收到一个错误,指出LegalTransactionRec不包含AccountNumber的定义。任何帮助将不胜感激。

class LegalTransactionList
{

    public static List<LegalTransactionRec> setTransactions()
    {
        Form1 form = new Form1();
        Workbook workbook = form.excelApp.Workbooks.Open(form.txtbxFilename.Text);

        List<LegalTransactionRec> lstTran = new List<LegalTransactionRec>();

        try
        {

            //workbook = excelApp.Workbooks.Open(txtbxFilename.Text);                      View above comment
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range xlRange = worksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    lstTran.Add(new LegalTransactionRec()
                    {
                        AccountNumber = Form1.CleanString(xlRange.Cells[i, 1].Value2.ToString()),
                        CostAmount = Form1.TryToParse(Form1.CleanAmount(xlRange.Cells[i, 3].Value2.ToString())),
                        SSN = Form1.CleanString(xlRange.Cells[i, 6].Value2.ToString()),
                        TransactionDate = Form1.CheckDate(xlRange.Cells[i, 2].Value2.ToDate()),
                        Description = xlRange.Cells[i, 8].Value2.ToString(),
                        TransactionCode = Form1.CheckNull(xlRange.Cells[i, 4].Value2.ToInt())
                    });
                }
            }

            if (form.validateHeader(worksheet))
            {




            }
        }
        catch (Exception ex)
        {
        }
        return lstTran;

    }
}

class Helpers
{
    public void insertRecords()
    {
        StringBuilder sql = new StringBuilder();
        var lstOfTran = LegalTransactionList.setTransactions();

        using (DataTable dt = DataManager.GetData(sql))
        {
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    sql.AppendLine("INSERT INTO LEGAL_TRANSCATIONS (BATCH_ID, ACCOUNT, ATTORNEY_ID, ORG_ID, TRANSACTION_DATE, DATE_INSERTED, TRANSACTION_CODE, AMOUNT, DESCRIPTION, DEBTOR_SSN");
                    sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber + );
                }
                catch (Exception ex)
                { }
            }
        }
    }
}

public class LegalTransactionRec
{
    public string AccountNumber { get; set; }
    public string CostAmount { get; set; }
    public string SSN { get; set; }
    public int BatchID { get; set; }
    public Attorney Attorney { get; set; }
    public DateTime TransactionDate { get; set; }
    public string Description { get; set; }
    public int TransactionCode { get; set; }
}

public int CheckNull(int intVal)
    {
        int cleanValue;
        if (intVal == null)
        {
            throw new System.ArgumentException("Value cannot be null", "original");
        }
        else
        {
            cleanValue = intVal;
        }

        return cleanValue;
    }

    public string CleanString(String strVal)
    {
        string cleanValue;
        if (strVal == null)
        {
            throw new System.ArgumentException("Value cannot be null", "original");
        }
        else
        {
            cleanValue = Regex.Replace(strVal, " ", "").Replace("$", "").Replace("-", "");
        }

        return cleanValue;
    }

    public string CleanAmount(String amt)
    {
        string cleanAmt;
        if (amt == null)
        {
            throw new System.ArgumentException("Value cannot be null", "original");
        }
        else
        {
            cleanAmt = Regex.Replace(amt, "(", "-").Replace(")", "").Replace("$", "").Replace("[^0-9]+", "").Replace(" ", "");
        }
        return cleanAmt;
    }

    public static void TryToParse(string strAmt)
    {
        decimal decAmt;

        bool result = Decimal.TryParse(strAmt, out decAmt);

    }

    public DateTime CheckDate(DateTime tranDate)
    {
        DateTime date;

        if (tranDate == null)
        {
            throw new System.ArgumentException("Value cannot be null", "original");
        }
        else
        {
            date = tranDate;
        }

        return date;
    }

4 个答案:

答案 0 :(得分:3)

您正尝试访问lstOfTran类型的LegalTransactionRec列表中的属性,该列表不需要AccountNumber属性。我想你要尝试的是lstOfTran[i].AccountNumber

答案 1 :(得分:2)

编辑:

这行甚至没有编译:

                sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber + );

+之后发生了什么?


深入研究:

AccountNumber = Form1.CleanString(xlRange.Cells[i, 1].Value2.ToString()),

看起来CleanString()会返回意外的内容。你能展示实施吗?

答案 2 :(得分:2)

您正在寻找AccountNumber上的lstOfTran

var lstOfTran = LegalTransactionList.setTransactions();
...
sql.AppendLine("..." + lstOfTran.AccountNumber);

LegalTransactionList.setTransactions()会返回LegalTransactionRec列表列表没有该属性,列表中的也有。

如果您打算遍历事务列表,那么它将类似于:

StringBuilder sql = new StringBuilder();
var lstOfTran = LegalTransactionList.setTransactions();

foreach (LegalTransactionRec trans in lstOfTran )
{
    sql.AppendLine(" INSERT INTO LEGAL_TRANSCATIONS (BATCH_ID, ACCOUNT, ATTORNEY_ID, ORG_ID, TRANSACTION_DATE, DATE_INSERTED, TRANSACTION_CODE, AMOUNT, DESCRIPTION, DEBTOR_SSN" );
    sql.AppendLine(" VALUES ( (select max(batch_id) from legal_transaction_batch_info),"
                  + trans.AccountNumber + );    
               //   ^-- use loop variable here
}

其他注意事项:

  • 不要吞下例外 - 要么处理它们,要么让它们冒泡。
  • 如果可以提供帮助,请不要附加字符串以生成SQL - 您冒着SQL注入的风险
  • 如果您执行追加字符串以获取SQL,请务必在字符串之间添加空格。 StringBuilder将添加一个换行符,可能被SQL尊重,但比抱歉更安全。
  • 从excel读取时,将整个值范围拉入数组,然后遍历数组。 xlRange.Cells[i, j]调用是COM调用,非常昂贵。如果首先将所有数据拉入数组,您会惊讶地发现它运行得多快多少。

答案 3 :(得分:2)

setTransactions()正在返回List<LegalTransactionRec>

并且您将var lstOfTran设置为从

返回的列表
LegalTransactionList.setTransactions();

您需要在列表中指定您尝试访问的项目。

尝试改为:

lstOfTran[0].AccountNumber

您可能还需要更改

sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber + );

sql.AppendLine("VALUES ( (select max(batch_id) from legal_transaction_batch_info)," + lstOfTran.AccountNumber );

注意我在+

之后删除了最后一次lstOfTran.AccountNumber