[英]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.