Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox assignment of variable

If I put:

variableName = namecombobox.selectedItem

or

Dim variablename as type = namecombobox.SelectedIndex

Visual Studio gives me the error

Option Strict disallows conversions from object to string.

I can fix this by putting:

variableName = convert.ToString(namecombobox.SelectedItem)

Are all values contained in a combobox automatically treated as a non-string even when they are string values (in this case "Male" & "Female") and what is the correct way of assigning the value selected in a combobox to a variable?

like image 216
Wannabe Avatar asked Apr 21 '26 12:04

Wannabe


1 Answers

This is normal, the ComboBox.Items property is a collection of System.Object. You should use the item's ToString() method, just like ComboBox does to generate the visible text.

 Dim variableName As String = namecombobox.SelectedItem.ToString()

Or use CStr(), the VB.NET way.

like image 127
Hans Passant Avatar answered Apr 23 '26 01:04

Hans Passant