2015-10-19 80 views
0

我寫了一個單獨的類,其中包含我的網站的權限。我在每個班級打電話給這個班級。我無法從我的權限類中取回值。我會告訴你我有如下:返回多個值C#

權限類

public class Permissions 
{ 
    public static string selectedNumber = ""; 
    public static string selectedName= ""; 
    public static string selectedLocation= ""; 

    public Info SetInfo(string selectedValue) 
    {   
     string selectInfo = "SELECT [Num] AS 'Number', [Name] AS 'Name', CONVERT(nvarchar(50),RTRIM(b.Num])) + ' - ' + CONVERT(nvarchar(50),b.Name) AS 'Location' " 
      + "FROM [TBL_Info] a " 
      + "left join [TBL_Where] b on (a.[ID] = b.[ID]) " 
      + "WHERE a.ID = @ID"; 

     sqlCmd = new SqlCommand(selectInfo, sqlConn); 
     sqlConn.Open(); 

     sqlCmd.Parameters.AddWithValue("@ID", selectedValue); 

     SqlDataReader rdrInfo = sqlCmd.ExecuteReader(); 

     if (rdrInfo.HasRows) 
     { 
      rdrInfo.Read(); 

      selectedNumber = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Number")).ToString(); 
      selectedName= rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Name")).ToString(); 
      selectedLocation = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Location")).ToString(); 
     } 
     sqlCmd.Connection.Close(); 

     return new Info() 
     { 
      number= selectedNumber, 
      name= selectedName, 
      location= selectedLocation 

     }; 
    } 

    public class Info 
    { 
     public String number{ get; set; } 
     public String name{ get; set; } 
     public String location{ get; set; } 
    } 
} 

目前我想它調用另一個類是這樣的:

Classes.Permissions permission = new Classes.Permissions(); 
permission.SetInfo(selectedUserValue); 

最終產品,是我所文本框中的類我試圖使用來自permission.SetInfo()的3個返回值的電話

目前我無法獲得任何返回....我知道我明顯地做錯了什麼。那麼有人可以給我一些關於如何實現這一目標的建議嗎?

+1

你不保存您正在返回對象'VAR信息=權限.SetInfo(selectedUserValue);'之後,你可以使用'info'變量 –

+0

對,我看到...但是我將如何返回3個對象?在列表中? – Code

+0

您應該在'using'塊中打開sql連接,以便它始終關閉。 –

回答

2

除了我遇到的一些風格問題之外,代碼看起來應該起作用(只要數據庫中有數據)。要從方法返回多個值,請創建一個新對象(即Info),該對象定義要返回的內容。

所以,你應該能夠寫類似:

Textbox txtBox; 

var info = new Classes.Permissions().SetInfo(SelectedUserValue); 

txtBox.Text = 
     info.number ?? "<null>" + " " + 
     info.name ?? "<null> + " " + 
     info.location ?? "<null>' 

請注意,我用的是空合併運算符(??)作爲SetInfo可以返回的Info,所有成員都null如果沒有行實例從數據庫查詢中返回。

順便說一句,另一路從一個方法返回多個值是使用out參數:

Textbox txtBox; 
string number; 
string name; 
string location; 

new Classes.Permissions().SetInfo(SelectedUserValue, out number, out name, out location); 

然後你SetInfo會是什麼樣子:

public void SetInfo(string SeelctedUserValue, 
    out string number, out string name, out string location) 
{ 
    //assign values to number,name and location 
} 

要返回多個實例相同的對象,比你的方法應該返回

  1. 一個IEnumerable(即ListArray),
  2. 一個Tuple<>
  3. 的容器對象。

例如,如果你知道你要返回恰好有三個,你可能希望有你的方法返回Tuple<Info,Info,Info>

public Tuple<Info, Info, Info> SetInfo(string SeelctedUserValue) 
{ 
    //query db 
    return new Tuple<Info, Info, Info>(
    new Info{ number = "number", name = "name", location="location}, 
    new Info{ number = "number", name = "name", location="location}, 
    new Info{ number = "number", name = "name", location="location});   
} 
+0

輸出參數看起來更容易... – Code

+0

有幾種方法可以做你想做的事情。你必須決定哪種方法最適合你的情況和環境。 –

+0

我有我的最初的方式和參數工作。將決定什麼是最適合我的。非常感謝你 – Code