0

CODE:

Private ingredientProperties(,) As Integer = {{ingredient1.Location.X, ingredient1.Location.Y}, {ingredient1.Size.Width, ingredient1.Size.Height}} ' {{ingredient location X, Y}, {ingredient size X, Y}}
Private amountProperties(,) As Integer = {{amount1.Location.X, amount1.Location.Y}, {amount1.Size.Width, amount1.Size.Height}} ' {{amount location X, Y}, {amount size X, Y}}

Here I am declaring two 2-d array's in the class scope that contain the locations and sizes of two text boxes. I am pretty sure I am getting this error:

An unhandled exception of type 'System.InvalidOperationException' occurred in Recipe Manager.exe Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

because the location and sizes are not existant yet, are there any alternate ways to declare them?

3
  • 1
    Why don't you initialize the array once your other options have been initialized? It doesn't make any sense to try and do it before. What are you trying to accomplish? Commented Jul 20, 2014 at 20:06
  • I need the scope for the arrays to be my whole class, if I do it in the Form_Load event handler then it is not accessible by the rest of the class Commented Jul 20, 2014 at 20:09
  • 2
    The scope is determined by the declaration and not by the initialization. E.g. if you just have the line Private ingredientProperties(,) As Integer in your class and then do ingredientProperties = {{ingredient1.Location.X, ingredient1.Location.Y}, {ingredient1.Size.Width, ingredient1.Size.Height}} in your Form_Load the variable is initialized and it can be accessed like you want it. Commented Jul 20, 2014 at 21:08

1 Answer 1

1

Since I think I have understood your problem now, I will provide an example on how you can initialize the array in your case:

You want a global variable in your class and initialize this with properties of other objects. To do this it is necessary for the other objects to be initialized first (or else you will get a NullReferenceException if you try to use them).

It is usually better to not initialize global variables inline, because you don't really know at which point every variable gets its values. It's better to use some initialization method that is called directly at the start of your application at a point that you exactly control. Then you can be sure of all the values of your variables.

I wrote some example code that also uses the Form.Load event. (Even better would be to also disable the Application Framework and use a custom Sub Main as the entry point, if you really want to control your startup sequence, but here it's just fine to use Form.Load.)

Public Class Form1

    'Global variables
    Private MyIngredient As Ingredient 'See: No inline initialization
    Private IngredientProperties(,) As Integer 'See: No inline initialization

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'At this stage, nothing is initialized yet, neither the ingredient, nor your array
        'First initialize the ingredient
        MyIngredient = New Ingredient

        'Now you can enter the values into the array
        With MyIngredient 'Make it more readable
            IngredientProperties = {{.Location.X, .Location.Y}, _
                                    {.Size.Width, .Size.Height}}
        End With
    End Sub
End Class

Public Class Ingredient
    Public Location As Point
    Public Size As Size
    Public Sub New()
        'Example values
        Location = New Point(32, 54)
        Size = New Size(64, 64)
    End Sub
End Class
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I figured out a way but this one's better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.