Results 1 to 5 of 5

Thread: AddHandler to Dynamic Object

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    65

    AddHandler to Dynamic Object

    Hi all,

    I have some code to create dynamic serial port objects, now Im trying to add an event handler to the object so I can capture the dataReceived event.

    But the vb designer is saying ".datareceived" is not en event of 'object' in this case serialports(dComPortNum)

    Code:
    Public serialports(5) As Object
    dComPortNum=1
    serialports(dComPortNum) = New SerialPort
    
    'This is the problem
    AddHandler serialports(dComPortNum).datareceived, AddressOf SerialPort_DataReceived
    Any help is greatly appreciated!

    Cheers

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: AddHandler to Dynamic Object

    That's exactly correct: the Object class has no DataReceived event. If your array is supposed to store SerialPort objects and only SerialPort objects, why have you specified the type of the elements as Object? You should be using the most specific type you can, which is SerialPort. That way serialports(dComPortNum) will be SerialPort reference rather than an Object reference and you will be able to access the DataReceived event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    65

    Re: AddHandler to Dynamic Object

    Thanks, I've changed my reference to SerialPort and I now have a .DataReceived method.

    Cheers!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: AddHandler to Dynamic Object

    In case what happened here is not completely clear to you, the compiler has no way to know exactly what type of object a variable will refer to at run time beyond the type of the variable. You may know that your variable will refer to a SerialPort object at run time but, if the variable itself is type Object, the compiler only knows for sure that it will be an Object. As such, the compiler will only allow you to access members of the Object type using that variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    65

    Re: AddHandler to Dynamic Object

    Yes I understand whats happening now, thanks for clarifying it for me!
    Cheers

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