我試圖打印稱爲Transactions的通用鏈接列表的內容,但輸出是「Task3.Transaction」。通用鏈接列表具有Transaction類作爲其數據類型,因爲我使用Transaction類在鏈接列表中創建節點。如何打印通用鏈接列表的內容?
這裏是我的代碼:
在我的代碼的一部分,其中的問題是有* *在它的兩側。
private void button1_Click(object sender, EventArgs e)
{
LinkedList<Transaction> Transactions = new LinkedList<Transaction>(); //create the generic linked list
SqlConnection con = new SqlConnection(@"Data Source=melss002; Initial Catalog=30001622; Integrated Security=True"); //Connection string
int accNum = Int32.Parse(Microsoft.VisualBasic.Interaction.InputBox("Please enter account number", "Account Number")); //Prompt the user for account number
SqlCommand cmd = new SqlCommand("Select * From Transactions where AccountNo = " + accNum, con); //command to execute
con.Open(); //open the connection to the database
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)//Check if the table has records
{
while (reader.Read()) //read all records with the given AccountNo
{
Transaction Transaction001 = new Transaction(reader.GetInt32(0), reader.GetDateTime(1), reader.GetString(2), reader.GetString(3), reader.GetDouble(4)); //New Transaction node
Transactions.AddFirst(Transaction001);// add the node to the Doubly Linked List (Transactions)
}
}
else
{
MessageBox.Show("No records found");
}
PrintNodes(Transactions);
reader.Close();
con.Close();
}
public void PrintNodes(LinkedList<Transaction> values)
{
if (values.Count != 0)
{
txtOutput.Text += "Here are your transaction details:";
**foreach (Transaction t in values)**
{
txtOutput.Text += "\r\n" + t;
}
txtOutput.Text += "\r\n";
}
else
{
txtOutput.Text += "The Doubly Linked List is empty!";
}
}
你是否覆蓋了'Transaction類'的'.ToString()'方法? – jacqijvv
請添加'Transaction'類代碼。 – varocarbas