Converting strings to objects - How do you do this?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Greeting Everyone,

Can you look at this code? It's a class module that I made so I can make our application more modular.

In the code there are 3 lines of code which have this as part of it:
FormBrowseAttendance.ComboBoxLookupStudent.

Could you tell me how I can change the coding to refer to this portion of code using these parameters:
pComboBoxName
pNameOfForm

I'm assuming these need to converted is some way but I don't know how to do it.

Thanks.

Truly,
Emad

VB.NET:
Imports System.Data.OleDb

Public Class Utilities
    ' Command object to be used in select, insert, delete and update statements.
    '---------------------------------------------------------------------------
    Dim objCommand As OleDbCommand

    ' Declare and Create the connection object to use an SQL query.
    '--------------------------------------------------------------
    Dim objConnection As New OleDbConnection(My.Settings.ISGL_Database)

    ' Declare a DataAdapter.
    '-----------------------
    Dim objDataAdapter As OleDbDataAdapter

    ' Declare a DataSet.
    '-------------------
    Dim objDataSet As New DataSet

    Public Sub FillPureComponentsComboBoxFromDataAdapter( _
        ByVal pComboBoxName As String, _
        ByVal pSqlQuery As String, _
        ByVal pNameOfForm As String, _
        ByVal pDisplayMember As String, _
        ByVal pValueMember As String)

        ' Set up a command to execute the SQL query.
        '-------------------------------------------
        objCommand = New OleDbCommand(pSqlQuery, objConnection)
        objCommand.CommandType = CommandType.Text

        ' Load data into the ComboBox
        Try
            objDataAdapter.SelectCommand = New OleDbCommand(pSqlQuery, objConnection)

            objDataAdapter.Fill(objDataSet)

            FormBrowseAttendance.ComboBoxLookupStudent.DataSource = objDataSet.Tables(0)

            FormBrowseAttendance.ComboBoxLookupStudent.DisplayMember = pDisplayMember

            FormBrowseAttendance.ComboBoxLookupStudent.ValueMember = pValueMember

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub
End Class
 
Last edited:
An idea is to pass the control reference (ByVal combo As ComboBox) and use the 'combo' variable in the method code.
 
Hi John,

Thanks for the reply.

I tried this:

VB.NET:
    Public Sub FillPureComponentsComboBoxFromDataAdapter( _
        ByVal pComboBoxName As PureComponents.EntrySet.Controls.ComboBox, _
        ByVal pSqlQuery As String, _
        ByVal pNameOfForm As Form, _
        ByVal pDisplayMember As String, _
        ByVal pValueMember As String)

And this:

VB.NET:
            pNameOfForm.pComboBoxName.DataSource = objDataSet.Tables(0)

but I get this error:

VB.NET:
Error	1	'pComboBoxName' is not a member of 'System.Windows.Forms.Form'.	C:\Emad's Section\Development\Visual Studio\Projects\ISGL\ISGL\Utilities.vb	49	13	ISGL

Truly,
Emad
 
You are close....

VB.NET:
Public Sub FillPureComponentsComboBoxFromDataAdapter( _
         ByVal pComboBoxName As PureComponents.EntrySet.Controls.ComboBox, _
         ByVal pSqlQuery As String, _
         ByVal pDisplayMember As String, _
         ByVal pValueMember As String)

        ' Set up a command to execute the SQL query.
        '-------------------------------------------
        objCommand = New OleDbCommand(pSqlQuery, objConnection)
        objCommand.CommandType = CommandType.Text

        ' Load data into the ComboBox
        Try
            objDataAdapter.SelectCommand = New OleDbCommand(pSqlQuery, objConnection)

            objDataAdapter.Fill(objDataSet)

           [B][COLOR="Red"] pComboBoxName.DataSource = objDataSet.Tables(0)

            pComboBoxName.DisplayMember = pDisplayMember

            pComboBoxName.ValueMember = pValueMember
[/COLOR][/B]
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

There really is no need to pass the Form to the function.
 
Back
Top