?
Solved

Selected List Value as Object

Posted on 2013-11-19
3
Medium Priority
?
342 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

764 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