|
-
Jun 3rd, 2009, 06:58 PM
#1
Thread Starter
Lively Member
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
-
Jun 3rd, 2009, 07:51 PM
#2
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.
-
Jun 3rd, 2009, 08:01 PM
#3
Thread Starter
Lively Member
Re: AddHandler to Dynamic Object
Thanks, I've changed my reference to SerialPort and I now have a .DataReceived method.
Cheers!!
-
Jun 3rd, 2009, 08:25 PM
#4
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.
-
Jun 3rd, 2009, 08:42 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|