[Gambas-user] Class array - Null Object [SOLVED]

Phan Damily phandamily at yahoo.com
Mon Jan 29 09:16:27 CET 2018


Hello everyone. I'm new to Gambas and new to this mailing list. I earlier subscribed and posted to the depreciated mailing list, not knowing it was depreciated. Thanks to Benoît Minisini for pointing out my mistake and directing me to the correct list.
And thanks to nando_f for solving my problem, even though it was on the depreciated list. I'm posting the solution here to help others.
[begin original post]
Hello everyone. I'm new to Gambas and new to the mailing list. I'm using v3.10.0 on Linux.I programmed extensively with VB6 back in the day. So Gambas is pretty straight-forward and I'm excited about switching over from Xamarin's C#.

But I've ran into a situation with Gambas I can't resolve. I've been testing code ideas, trying to create a working one-dimensional class array. But keep getting "Null object" errors when trying to assign values to fields.

' Class1.class
    Public I As Integer
    Public S As String

' FMain.class
    Public TheArray As New Class1[]

Public Sub cmdTest_Click()
    Dim TheCounter As Integer
    TheArray = New Class1[]
    TheArray.Resize(100)
    For TheCounter = 0 To 100  
        TheArray[TheCounter].I = TheCounter            <---- Null object in FMain
        TheArray[TheCounter].S = CStr(TheCounter)
    Next
    For TheCounter = 0 To 100
        Print TheArray[TheCounter].I, TheArray[TheCounter].S
    Next
End

I'm sure the solution is pretty simple. But I haven't been able to find an answer or a working code example for this particular situation. Thanks for reading.
[end original post]
Again, thanks to guidance from nando_f, I've posted the solution to my problem below. Plus, I've included a workaround solution to the incorrect Resize in my original code, where TheCounter would give an error once it reached 100. This workaround method "TheArray.Resize(TheArray.Count + 1)" should suffice in situations where the required array size can't be known. 

Dim TheCounter As Integer  
TheArray = New Class1[]
 For TheCounter = 0 To 100
    TheArray.Resize(TheArray.Count + 1)
    TheArray[TheCounter] = New Class1
    TheArray[TheCounter].I = TheCounter
    TheArray[TheCounter].S = CString(TheCounter)
 Next

  For TheCounter = 0 To 100
    Print TheArray[TheCounter].I, TheArray[TheCounter].S
  Next

I hope this is helpful to someone. Thanks for reading.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20180129/ebe14065/attachment-0001.html>


More information about the User mailing list