繁体   English   中英

C# - 初始化一个变量而不知道它会是什么

[英]C# - Initialize a variable without knowing what its going to be

我的数据库中有两个不同的表,每个表都根据用户的“SortOrder”显示给用户。 我编写了两个函数,它们占用一行(或实体)并将其排序顺序与最接近它的一个交换排序顺序(向上或向下,取决于正在执行的 function)。 我需要使这些函数适用于两个不同的表,具体取决于事件发生的位置(具有相同功能的多个网格视图)。 这是我到目前为止所拥有的(同样,有一个几乎相同的 function 用于向下移动,但我不会发布它,因为它是多余的):

protected void moveUp(String ValId, String dbName)
    {
        int ValueId = Convert.ToInt32(ValId);
        DataModel.DataAccess.Entities dc = new DataModel.DataAccess.Entities();
        if (dbName.ToLower() == "table1")
        {
            DataModel.DataAccess.Table1 currentValue = dc.Table1.Single(table1item => table1item.Table1ItemId == ValueId);
        }
        else if (dbName.ToLower() == "table2")
        {
            DataModel.DataAccess.Table2 currentValue = dc.Table2.Single(table2item => table2item.Table2ItemId == ValueId);
        }
        try
        {
            //make the change and update the database and gridview
        }
        catch (InvalidOperationException)
        {
        }
    }

明显的问题是我需要在 if 语句之前启动currentValue变量,否则它永远不会被声明的“可能性”,因此 function 的 rest (利用 currentValue 变量)将不起作用。

我的问题是:如果我不确定它会是什么,我应该如何在 if 语句之前初始化变量? 我认为这可能有效,但它说我仍然需要初始化它(“必须初始化隐式类型的局部变量”):

    var currentValue; //this is the line where I get the error message above
    if (dbName.ToLower() == "table1")
    {
        currentValue = (DataModel.DataAccess.Table1)dc.Table1.Single(table1item => table1item.Table1ItemId == ValueId);
    }
    else if (dbName.ToLower() == "table2")
    {
        currentValue = (DataModel.DataAccess.Table2)dc.Table2.Single(table2item => table2item.Table2ItemId == ValueId);
    }

[编辑] 更改标题以使其更准确地反映我的问题

在 C# 中,所有类型都需要一个类型。 如果您的Table#类型扩展DataModel.DataAccess.Table ,请使用:

DataModel.DataAccess.Table currentValue;

否则,您需要找到一个共同的基础 class (对象是所有这些的曾祖父)。

object currentValue;

由于您没有初始化currentValue ,编译器无法知道您所说的var是什么类型。 这就是为什么你得到例外。

附录:也许,您可以使用通用方法,而不是传入表的名称,如下所示:

moveUp(dc.Table1, item => item.Table1Key, "george");

void moveUp<T> (IEnumerable<T> table, Func<T,string> keySelector, string ValId)
{
    T currentValue = table.Single(item => keySelector(item) == ValueId);

    try
    {
        //make the change and update the database and gridview
    }
    catch (InvalidOperationException)
    {
    }
}

使用 object 类型而不是 var,尽管我可能会重写整个过程并使用一致(和标准)的命名约定。

所以:

object currentValue = null;

您可以尝试编写每个实体使用的接口和接受该接口的 function。

public interface ISortableEntity
{
    int ID { get; set; }
    int SortOrder { get; set; }
}


 public class DataFunctions
{
    public static void MoveUp(string dbName, int valID)
    {
        var db = //Get your context here;
        List<KeyValuePair<string, object>> keys = new List<KeyValuePair<string, object>>();
        keys.Add(new KeyValuePair<string, object>("ID", valID));

        ISortableEntity entity = db.GetObjectByKey(new System.Data.EntityKey(dbName, keys)) as ISortableEntity;

        if (entity != null)
        {
            entity.SortOrder += 1;
        }

        db.SaveChanges();
    }
}

你不知道变量的类型,这就是你隐式声明它的原因('var',而不是'int')?

您不必初始化显式类型 - 隐式类型需要它,因为它们通过给定的值确定它们的类型。

解决方案是接口。 您的表 1 和表 2 类应该实现一个带有 CurrentValue 属性的接口(例如 ISortableTable 或您想调用的任何名称)。 Table1 的 CurrentValue 属性实现将为 Table1 返回正确的结果,而 Table2 的 CurrentValue 属性将为 Table2 返回正确的结果。 然后,您的排序 function 可以与任何实现 ISortableInterface 并使用相应对象的 CurrentValue 属性的 class 一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM