[Gambas-user] instance inline vs declaration and later instanciation

Tobias Boege taboege at ...626...
Thu Aug 18 20:53:36 CEST 2016


On Thu, 18 Aug 2016, PICCORO McKAY Lenz wrote:
> i have a global variable:
> 
> Private varaible As New Collection
> 
> this declaration has a inline instanciation due the keyword "New" in the
> same line
> 
> whats the difference if i done as:
> 
> private variable as Collection
> 
> and later inside funtion:
> 
> variable = New Collection
> 
> Please! answer me!
> 

The first way ensures that the variable contains a valid reference to a
Collection object as soon as an object of the class you're writing is
instantiated. This happens even before the constructor of that object is
called.

In the second case there is certainly an interval in the life of your
object when the variable is Null and accessing it somewhere in your code
will lead to an error.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list