0

Hello I have a code and blah blah blah... The problem is I have a maskedtextbox(s) in a row (12 of them to be exact) and i want to give them a specific value from array. Is it possible to do it by loop? I tried this: This is the declaration:

    Dim BiA11() As MaskedTextBox = New MaskedTextBox() {BiA11_1, BiA11_2, 
    BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, 
    BiA11_11, BiA11_12}

And this is the code later in the program:

    For index As Integer = 0 To 11
        BiA11(index).Text = ""
    Next

Yes I know now that i am pusshing nothing to the MaskedTextBox but this was only for my test. But the visual studio gives me this error:

System.NullReferenceException: Object reference not set to an instance of an object.

BiA11() – Nothing.

In the end I want to push to the textboxes strings from an array throgh loop (it will save a quantum of space because I have 132 of these textboxes.

Thank you for your time.

********************EDIT********************

So I found an answer First of all in the declaration i changed the array a little bit:

Dim BiA11() As MaskedTextBox = New MaskedTextBox(11) {}

The declaration takes a place in the Public Class Form1 Then in the sub that handels the first events of appication in the first lines I filled the array with the references of the MaskedTextBox('s)

 BiA11 = {BiA11_1, BiA11_2, BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, BiA11_11, BiA11_12}

And then finaly when it comes to deleting all of the MaskedTextBox('s) i just puted this for loop inside and it worked for me.

    For index As Integer = 0 To 11
        BiA11(index).Text = ""
    Next

No exceptions and the MaskedTextBox('s) are clear. Thank you for all your help. Hope that this will come in handy when someone will have similiar problem.

TomHardy
  • 33
  • 10

1 Answers1

0

The issue is presumably that you have got that first code snippet at the class level. In that case, it will be executed before the constructor, in which case your controls haven't been created yet. What you need to do is declare the array variable at the class level:

Private BiA11 As MaskedTextBox()

and then create and populate the array inside the Load event handler of the form:

BiA11 = {BiA11_1, BiA11_2, BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, BiA11_11, BiA11_12}
jmcilhinney
  • 40,280
  • 5
  • 23
  • 39