C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How can I make a complicated linked list Posted by Dao Tran Minh on 7 Jun 2003 at 7:31 PM
I design a class "Item", and two classes "Table", "Chair" inherited from class "Item".
I want to make a linked list that each node can be "Table" or "Chair", so I decide to use the linked list of "Item", hope that each object can be declared like this:
Item one_node = new Table();
But then, I can not use the variable "one_node" as an object of "Table".
That is my problem. Anyone can help me to create a linked list where each node can be an object of some classes inherited from one class? And can the depth of the inheritance be more than one?
Sorry if my english is bad.
Report
Re: linked list of classes Posted by yuval on 29 Jun 2003 at 5:47 AM
: I design a class "Item", and two classes "Table", "Chair" inherited from class "Item".
: I want to make a linked list that each node can be "Table" or "Chair", so I decide to use the linked list of "Item", hope that each object can be declared like this:
: Item one_node = new Table();
: But then, I can not use the variable "one_node" as an object of "Table".
: That is my problem. Anyone can help me to create a linked list where each node can be an object of some classes inherited from one class? And can the depth of the inheritance be more than one?
: Sorry if my english is bad.
:

Report
Re: linked list of classes Posted by yuval on 29 Jun 2003 at 5:52 AM
This message was edited by yuval at 2003-6-29 5:54:40

Dao,

1. Your english is good.
2. try this:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ListOfClass
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(72, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(152, 104);
this.label1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

Table table1 = new Table();
Table table2 = new Table();
Chair chair1 = new Chair();
Chair chair2 = new Chair();

ArrayList items = new ArrayList();

items.Add (table1);
items.Add (table2);
items.Add (chair1);
items.Add (chair2);

table1.color=Color.Red ;
table2.color =Color.Black;
chair1.color = Color.Green;
chair2.color = Color.Brown;




foreach (Item i in items)
{
string type= i.GetType().ToString();

if (type=="ListOfClass.Table")
{
label1.Text += ((Table)i).color.ToString ()+"\n\n";

}

if (type=="ListOfClass.Chair")
{
label1.Text +=(((Chair)i).color).ToString() +"\n\n";
}
}


}
}

class Item
{

public Item()
{

}

}

class Table:Item
{
public Color color ;
public int width ;
public int length;
public string style;


public Table()
{
}



}

class Chair:Item
{
public Color color ;
public int width ;
public int length;
public string style;


public Chair()
{
}


}
}


3. Copy the text (cntl C).
4. Open a new window project. Delete everything.
5. paste the above code.
6. See if it fits your needs.

best of luck!





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.