INTRO:一直在寻找这个答案,尽管有几次搜索,但在归档中没有找到它。要么是太简单的问题,要么是做一些违反C#的规律的东西#从双向链表中的对象返回一个变量
我是新来的双向链表,他们正在做我的头!
我有问题得到一个双向链表,从列表中的对象吐出一个变量。
我试图简化一个新文件中的问题来解决我的头,但我只是找不到正确的语法来查找DLL中的对象内的变量。
我已经将它分解为DLL中的五个对象,每个对象都有一个标题。 现在我想查找一个对象并返回它的标题。 但是改为:aaaaaaaaaaaaaaaaaaarrrgh!我到底在做什么错了?
namespace ADS
{
public partial class DLL_Generic_Object : Form
{
public DLL_Generic_Object()
{
InitializeComponent();
}
//create class
public partial class Weapon
{
string Title;
public Weapon(string title)
{
this.Title = title;
}
}
//create objects;
Weapon object1 = new Weapon("Revolver");
Weapon object2 = new Weapon("Candlestick");
Weapon object3 = new Weapon("Lead Pipe");
Weapon object4 = new Weapon("Rope");
Weapon object5 = new Weapon("Knife");
public class Structures
{
public string getTitle(LinkedList<Object> nameofDLL)
{
string gotTitle = "How do I Find the Title variable of an Object in the DLL?";
return gotTitle;
}
}
//create an object Doubly Linked List
LinkedList<object> weapons = new LinkedList<object>();
private void btnExecute_Click(object sender, EventArgs e)
{
PrintNodes(weapons); //This will show the DLL is empty.
//Add nodes to the list
weapons.AddFirst(object1); //add a node to the front of the list
//Add a node after a specific node.
weapons.AddAfter(weapons.Find(object1), object2);
weapons.AddAfter(weapons.Find(object2), object4);
//Add a node before a specific node.
weapons.AddBefore(weapons.Find(object4), object3);
//Add a node to the end of the list
weapons.AddLast(object5);
PrintNodes(weapons); // This will show the DLL has 5 Nodes.
// Find the value of a node
}
public string FindTitle(LinkedList<Object> nameofDLL)
{
// initialise a Structures class
Structures structure = new Structures();
// Find the value of a node
string value = structure.getTitle(weapons) + "\r\n";
return value;
}
public void PrintNodes(LinkedList<object> values)
{
if (values.Count != 0) //check if there are any nodes in the list
{
txtOutput.Text += "The number of nodes is: " + values.Count.ToString() + "\r\n";
txtOutput.Text += FindTitle(weapons);
}
else
txtOutput.Text += "The Doubly Linked List is empty" + "\r\n";
}
}
}
当您不知道列表中对象的类型时使用'object'。如果列表中只包含武器,为什么不是'LinkedList'? –
公平的电话。我试图保持它的通用性,但如果它工作的话,很乐意将LinkedList更改为! –
它不会因为更改为而摔倒,所以非常感谢。但我仍然需要知道如何从DLL中返回标题值。我不明白我要去哪里错... –