Solved

Selected List Value as Object

Posted on 2013-11-19
3
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Expert Comment

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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

718 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