Results 1 to 2 of 2

Thread: System.InValid Cast Exception -Pls guide

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Posts
    97

    System.InValid Cast Exception -Pls guide

    Hello everybody:
    I am creating a client server application Asynchronously. I am adding the asynchrous socket to the hashtable.

    IAsyncResult asyncSkt = m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
    sockettable.Add(connectId, asyncSkt);

    Now I want to access the connectId from the hashtable.

    Socket socket = ( Socket)sockettable[connectId];

    But I am getting System.InValidCastException... Can anyone tell me what is the problem?


    Pls guide...

    Rahil

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    so here, you get an IAsyncResult reference:
    PHP Code:
    IAsyncResult asyncSkt m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null); 
    here you add your IAsyncResult reference to a hashtable:
    PHP Code:
    sockettable.Add(connectIdasyncSkt); 
    and then finally you try to cast an IAsyncResult reference to a Socket:
    PHP Code:
    Socket socket = ( Socket)sockettable[connectId]; 
    You end up with an InvalidCastException because you can't cast an IAsyncResult to a Socket. Maybe reading up on asynchronous programming willhelp you out.

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