Question

I am trying to print the content of my generic linked list called Transactions, but the output is "Task3.Transaction". The generic linked list has the class Transaction as its datatype because I am using the Transaction class to create nodes in the linked list.

Here is my code:

The part in my code where the issue is has ** on both sides of it.

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!";
    }
}
Was it helpful?

Solution

When you convert an instance of a type (except string) to a string, such as when you:

txtOutput.Text += "\r\n" + t;

the CLR (.NET runtime) will call the method ToString() on the passed object. This is a method that all types derived from System.Object (there are very few types in .NET not derived from Object) inherit.

But the default implementation just returns the name of the type.

You need to override Object.ToString() in your type. and return a more meaningful string.

Eg.

public class Transaction {
  //...
  public override string ToString() {
    // Guess field names from constructor:
    //  new Transaction(reader.GetInt32(0), reader.GetDateTime(1), reader.GetString(2), reader.GetString(3), reader.GetDouble(4))

    return String.Format("#{0}: {1} {2} {3} {4} {5}", id, timestamp, string1, string2, number);
  }
  // ...

Ideally an overflow talking an IFormatProvider and passing that through to formatting functions should also be present (and will be used by String.Format and such methods if available). Even better to implement IFormattable.

OTHER TIPS

Override the ToString method in the Transaction class. By default it outputs the name of the type. This happens implicitly when you call this:

txtOutput.Text += "\r\n" + t;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top