Solved

Selected List Value as Object

Posted on 2013-11-19
3
330 Views
Last Modified: 2013-11-19
I don't even know the correct terminology for this, so I will try to explain.

I have a list box that I populate with values from an object (Item), one object per row.

lstThing

Now I want to select and use the value in that row to pass it as a reference to the object in another method.

Private Sub  xyz
' declare the variable
Dim thing As Thing
Dim addThing As AnotherThing

' I get an error with this:
getThing = lstThing.SelectedValue.ToString

'Do Stuff
addThing = _Machine.doSomething(getThing)

End Sub

Trying to pass the object associated with getThing to addThing, but get an error "Value of Type String cannot be converted..."  

VB suggests using cType to convert, but that doesn't work either.

I know it's something very elementary, but I can't figure it out!
0
Comment
Question by:ipeng
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39658698
Can you show us how you are populating the list?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39659598
If you put "Thing" in, then you need to cast back to "Thing" on the way out:

    Dim getThing As Thing = CType(lstThing.SelectedValue, Thing)

Replacing "Thing" with the correct type in both places.

*You're using SelectedValue().  Only use this if you've set DisplayMember() and ValueMember().  If you didn't set those then use SelectedItem() instead:

    Dim getThing As Thing = CType(lstThing.SelectedItem, Thing)
0
 

Author Comment

by:ipeng
ID: 39660197
Thanks for the help.    Using the CType with SelectedItem fixed it.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now