Is there special way declaration class in VB.NET -
i newbie in vb.net. when read document on msdn (https://msdn.microsoft.com/en-us/library/vstudio/bb534304%28v=vs.100%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1) see following code (excerpt):
dim pets new list(of pet)(new pet() _ {new pet {.name = "barley", .age = 8}, _ new pet {.name = "boots", .age = 4}, _ new pet {.name = "whiskers", .age = 1}, _ new pet {.name = "daisy", .age = 4}})
i don't see definition of class pet before. can explain me?
this confusing, because vb.net re-uses tokens in case mean different things. i'll spread code on several lines , explain happens @ each point.
dim pets new list(of pet)
that creates list object hold pet objects.
(
we calling constructor list(of pet) type. there several overloads list(of t) constructor, code here uses this one.
new pet()
this gets tricky. code not creating new pet
object. in context, ()
tokens indicate array.
_
that moves code next line
{
this indicates in collection initializer, used populate new array/list/etc items.
new pet
this time creating new pet object.
{
this indicates we're using object initializer. it's worth mentioning here cue compiler has difference between new pet() array , new pet() object in case use of initializers, , difference between 2 kinds of initializer with
keyword. can make code in vb confusing, if you're not used it.
.name = "barley", .age = 8}
assign values properties new object, , finish object initializer.
, _
move next item in collection initializer, , continue on line.
new pet {.name = "boots", .age = 4}, _ new pet {.name = "whiskers", .age = 1}, _ new pet {.name = "daisy", .age = 4}
just previous code, creates 3 more new pet objects , sets values of properties. note there no comma (,) after last entry.
})
now close collection initializer, , complete call list constructor.
Comments
Post a Comment