都需要在foreach語句中我有以下代碼從VB到C#類型和標識符使用對象
private void LoadDropDownList()
{
DropDownList location = (DropDownList) Form.FindControl("ddlLocation_ID");
DropDownList vendorID = (DropDownList) Form.FindControl("ddlVendor_ID");
//-- Load the Sales Locations
dtDataList_v10_r1.List objList = new dtDataList_v10_r1.List();
DataSet ds = default(DataSet);
DataView dvActiveLocations = null;
ds = objList.GetSalesLocationDataset(mobjSecurity.SQLDatabase, mobjSecurity.Inst_ID);
if ((ds != null)) {
if (ds.Tables.Count > 0) {
dvActiveLocations = new DataView(ds.Tables[0]); //changed to square brackets per c# syntax 10/9/15 Max //
dvActiveLocations.RowFilter = "status='A'";
}
}
//ddlLocation_ID.DataSource = dvActiveLocations;
//ddlLocation_ID.DataTextField = "ChannelName";
//ddlLocation_ID.DataValueField = "Channel_ID";
//ddlLocation_ID.DataBind();
location.DataSource = dvActiveLocations; // changed to reference control and c# syntax 10/9/15 Max //
location.DataTextField = "ChannelName"; // changed to reference control and c# syntax 10/9/15 Max //
location.DataValueField = "Channel_ID"; // changed to reference control and c# syntax 10/9/15 Max //
location.DataBind(); // changed to reference control and c# syntax 10/9/15 Max //
//-- Load the Available Auction downloads
dtIntegration_v10_r1.Vendor objVendor = default(dtIntegration_v10_r1.Vendor);
dtIntegration_v10_r1.Vendor[] objVendors = null;
dtIntegration_v10_r1.Auctions objAuctions = new dtIntegration_v10_r1.Auctions(ref mobjSecurity); //added ref key word 10/9/15 Max //
objVendors = objAuctions.Vendors;
foreach (objVendor in objVendors)
if (objVendor.HasVendorRelationship == true)
{
//ddlVendor_ID.Items.Insert(0, objVendor.Name);
//ddlVendor_ID.Items(0).Value = objVendor.Vendor_ID;
vendorID.Items.Insert(0, objVendor.Name);
vendorID.Items[0].Value = Convert.ToString(objVendor.Vendor_ID); //changed to reference control and facilitate conversion to string 10/9/15 Max //
}
}
轉換我得到以下錯誤,當我執行它
類型和標識符在foreach語句中都是必需的
現在我認識到一個正確的聲明應該看起來像這樣
的foreach(在對象的類型var somevar)....
我想這句法和它拋出這個錯誤
名爲 'objVendor' 的局部變量不能在此範圍內聲明因爲它會賦予'objVendor'不同的含義,'objVendor'已經在父級或當前範圍內用於表示其他內容
所以我很好奇如何解決這個問題特別的錯誤。在任何的主題我無法在這裏找到它,所以我想我會問
爲什麼不給它一個不同的名字? –
@MaorVeitsman @MaorVeitsman我試過了,它似乎給出了同樣的錯誤,即使當我在foreach語句中更改引用名稱和變量時 – MaximusPrime
感謝downvote,儘管我不知道爲什麼 – MaximusPrime