Solved

Selected List Value as Object

Posted on 2013-11-19
3
336 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 46
Close form "before" open 3 40
Write to file when application closes (by logoff) - VB.NET 1 31
VB.NET 2008 - SQL Timeout 9 24
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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