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中返回標題值。我不明白我要去哪裏錯... –