By the looks of it... everything is wrong with it...
Could you post the code that actually adds the elements to the linked list, so we can have a look?
The exception means that the Data object is not of the Vals type (I'm guessing it's actually a string). It seems to me that you have some misconceptions about what structs are, and how they should be used.
OK, here's a rough example of how you can implement a linked list in C#.
Code:
namespace LinkedListExample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Element[] elements = new Element[3];
for (int i = 0; i < elements.Length; ++i)
{
elements[i] = new Element();
elements[i].Value = "val" + i.ToString();
}
MyLinkedList ll = new MyLinkedList();
foreach (Element e in elements)
ll.Add(e);
for (int i = 0; i < ll.Count; ++i)
Console.WriteLine(ll[i].Value); // prints out: val0, val1, val2
}
}
public class Element
{
public string Value { get; set; }
public Element Next { get; set; }
}
public class MyLinkedList
{
private Element head = null;
public int Count { get; private set; }
public void Add(Element item) // see, no ref keyword required, as long as Element is class, instead of a struct
{
if (head == null)
head = item;
else
{
Element last = head;
while (last.Next != null)
last = last.Next;
last.Next = item;
}
Count++;
}
public Element this[int i] // indexer
{
get
{
int k = 0;
Element current = head;
while (k < Count)
{
if (i == k)
return current;
current = current.Next;
k++;
}
return null;
}
// you can implement set...
}
}
}
By the looks of it... everything is wrong with it...
Could you post the code that actually adds the elements to the linked list, so we can have a look?
The exception means that the Data object is not of the Vals type (I'm guessing it's actually a string). It seems to me that you have some misconceptions about what structs are, and how they should be used.
I'm not sure if I get what youre saying. The Data object has been passed a Vals.
Here's the code -if it's the first entry-:
public void Add(Object Data)
{
VData vdata = new VData();
vdata.Data = Data;
if (firstNode == null) //add in empty list
{
vdata.ID = 0;
VNode NVN = new VNode(vdata);
[...]
}
so when you take it back:
public bool Find(double Tid, ref VData data) //extenal use
{
VNode tmp = Search(Tid); //search for the specified id.
if (tmp != null) data = tmp.Data(); //this is the VData struct stored int he node.
else return false;
return true;
}
It actually worked..
I can get for example the ID usinf the vdata.ID, but the vdata.data [which in this case is a Vals structure] do not typecast
Bookmarks