Convert String Value to Object Item

Ryan Lashway 61 Reputation points
2023-02-09T15:21:55.1466667+00:00

I have to assume this is easy and I am just unaware of how to do it, but how can I convert a string value to an object.

I have about 50 checkboxes that I want to allow a user to click and hide a column in a grid and I wanted

I was hoping to change my below code to the following but it errors with a string value for the ckbox name.

Dim Val as string = "Mail"

Dim colName as string = "col_" + Val

Dim chkbox as string = "chkbox_" + Val

columnOptions(colName, chkbox)

My code looks like:

Private Function columnOptions(ByVal colName as String, ByVal chkbox as Object)
if chkbox.checked = True Then
dgview_adUserListing.Columns(colName).Visible = True
Else
dgview_adUserListing.Columns(colName).Visible = False
End Function


Private Sub chkbx_Mail_CheckChanged(sender as Object, e as EventArgs) handles chkbox_Mail.CheckedChange
Dim colName As String = "col_Mail"
Dim chkbox As object = "chkbx_Mail
ColumnOPtions(colName, chkbox)
End Sub
Developer technologies .NET Other
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-02-09T15:56:29.1266667+00:00

    This will probably work:

    Private Sub chkbx_Mail_CheckChanged(sender as Object, e as EventArgs) handles chkbox_Mail.CheckedChange
       Dim colName As String = "col_Mail"
       Dim chkbox As object = sender
       ColumnOPtions(colName, chkbox)
    End Sub
    

    Although it can be improved.

    But if you need Dim chkbox as string = "chkbox_" + Val, then try this:

    Dim chkbox as string = "chkbox_" + Val
    Dim chkbox_object = Controls.Find(chkbox, true)(0) 
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ryan Lashway 61 Reputation points
    2023-02-09T19:23:04.3133333+00:00

    I went with:

    Dim chkbox as Object = sender
    Dim colNameVal as String = Replace(sender.name, "chkbx_", "")
    Dim colName as String = "col_" + colnameVal
    If chkbox.Checked = True then
    dgview_adUserListing.Columns(colName).Visible = True
    Else
    dgview_adUserListing.Columns(colName.Visible = False
    End if
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.