How to properly set a public variable?

Starfleeter

Member
Joined
Oct 31, 2010
Messages
5
Programming Experience
Beginner
Alright, I can't figure out for the life of me to how to do this but this is what I'm trying to do. I want Val(InitialStrength.Text) to be set as the variable iST so I'm typing this at the top of the page before all my subs:
VB.NET:
Public Class CharacterSheet
    Public iST As Integer = Val(InitialStrength.Text)

    Private Sub InitialStrength_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InitialStrength.TextChanged
        Dim a As Integer = Val(InitialStrength.Text)
        Dim c As Integer = Val(InitialStrength.Text) + Val(InitialPerception.Text) + Val(InitialEndurance.Text) + Val(InitialCharisma.Text) + Val(InitialIntelligence.Text) + Val(InitialAgility.Text) + Val(InitialLuck.Text)
        Dim b As Integer = 40
        If a > 10 Then
            InitialStrength.Text = 10
        ElseIf (a < 1) Then
            InitialStrength.Text = 1
        Else
            InitialStrength.Text = Val(InitialStrength.Text)
        End If
        If (b - c) >= 0 Then
            lblSPECIALTotal.Text = (b - c)
        End If
        If chkStrengthImplant.Checked = True Then
            FinalStrength.Text = Val(InitialStrength.Text) + 1
        Else
            FinalStrength.Text = Val(InitialStrength.Text)
        End If
        If Val(FinalStrength.Text) > 10 Then
            FinalStrength.Text = 10
        End If
    End Sub

but every time I do that and try to compile without even using the variabe, I get this error:

"An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."

I apologize if the solution is something minor but I'm just starting with VB.net and trying to learn it on my own and I've noticed it's difficult to use help files, etc to find basic help.
 
Last edited:
The immediate problem is that the TextBox doesn't exist when your code is executed and you can't get the Text property of an object that doesn't exist.

To elaborate, when you create an instance of a class, the first thing that gets executed is all the field declarations, including any code to initialise them. After that, the constructor is executed. In a form, the constructor calls the InitializeComponent method, which is where all the designer-generated code is located. The InitializeComponent method is where all your controls get created and configured as per your actions in the designer. If your field is declared and initialised before InitializeComponent is executed and InitializeComponent is where your TextBox gets created, that means that your TextBox hasn't been created when you try to use its Text property to intialise your field.

In short, if you want to use a component that you added in the designer during the initialisation of a form, you need to either do so in the constructor, after the call to InitializeComponent, or in the Load event handler, which is the more usual.

Having said all that, what's the point in this case? The user hasn't entered anything into the TextBox at that stage so what's the point of getting its contents?
 
I have the value set to 5 as the default and it can be modified from there. I guess because I'm using the Designer rather than manually typing the values of everything in the properties of the designer. I'm just trying to find ways to simplify my coding so that I don't have huge strings of val(text<attribute>.Text) etc when doing arithmetic. I'll see if I can attach a zip file of my project so you can have an idea of what I'm trying to do.
 
Last edited by a moderator:
I have removed the attachment from your previous post. I apologise if I'm wrong as I didn't actually look but the size indicated that you had included the compiled output, which includes binary files. Attaching binary files is against forum rules so you must ensure that you delete the 'bin' and 'obj' folders before zipping up your project.

That said, attaching a whole project should be a last resort anyway. We don't need to download, unzip, open and run your project. We just need a full and clear description from you and for you to post the relevant code directly.

A better option would be to not set the Text of the TextBox in the designer but rather initialise the field to a literal value. You can then assign the value of the field to the Text of the TextBox in the Load event handler.

Also, you don't need to litter your code with Val(InitialStrength.Text). Any time you are going to use the same calculated value multiple times, you should calculate the value once, assign it to a local variable and then use that variable multiple times. You've already assigned the value of that calculation to the 'a' variable so you should replace every subsequent occurrence with 'a'.
 
I apologize about that. I've been using local variables but I'm wondering if it's possible for me to define a variable and be able to use those variables repeatedly in multiple sub procedures. I can't seem to get it to work probably because I don't know how to declare them properly or something. Also, I'm not a programmer, I literally just started doing this a couple nights ago trying to convert an excel sheet for generating a character into visual basic so I'm a bit lost.

A better option would be to not set the Text of the TextBox in the designer but rather initialise the field to a literal value. You can then assign the value of the field to the Text of the TextBox in the Load event handler.

I know that the Designer.vb initializes the components and I saw the code there for how it initializes the 5 into the text field. What code/sub procedure would I need to put in my own tab of code in order to initialize components and then use it as a public variable?

VB.NET:
    Private Sub InitializeComponents()
        Me.InitialStrength.Text = 5
    End Sub
    Public Sub InitialStrength_Text() Handles InitialStrength.Enter
        InitialStrength.Text = 5
    End Sub

    Public Sub InitialStrength_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InitialStrength.TextChanged
        Dim a As Integer = Val(InitialStrength.Text)
        Dim b As Integer = Val(InitialStrength.Text) + Val(InitialPerception.Text) + Val(InitialEndurance.Text) + Val(InitialCharisma.Text) + Val(InitialIntelligence.Text) + Val(InitialAgility.Text) + Val(InitialLuck.Text)
        Dim c As Integer = 40
        If a > 10 Then
            InitialStrength.Text = 10
        ElseIf (a < 1) Then
            InitialStrength.Text = 1
        Else
            InitialStrength.Text = a
        End If
        If (c - b) >= 0 Then
            lblSPECIALTotal.Text = (c - b)
        End If
        If chkStrengthImplant.Checked = True Then
            FinalStrength.Text = a + 1
        Else
            FinalStrength.Text = a
        End If
        If Val(FinalStrength.Text) > 10 Then
            FinalStrength.Text = 10
        End If
    End Sub

I don't even know if I did that write but I think that initiated the InitialStrength.Text component. Now, I would like to make a public iST = val(InitialStrength.Text) as well as do that with values for 6 other text boxes since I'm using them in tons of procedures (and knowing how to properly declare global variables will help with other things I'm going to try to do) but I can't figure out how to do it. No matter what I do, it gives me an object reference error. Where and what do I have to enter to do this and/or what do I have to fix?
 
hi,

as a beginner myself I understand some of your confusions, but let's try to make it simple.

VB.NET:
Public Class Form1

       Public iST As Integer = 5  '  See what I've done here, it's an actual value, and not the answer from a property of a control that still does not exist.
                                          '  your code can change that value later on based on the .text of some control.

and from a different form:
VB.NET:
Public Class Form2

        Sub AccessForm1()
                Form1.iST = 10
        End Sub

The object reference error it's not because of the Global vs Local variable paradigm, it's just because you're trying to grab the value out of a textbox that still does not exist.
got it?
 
Last edited:
Back
Top