$1 Million dollars to whoever can figure this one out....

This problem's stumped me completely...take a look:

I defined the following function to create a ComboBox and add it to an arraylist:

Public
Function addComboBox(ByVal Text As String, ByVal Ops As ArrayList, ByVal parent As Object, ByVal enabled As Boolean, ByVal x As Integer, ByVal y As Integer) As ComboBox
   Dim newCbo As New ComboBox
   newCbo.Text = Text
   newCbo.AutoSize = True
   newCbo.Parent = parent
   newCbo.Left = x
   newCbo.Top = y
   newCbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
   newCbo.AutoCompleteSource = AutoCompleteSource.CustomSource
   newCbo.Enabled = enabled
   newCbo.Show()
   Ops.Add(newCbo)
   Return (newCbo)
End Function

It works perfectly, but ONLY IN ONE PROJECT. When I copied the code to another project for use in a different program, it returns a NullReferenceException. If I disable the "Ops.add(newCbo)" line, it creates the combobox with all of the defined properties defined...

what's going on


[EDIT]
upadate: in the project where it doesn't work, I tried running the following code:

Ops.add("hello, world")

This causes a NullReferenceException as well...but the same command works fine in the other project...




Answer this question

$1 Million dollars to whoever can figure this one out....

  • tikira

     Dustin_H wrote:

    The Ops variable is passed ByVal.  If you intend to pass an arraylist from another function to add the item to the list from inside the sub, then declare ops as ByRef.





    Public Function addComboBox(ByVal Text As String, ByRef Ops As ArrayList, ByVal parent As Object, ByVal enabled As Boolean, ByVal x As Integer, ByVal y As Integer) As ComboBox

     


    You should also make sure ops is a value.





    If Not (Ops Is Nothing) Then Ops.Add(newCbo)

     

    Your second suggestion is correct.  Your first is incorrect.  If you pass a reference type object by value, you can still edit the object and have those changes reflected in the original object when the method completes.  What you can't do is assign a different object to the variable.  This is because when you pass a reference by value, a local copy of the reference is made, but the copy still refers to the same object.  Test this code to see what I mean.


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim al1 As New ArrayList(New Integer() {1, 2, 3})

            MessageBox.Show(al1.Count.ToString())

            Me.AddItemToArrayListByVal(al1, 4)

            MessageBox.Show(al1.Count.ToString())

            Me.ReassignArrayListByVal(al1)

            MessageBox.Show(al1.Count.ToString())

            Dim al2 As New ArrayList(New Integer() {1, 2, 3})

            MessageBox.Show(al2.Count.ToString())

            Me.AddItemToArrayListByRef(al2, 4)

            MessageBox.Show(al2.Count.ToString())

            Me.ReassignArrayListByRef(al2)

            MessageBox.Show(al2.Count.ToString())
        End Sub

        Private Sub AddItemToArrayListByVal(ByVal al As ArrayList, ByVal o As Object)
            al.Add(o)
        End Sub

        Private Sub AddItemToArrayListByRef(ByRef al As ArrayList, ByVal o As Object)
            al.Add(o)
        End Sub

        Private Sub ReassignArrayListByVal(ByVal al As ArrayList)
            al = New ArrayList
        End Sub

        Private Sub ReassignArrayListByRef(ByRef al As ArrayList)
            al = New ArrayList
        End Sub

     


  • G. Rahul

    The Ops variable is passed ByVal.  If you intend to pass an arraylist from another function to add the item to the list from inside the sub, then declare ops as ByRef.



    Public Function addComboBox(ByVal Text As String, ByRef Ops As ArrayList, ByVal parent As Object, ByVal enabled As Boolean, ByVal x As Integer, ByVal y As Integer) As ComboBox

     


    You should also make sure ops is a value.



    If Not (Ops Is Nothing) Then Ops.Add(newCbo)

     



  • Afterburn

    I take visa and mastercard, no personal cheques :)

  • Shaik Pakeer

    ...one million dollars to you, sir Big Smile

  • $1 Million dollars to whoever can figure this one out....