Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15246

VS 2019 [RESOLVED] Simplifying Object Initialization

$
0
0
Hi,

I have the following code:

vb Code:
  1. Dim cmd As New SqlCommand
  2.             cmd.CommandText = SqlInsertVendor
  3.             cmd.Parameters.Add("@job_number", SqlDbType.VarChar).Value = JobNo
  4.             cmd.Parameters.Add("@vendor_name", SqlDbType.VarChar).Value = Me.txtVendorName.Text
  5.             cmd.Parameters.Add("@vendor_code", SqlDbType.VarChar).Value = Me.txtVendorCode.Text

I get a message stating Object Initialization can be simplified, which changes to this:

vb Code:
  1. Dim cmd As New SqlCommand With {
  2.                 .CommandText = SqlInsertVendor
  3.             }
  4.             cmd.Parameters.Add("@job_number", SqlDbType.VarChar).Value = JobNo
  5.             cmd.Parameters.Add("@vendor_name", SqlDbType.VarChar).Value = Me.txtVendorName.Text
  6.             cmd.Parameters.Add("@vendor_code", SqlDbType.VarChar).Value = Me.txtVendorCode.Text

Why can't the Parameters form part of the "With" also?

Is there any documentation that details what properties each object can use in the "With" during Initialization?

Thank you.

Viewing all articles
Browse latest Browse all 15246

Trending Articles