我正在嘗試從 menuStrip 按鈕單擊上的 2 個不同串列中形成一個新串列,但它要么沒有將它們放入“newList”串列中,要么將其顯示為空。List1 是“.txt”中第一個資料檔案的串列。檔案,List2 相同,newList 用于從 2 個串列中的特定引數形成一個新串列。建議?
public partial class Form1 : Form
{
List<Pieces> List1;
List<Pieces> List2;
List<Pieces> newList;
Pieces piece;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
enterSecondFileToolStripMenuItem.Enabled = false;
}
private void accordingToUsersSpecificationsToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAUsersList(List1,newList);
FormAUsersList(List2,newList);
///newList was null error that makes no sense
///object refrence not set to an instance of an object
///error
finalResults.Text = newList.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
/// <summary>
/// Form a list according to selected parameters by the user that we call
/// 2 times to form a complete new list
/// </summary>
/// <param name="list"></param>
/// <param name="newList"></param>
/// <returns></returns>
private List<Pieces> FormAUsersList(List<Pieces> list, List<Pieces> newList)
{
newList = new List<Pieces>();
int year = Convert.ToInt32(Years.SelectedItem); /// comboBox item that signifies years
string type = Convert.ToString(Types.SelectedItem); /// comboBox item that signifies types
for (int i = 0; i < list.Count; i )
{
if (year <= list[i].PurchaseYear && type == list[i].Type)
{
newList.Add(list[i]);
}
}
return newList;
}
}
uj5u.com熱心網友回復:
您應該對傳入 List1 和 List2 的 FormAUsersList 進行一次呼叫,如下所示:
FormAUsersList(List1,List2);
在您的代碼中,FormAUsersList 方法接收 newList 并立即將其替換為新的空串列,這沒有意義。FormAUsersList 應該看起來更像:
private List<Pieces> FormAUsersList(List<Pieces> list1, List<Pieces> list2)
{
List<Pieces> newList = new List<Pieces>();
for (int i = 0; i < list1.Count; i )
{
newList.Add(list1.PurchaseYear, list2.Whatever);
}
return newList;
}
請注意,此代碼假定 list1 和 list2 的長度相同。
uj5u.com熱心網友回復:
你有一個錯誤。每次您創建一個新串列但不回傳它。
您應該洗掉 newList = new List(); 外部
private List<Pieces> FormAUsersList(List<Pieces> list, List<Pieces> newList)
{
newList = new List<Pieces>(); // remove from here
.....
}
和代碼應該是
newList = new List<Pieces>();
FormAUsersList(List1,newList);
FormAUsersList(List2,newList);
恕我直言,您也可以使用 linq
var itemsToAdd=list.Where(i=> year <= i.PurchaseYear && type == i.Type);
if(itemsToAdd!=null) newList.AddRange(itemsToAdd);
uj5u.com熱心網友回復:
好的,我重構了你的代碼。我不得不洗掉您的表單代碼,因為它當然不會在本地編譯:
using Newtonsoft.Json;
namespace Pieces
{
public class Piece
{
public class Piece
{
public Piece(int purchaseYear, string type)
{
PurchaseYear = purchaseYear;
Type = type;
}
public int PurchaseYear { get; set; }
public string Type { get; set; }
// EDIT:
public override string ToString()
{
return $"Purchased year: {PurchaseYear} Type: {Type}";
}
}
public partial class Form1
{
List<Piece> List1 = new List<Piece> { new Piece (2022, "????"), new Piece(2021, "????") };
List<Piece> List2 = new List<Piece> { new Piece(2020, "????"), new Piece(2019, "????") };
List<Piece> newList;
public Form1()
{
accordingToUsersSpecificationsToolStripMenuItem_Click(null, null); // Just so it compiles.
}
private void accordingToUsersSpecificationsToolStripMenuItem_Click(object sender, EventArgs e)
{
var year = 2018;
var type = "????";
// Create the list outside your FormUsersList() method, No need to pass newlist to it.
newList = new List<Piece>();
FormAUsersList(List1);
FormAUsersList(List2);
// Now the same with LINQ
newList = new List<Piece>();
FormAUsersListWithLinq(List1, year, type);
FormAUsersListWithLinq(List2, year, type);
// Or just LINQ
newList = List1
.Union(List2)
.Where(x => x.PurchaseYear >= year && x.Type == type)
.ToList();
// EDIT:
newList.ForEach(x => Console.WriteLine(x));
}
/// <summary>
/// Form a list according to selected parameters by the user that we call
/// 2 times to form a complete new list
/// </summary>
private void FormAUsersList(List<Piece> list)
{
int year = 2018; // Just so it compiles
string type = "????"; // Just so it compiles
for (int i = 0; i < list.Count; i )
{
if (year <= list[i].PurchaseYear && type == list[i].Type)
{
newList.Add(list[i]);
}
}
}
private void FormAUsersListWithLinq(List<Piece> list, int year, string type)
{
newList.AddRange(list.Where(x => x.PurchaseYear >= year && x.Type == type));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/442590.html
