Private Class aDetail
Public Property A As String
Public Property B As String
End Class
Private Sub testSub(Aaa as aDetail)
End Sub
Dim XYZ as new aDetail
XYZ.A = "xxx"
XYZ.B = "YYY"
testSub(XYZ)
Could i send the "xxx" and "YYY" directly on the testSub(XYZ) line without declaring XYZ?
Yes, you can ditch the variable if you don't need it to refer to later. You must create the aDetail object and pass that, either using a With object initializer expression or by adding a constructor to the class that takes the two parameters. How to: Declare an Object by Using an Object Initializer (Visual Basic)
In your original example, you have to pass an aDetail object to the testSub method. That's the only requirement. Where that object comes from is of absolutely no concern to the method. When you call the method, you have to specify an expression that evaluates to an aDetail reference... ANY expression. Look at your own code:
VB.NET:
[B][U]Dim XYZ As New aDetail[/U][/B]
XYZ.A = "xxx"
XYZ.B = "YYY"
testSub(XYZ)
You know what the highlighted line does, right? It declares a variable, creates an object and assigns that object to the variable. It's shorthand for this:
VB.NET:
Dim XYZ As aDetail = [B][U]New aDetail[/U][/B]
Obviously the highlighted expression there must evaluate to an aDetail reference, otherwise what would it be assigning to the variable? Given that JohnH gave you a relevant link, you should know by now that you can use an object initialiser to create an object and set some of its properties in one line. Your code creates an object and then sets some of its properties so it can be collapsed using an object initialiser:
VB.NET:
Dim XYZ As New aDetail With {.A = "xxx", .B = "YYY"}
testSub(XYZ)
Once again, that is a shorthand for this:
VB.NET:
Dim XYZ As aDetail = [B][U]New aDetail With {.A = "xxx", .B = "YYY"}[/U][/B]
testSub(XYZ)
Once again, the highlighted expression must evaluate to an aDetail reference, otherwise what's being assigned to the variable. Didn't I say earlier that you can use any expression that evaluates to an aDetail reference as your method argument?
VB.NET:
testSub(New aDetail With {.A = "xxx", .B = "YYY"})
Just like the a method argument, an array element simply requires an expression that evaluates to an object of the appropriate type. That means that to initialise an array of type aDetail could look like this:
myArray = {New aDetail With {.A = "xxx", .B = "YYY"}, New aDetail With {.A = "xxx", .B = "YYY"}}
Now, if the expression on the right of that assignment can be assigned to a variable of type aDetail() then it must evaluate to aDetail(), so it must be able to be used anywhere that an expression of that type is expected, e.g. a method argument:
testSub({New aDetail With {.A = "xxx", .B = "YYY"}, New aDetail With {.A = "xxx", .B = "YYY"}})
You need to start thinking in terms of expressions. Getting the value of a variable is just an expression. Anywhere you can use the value of a variable, you can use any other expression that evaluates to the same type. The expression can be as complex as you like, as long as it evaluates to the appropriate type.
Ah thanks. I was only looking at the {.A = "xxx, .B = "yyy"} as the object that need to be initialize into the variable. Now i get it. each one need to have a new adetail structure before putting in the data.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.