Pergunta

I'm trying to create a typed-sized parameters array in VB.Net:

Dim parameters() As SqlParameter = New SqlParameter() _
        {
          New SqlParameter("@first_name", SqlDbType.VarChar, 50) {Value = "john"},
          New SqlParameter("@last_name", SqlDbType.VarChar, 50) {Value = "doe"},
          New SqlParameter("@age", SqlDbType.Int) {Value = 18},
          New SqlParameter("@id", SqlDbType.Int) {Value = 123}
        }

But VS says: Value' is not declared. It may be inaccessible due to its protection level

What's wrong with the code above?

Thanks!

Foi útil?

Solução

You need to use the VB syntax for object initializers:

Dim parameters() As SqlParameter = New SqlParameter() _
        {
          New SqlParameter("@first_name", SqlDbType.VarChar, 50) With { .Value = "john"},
          New SqlParameter("@last_name", SqlDbType.VarChar, 50) With { .Value = "doe"},
          New SqlParameter("@age", SqlDbType.Int) With { .Value = 18},
          New SqlParameter("@id", SqlDbType.Int) With { .Value = 123}
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top