How to cast a System.Object type to a class instance?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top