How to skip declaring temporary variable for a custom class

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
VB.NET:
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?
 
What if i want to declare an array of the custom type aDetail?

VB.NET:
Dim XYZ(2) as aDetail
XYZ(0) = new aDetail With { .A="xxx", .B="yyy" }

Or can i declare all the data in a big bracket during the Dim line? like how you can do it with a string array.
 
i just cant seem to figure out how to do it for the custom type. Can u give me an example? How do you declare the XYZ()?
 
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"})
 
Yes i know how to do it when its a single variable, but i am talking about an array .

VB.NET:
Dim XYZ(1) as aDetail
XYZ(0) = new aDetail With { .A="xxx", .B="yyy" }
XYZ(1) = new aDetail With { .A="aaa", .B="yyy" }

can i do it something like

VB.NET:
Dim XYZ() as aDetail = New aDetail() With {{.A="xxx",.B="yyy"},{.A="aaa", .B="yyy"}}
 
Surely you know how to initialise an array, e.g.
myArray = New SomeType() {something, soemthingElse, yetAnotherThing}
or, in later versions of VB:
myArray = {something, soemthingElse, yetAnotherThing}
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.
 
Back
Top