On Mon, 05 Oct 2015, Moviga Technologies wrote: > I thought when one declares a fixed array, one should not be able to add > new elements to it? > But, this works: > > ' Gambas module file > > Public Sub Main() > > Dim sCarBrands As New String[5] > > sCarBrands = ["Volvo", "Saab", "Fiat", "Aston Martin", "Skoda"] > sCarBrands.Add("Audi") > sCarBrands.Add("VW") > Print sCarBrands.Join(", ") > > End >
Why do you think so? The thing ["Volvo", "Saab", "Fiat", "Aston Martin", "Skoda"] is an object of type String[], filled with the given data. It is no different from the object obtained by this code: Dim aCarBrands As New String[] With aCardBrands .Add("Volvo") .Add("Saab") .Add("Fiat") .Add("Aston Martin") .Add("Skoda") End With Also note that your declaration Dim aCarBrands As New String[5] is not perfect. There you allocate a new String array object with space for 5 strings. But immediately afterwards you replace that object with its empty space by a different object (the inline array). The important part here is that the assignment above does not fill the allocated space in the object but replaces the entire object. You should have written Dim aCarBrands As String[] aCarBrands = ["Volvo", ...] which does not waste time allocating useless memory. Also, if you ask me, I strongly prefer the equivalent notation Dim aCarBrands As New String[](5) over Dim aCarBrands As New String[5] to preallocate a number of elements in an array, because the former tells unambiguously that String[] is just an ordinary class whose constructor will receive the parameter 5. Regards, Tobi -- "There's an old saying: Don't change anything... ever!" -- Mr. Monk ------------------------------------------------------------------------------ _______________________________________________ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user