none
Instantiating public properties and return them as Connections and SQLCommands RRS feed

  • Question

  • Now that I may have known how to instantiate public properties within a public class, how about this code snippet?

    Public Class ConnectOne  
      Public Property cn as new Connection
        Public Property cmd as new SQLCommand
    'One property for the SQLcommand here
    'One property for the SQLConnection here
    
    'Rest of appended code from last question and response.
    
    End Class

    Maybe this can be easier to fill in the blanks for any user who knows more regarding class creating and instantiating.

    Regards,

    JohnDBCTX


    jp

    Wednesday, February 5, 2020 3:28 AM

Answers

  • Hi John,
    try this demo:

    Imports System.Data.SqlClient
    
    Public Class Form69
    
      Private ClassOneObject As New ClassOne
    
      Private Sub Form69_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(ClassOneObject.DataGridViews(0))
        Dim dt As New DataTable
        Using cn = ClassOneObject.Connections(0)
          cn.ConnectionString = My.Settings.cnSQL
          Using da As New SqlDataAdapter("SELECT * FROM Tab1", cn)
            da.Fill(dt)
          End Using
        End Using
        ClassOneObject.BindingSources(0).DataSource = dt
        ClassOneObject.DataGridViews(0).DataSource = ClassOneObject.BindingSources(0)
      End Sub
    
      Public Class ClassOne
    
        Public Connect As New SqlConnection
        Public Cmd As New SqlCommand
    
        Public ReadOnly Property Connections() As SqlConnection()
          Get
            Return New SqlConnection() {Connect}
          End Get
        End Property
    
        Public ReadOnly Property Commands() As SqlCommand()
          Get
            Return New SqlCommand() {Cmd}
          End Get
        End Property
    
        Public BndSrc As New BindingSource
        Public DatGrd As New DataGridView
    
        Public ReadOnly Property DataGridViews() As DataGridView()
          Get
            Return New DataGridView() {DatGrd}
          End Get
        End Property
    
        Public ReadOnly Property BindingSources() As BindingSource()
          Get
            Return New BindingSource() {BndSrc}
          End Get
        End Property
      End Class
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by JohnDBCTX Wednesday, February 5, 2020 7:35 AM
    Wednesday, February 5, 2020 6:30 AM

All replies

  • Here is the appended code snippet:

    Public Class ClassOne
      
    Public Connect As New SqlConnection
        Public Cmd As New SqlCommand
    
    End Class

       Public ReadOnly Property Connections() As SqlConnection()
            Get
                Return New SqlConnection() {Connect}
            End Get
        End Property
    
        Public ReadOnly Property Commands() As SqlCommand()
            Get
                Return New SqlCommand() {Cmd}
            End Get
        End Property

    Now calling those public properties from this class is a bit tricky, since that they are object types.

    I am going to hope it is possible to call them from this public class.

    Regards,

    JohnDBCTX


    jp

    Wednesday, February 5, 2020 4:44 AM
  • Hi John,
    try this demo:

    Imports System.Data.SqlClient
    
    Public Class Form69
    
      Private ClassOneObject As New ClassOne
    
      Private Sub Form69_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(ClassOneObject.DataGridViews(0))
        Dim dt As New DataTable
        Using cn = ClassOneObject.Connections(0)
          cn.ConnectionString = My.Settings.cnSQL
          Using da As New SqlDataAdapter("SELECT * FROM Tab1", cn)
            da.Fill(dt)
          End Using
        End Using
        ClassOneObject.BindingSources(0).DataSource = dt
        ClassOneObject.DataGridViews(0).DataSource = ClassOneObject.BindingSources(0)
      End Sub
    
      Public Class ClassOne
    
        Public Connect As New SqlConnection
        Public Cmd As New SqlCommand
    
        Public ReadOnly Property Connections() As SqlConnection()
          Get
            Return New SqlConnection() {Connect}
          End Get
        End Property
    
        Public ReadOnly Property Commands() As SqlCommand()
          Get
            Return New SqlCommand() {Cmd}
          End Get
        End Property
    
        Public BndSrc As New BindingSource
        Public DatGrd As New DataGridView
    
        Public ReadOnly Property DataGridViews() As DataGridView()
          Get
            Return New DataGridView() {DatGrd}
          End Get
        End Property
    
        Public ReadOnly Property BindingSources() As BindingSource()
          Get
            Return New BindingSource() {BndSrc}
          End Get
        End Property
      End Class
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by JohnDBCTX Wednesday, February 5, 2020 7:35 AM
    Wednesday, February 5, 2020 6:30 AM
  • Neat!

    This shows users how to import its data from SQL client library.

    Regards,

    JohnDBCTX


    jp

    Wednesday, February 5, 2020 7:35 AM
  • For the record it's unwise to use public scoped connection and command as when done correctly there need to be assertions done to ensure the connection state is not closed or broken. Each time a connection is needed create a local instance for both the connection and command. For DataAdapter, this is fine to make this public.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, February 5, 2020 10:38 AM
    Moderator