C# Custom LinkedList Derived Classes, Constructor Initializations - Learning C# - Part 2
To allow our derived class to inherit from our Inventory class, after defining the LinkedList class, we add a colon and the name of the base class "Inventory." Upon typing the word "Inventory" from the keyboard, type "Ctrl" + "." -- this key combination opens up a drop down asking you to "Implement abstract class Inventory." Select that option and your class is created with the base class overrideable methods, in this case "Category."
public class LinkedList : Inventory
{
public override string Category()
{
throw new NotImplementedException();
}
}
We define a private string inventoryCategory, to hold our assigned values. When we need to retrieve these values, we use the Category method. On your own, do some research on using Get/Set Properties instead of methods.
public class LinkedList : Inventory
{
// custom linked list
private string inventoryCategory;
public override string Category()
{
return inventoryCategory;
}
// self-referencing members (same name as its class)
private LinkedList Head; // pointer to the top of the stack
// the last node added to the list
private LinkedList Next; // index to locate an item in the list
private int size; // how many records in the list
public LinkedList() // constructor / default values
{
Head = null; // no items have been added
Next = null;
inventoryCategory = string.Empty;
size = 0; // 1 based - list starts out as empty
}
We create two self-referencing members, meaning
the type is the same name as their class: "private
LinkedList Head" and "private LinkedList Next". The first
points to the most recent node added to the list or the top
of the stack. The second points to the next node on the list
that was just previously added so that we can traverse
through the list. Once we traverse to the next node, it
again has its own Next node, to point to the previous node,
and so forth.
The "private int size" statement
defines an integer used to keep track of how many records
are in the list.
Next we define our
constructor: "public LinkedList()." "Constructors: are class methods that are executed when an object of a
class or struct is created. They have the same name as the
class or struct, and usually initialize the data members of
the new object."
True to our definition, we
initialize the Head and Next to null. We set our
inventoryCategory to empty using "string.Empty." We could
have just as easily set our inventoryCategory to empty by
assigning it a value of "" (2 quote marks).
We
set our size to 0. Our list will be 1 based and increment to
1 when the first node is added.
In Part 3 of our
series, we will define our Push and Pop methods.
- Download Source Code on CodePlex: CSLinkedList.zip
- C# Custom LinkedList Console Application and Abstract Base Class and Method - Learning C# - Part 1
- C# Custom LinkedList Derived Classes, Constructor Initializations - Learning C# - Part 2
- C# Custom LinkedList Push and Pop Methods - Learning C# - Part 3
- C# Custom LinkedList PrintLifo method and C# Operators - Learning C# - Part 4
- C# Custom LinkedList PrintFifo method and GetNode by Position - Learning C# - Part 5
- C# Custom LinkedList FindItems() method - Learning C# - Part 6
- C# Custom LinkedList Main() method - Learning C# - Part 7
[CSLINKEDLIST]