CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2012
    Posts
    18

    Typecasting Object to Struct.

    I have a linked list that takes any object, and puts it a struct with some other params:

    Let's say:

    public VList(Object Data);

    passes it to

    public struct VData
    {
    public double ID;
    public Object Data;
    }
    So when you want to retreive it you create a struct, pass it as a ref, and take the struct.data part.

    Lets say:


    struct Vals
    {
    public string a;
    public string b;
    public string c;
    public string d;
    }

    [ creating instances and stuff]

    List.Add(vals);

    List.Find(id, ref vdata);

    vals = (Vals) vdata.Data;

    THis actually worked till I changed some code on the list's library (not really had to do with all these).

    Now it sends an InvalidCastException like "Specified cast is not valid."

    What's wrong with it?

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Typecasting Object to Struct.

    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.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Typecasting Object to Struct.

    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...
            }
        }
    }

  4. #4
    Join Date
    Oct 2012
    Posts
    18

    Re: Typecasting Object to Struct.

    Quote Originally Posted by TheGreatCthulhu View Post
    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

  5. #5
    Join Date
    Oct 2012
    Posts
    18

    Re: Typecasting Object to Struct.

    holly ****

    Sorry Cthulu, my fault.
    Somewhere in one of the node's constr. I missed to pass the obj corectly .
    Works fine now
    Last edited by dazibao; October 12th, 2012 at 11:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured