Home c# C # lists and class objects

C # lists and class objects

Author

Date

Category

There is a written class of a list with basic functions, and there is a Point class which is added to the list itself and has its own methods. After adding a list of multiple objects, in the cycle I am trying to call the Print method at the Point class, but nothing happens, can anyone know how to solve this problem?

Class node

Public Class Item & lt; t & gt;
{
  Private T Data = Default (T);
 // This stored in the list of the list
  Public T Data {
    get {Return Data; }
    set {if (Value! = NULL)
        Data = Value;
    }
  }
  // Next List Cell
  Public Item & LT; T & GT; Next {Get; SET; }
  Public Item (T DATA)
  {
    Data = DATA;
  }
  Public Override String Tostring ()
  {
    Return Data.Tostring ();
  }
}

List and class Point

Public Class List & lt; T & GT; : Ienumerable
{// First List Element
  Public Item & LT; T & GT; Head {Get; Private SET; }//head
  // Last List Element
  Public Item & LT; T & GT; Tail {Get; Private SET; }//tail
  // Number of Elements
  Public int Count {Get; Private SET; } // count of elements
  // Designers
  // Create an empty list
  Public List ()
  {
    Clear ();
  }
  // Create a list with an initial element
  Public List (t DATA)
  {
    SetHeadandTail (DATA);
  }
  // Add data to the end of the list
  Public Void Add (T DATA)
  {
    if (tail! = NULL)
    {
      var imem = new item & lt; t & gt; (data);
      Tail.next = item; // tail pointer hooked for the next
      Tail = item;
      COUNT ++;
    }
    ELSE.
    {
      SetHeadandTail (DATA);
    }
  }
  // Removal
  Public Void Delete (T DATA)
  {
    if (Head! = NULL)
    {
      if (head.data.equals (DATA))
      {
        Head = head.next;
        Count--;
        Return;
      }
      var current = head.next;
      var previos = Head;
      While (Current! = NULL)
      {
        if (current.data.equals (DATA))
        {
          Previous.Next = current.next; // Pointer Change
          Count--;
          Return;
        }
        Previous = Current;
        current = current.next;
      }
    }
  }
  Private Void SetHeadandTail (T Data)
  {
    var imem = new item & lt; t & gt; (data);
    Head = Item;
    Tail = item;
    Count = 1;
  }
  // Get the listings of all list items
  Public Ienumerator GETENUMERATOR ()
  {
    VAR Current = Head;
    While (Current! = NULL)
    {
      Yield Return Current.data;
      current = current.next;
    }
  }
  Public Override String Tostring ()
  {
    RETURN "LINKED LIST" + COUNT + "ELEMENTS";
  }
  Public Void PrintPoint ()
  {
    Point P = New Point ();
    Console.WriteLine ("{0} {1}", p.x, p.xname);
  }
  // Cleaning the list
  Public Void Clear ()
  {
    HEAD = NULL;
    Tail = NULL;
    Count = 0;
  }
  // add to the top of the list
  Public Void AppendHead (T Data)
  {
    Var Item = New Item & LT; T & GT; (DATA)
    {
      Next = Head.
    };
    Head = Item;
  }
}
Public Class Point
{
  Public Point ()
  {
  }
  Public Point (Int XX, String Str)
  {
    X = xx;
    Xname = str;
  }
  Public int x {get; SET; }
  Public String XName {Get; SET; }
  Public Void Print ()
  {
    Console.WriteLine ("X" + X + "NAME" + XName);
  }
}

Maine function

static void main (String [] Args)
  {
    Var List = New List & LT; Point & GT; ();
    List.Add (New Point (1, "Dadadad"));
    List.Add (New Point (2, "Dadadad"));
    List.Add (New Point (3, "Dadadad"));
    Foreach (VAR P IN LIST)
    { 
// here is the problem
       p.
     }
   }

Answer 1

You have implemented a necessary (non-Generic) Interface IEENMERABLE . The compiler cannot withdraw from IEnumerable type of object of the object. You need or implement ienumerable & lt; T & GT; , or clearly type specify:

foreach (Point P in List)
{
   p.print ();
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions