Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code:
VB
Public Function TestControl(c As Integer())
        Dim a As Control()
        Dim l As Point
        Dim F As New Form
        For i As Integer = 0 To c.Length - 1
            If c(i) Then
                a(i) = New TextBox
            Else
                a(i) = New ComboBox
            End If
            a(i).Width = 400
            a(i).Height = 40
            l.Y = 50 + (3 * i - 2) * a(i).Height / 2
            l.X = 150
            a(i).Location = l
            F.Controls.Add(a(i))
        Next
        F.Show()
        Return Nothing
   End Function

When I am trying to execute the Function the following Error appears:

Variable 'a' is used before it has been assigned a value. A null reference exception could result at runtime.

What I have tried:

I am trying to solve it but i couldn't succeed. any Idea would very helpful for me

Thanks
Posted
Updated 31-Jul-17 21:32pm

You dim a as Array of Control with no Dimension. That ist the Prior mistake.
But for what do you need this Array ? It's enough to instance your control and add it to the Form's Control-collection.

To correct your code (which has the wrong approach in the moment) I would like to know something more about the Goal ...

Additional after clarification :
VB
Public Sub CreateControls (c As Integer())
        Dim l As Point

        For i As Integer = 0 To c.Length - 1
            l.Y = 50 + (3 * i - 2) * 40  ' your Control-Height
            l.X = 150

            If c(i) = ??? Then  ' <- add the right value for comparing here
                dim myTB as New TextBox
                myTB.Width = 400
                myTB.Height = 40
                myTB.Location = l
                myTB.Name = "TextBox"+i.toString  ' <- important for later accessing
                me.Controls.Add(myTB)

            Else  ' if c(i)= ???
                dim myCB as New ComboBox
                myCB.Width = 400
                myCB.Height = 40
                myCB.Location = l
                myCB.Name = "ComboBox"+i.toString  ' <- important for later accessing
                me.Controls.Add(myCB)
            End If
        Next
   End Sub
 
Share this answer
 
v3
Comments
YahyaSh 1-Aug-17 4:29am    
Hello Ralf, you absolutely right. when i defined the length of control array, it works. but the idea is that i want it to be dynamic.

well, let me try to approach my goal:

I am working with dataset that is linked to a database. In the dataset i have many tables that are related where some field are related. for example I have a datagridview1 binded to table "Client" where each client has a unique account No "ACCNO". another table "Contracts" whith a field "AccNo". one client can have many contracts.

the function that i want to create is to add a new record and shall have parameter (Table as Datatable).

when i will call the function and new form shall be shown with number of controls which the same number of Datatable.columns.count....and here's the idea: each control type depend on the type of columns. for example: for the "Name" column i just need a Textbox. on the otherside, for Contracts.Columns("AccNo") I need a ComboBox where the display member are from Client.Columns("AccNo").

Hopefully, my idea is clear
Ralf Meier 1-Aug-17 5:01am    
Not complete clear but enough to start a discussion ...
I suggest the following :
- the DataGridView and the Connection to your Database is part of your Form - so also this method should better be part of your Form.
- I suppose you have a Button or something similar which loads the Database. When loaded you start your method which creates the required controls

Based on this the code could look like as shown above (I will improve my Solution) ...
YahyaSh 1-Aug-17 5:26am    
Ralf,

I worked it out as you proposed. it works perfectly.

Many thanks.
Here is an example of working with groups of objects - Controls in this instance...
VB
Private controls As List(Of Control) = New List(Of Control)

Sub AddControls()

    controls.Add(New PictureBox)
    controls.Add(New TextBox)
    controls.Add(New Label)

End Sub
 
Share this answer
 
Well, the error message is clear. You declared the array
Quote:
Dim a As Control()
but never created it.


The statement
VB
a = new Control(c.Length){}
is missing in your code.

In any case, I would follow Graeme_Grant suggestion and use instead a dynamic container, like the List.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900