C
cv
How to cast a System.Object type to a class instance?
For example, Form1 has a Listbox. I create a custom class MyClass and
add several instances of that class to the Listbox.
When the user clicks the item displayed in the listbox, a new second
form is instantiated, Form2. Form2 has a public property of type
MyClass called MyObject, and I want to pass the object selected in the
Listbox on Form1 to the public property MyObject.
The problem is, the object selected in Form1.Listbox1 is of type
System.Object and Form2.MyObject is of type MyClass.
So when Option Strict is on, this will fail:
Form2.MyObject = ListBox1.SelectedItem
It will work if I make the property a SystemObject and turn off Option
Strict to allow late binding. But what if I am on an assignment that
requires me to leave Option Strict on?
TIA
CV
For example, Form1 has a Listbox. I create a custom class MyClass and
add several instances of that class to the Listbox.
When the user clicks the item displayed in the listbox, a new second
form is instantiated, Form2. Form2 has a public property of type
MyClass called MyObject, and I want to pass the object selected in the
Listbox on Form1 to the public property MyObject.
The problem is, the object selected in Form1.Listbox1 is of type
System.Object and Form2.MyObject is of type MyClass.
So when Option Strict is on, this will fail:
Form2.MyObject = ListBox1.SelectedItem
It will work if I make the property a SystemObject and turn off Option
Strict to allow late binding. But what if I am on an assignment that
requires me to leave Option Strict on?
TIA
CV