1

I'm trying to make a simple login program, but I'm having troubles with assigning a value to my two arrays "User()" and "Pass()". I have the following code on a form titled "frmCreate". This is the form I will be using to create the accounts.

Public Class frmCreate
Dim passs() As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    If Not (txtUser.Text = "") And Not (txtPass.Text = "") Then
        userCount = userCount + 1
        User(userCount) = txtUser.Text
        Pass(userCount) = txtPass.Text
    Else
        MsgBox("Please enter a username or password")
    End If
End Sub

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    passs(0) = "hi"
End Sub
End Class

The issue I'm getting is as follows: Error in array

The txtPass and txtUser are both text boxes, which can be edited by the user.

Any help would be much appreciated! (Also, I tried mucking around with the variables and all, the strings after the = sign aren't the problem, and when I set the "userCount" inside the brackets to "0" for example, it still returned the same error)

EDIT Added code as text, rather then image (image still there). Note the extra few lines at the end where I set a new array which I named passs() to "hi". The dim is also further up. If I am declaring my variables incorrectly, please let me know.

EDIT2 Ok, I changed my declaration of "User()" and "Pass()" to = {}. Now my problem is that I'm getting the error that the value is out of the bounds of the array. I understand that this happens when you try to call on a non existent value which is outside the arrays boundaries, but the array I set has no boundaries, and I'm just trying to give it a value, not call on one.

EDIT3 Ugh... Ok tweaked a bit, I've found that if I add an unused value to "User()" then it is able to replace it. So I can get the program to replace already existent values in arrays, but I can not get it to create new values in arrays.

Silver
  • 67
  • 9
  • 1
    Paste the code as text with proper formatting, but not as image, please. Also the error message text. – omegastripes Jun 23 '16 at 08:07
  • And if you start the array at 1 like this question?: http://stackoverflow.com/questions/24007320/array-index-starts-at-1-why – smoore4 Jun 23 '16 at 08:21
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Enigmativity Jun 23 '16 at 08:41
  • The array `User` hasn't been instantiated (it's currently `Nothing`). You just need to fix that. – Enigmativity Jun 23 '16 at 08:42
  • Thanks for that, but I'm still confused. I understand that the array "User" is nothing, but how do I fix that? I have it declared as: Public User() As String So it exists, but it's still nothing?? – Silver Jun 23 '16 at 09:07

3 Answers3

1

It would appear your User variable is NULL. I presume this is intended to be a list of strings or something similar?

You should check the place you are initializing this variable to make sure it is being created...

You could add an

If User Is Nothing

line at the start of the function to check this, and if the list is null at this point create it.

Milney
  • 5,876
  • 2
  • 16
  • 29
  • Thanks Milney, by nothing do you mean has no values assigned to it? Because that's what I'm trying to do here - assign a value to the variable. If null means the variable doesn't exist, then I have a problem. I declared my variables in a module, where I declared them as public: Public User() As String. Indeed, as I ran the program it underlined my array purple, and when I hovered over it, it simply said "Nothing". How would I fix this? – Silver Jun 23 '16 at 08:56
  • When a variable is `null` then no instance has been created for it. The check whether something is `null` in VB is done like `If variable Is Nothing`. It **does not mean** there are *no entries in the list*. It means that *there is no list*. – Thorsten Dittmar Jun 23 '16 at 09:32
  • If you are trying to add multiple user names to the User variable, it should be declared as an Array or List of Strings, not just a single String. Otherwise if you are just trying to store a single user name, you would User = xxx rather than User(userCount). – Milney Jun 23 '16 at 09:35
  • Please consult the documentation on Collections: https://msdn.microsoft.com/en-us/library/a1y8b3b3(v=vs.100).aspx – Milney Jun 23 '16 at 09:36
  • It is declared as an array: Public User() As String Unless my understanding of how an array is declared is out-dated/incorrect? It's very odd, because if I set the array to have two values eg, Public User() As String = {"val1", "val2"} then it sets the username and password just fine twice. But after it gets past the point where I have not manually given the array any values, it encounters a `null` value. – Silver Jun 23 '16 at 11:31
0

I have discovered what it is I was doing wrong. See, I thought when I declared a variable with empty parenthesis, it created a dynamic variable. This is obviously not the case. Since setting the size of my variable to the ludicrous size of 50,000, my program now works. Thanks to all who tried to help, and sorry for asking the wrong thing :/

Silver
  • 67
  • 9
0

Your problem here is that you only defined the variable array. You never reserved a spot for the array values in memory. Therefore your variable has a value of Nothing.

Before assigning the first item, you must assign room for it...

'We say we want an array
Dim passs() As String
'We ask the memory to reserve six spots for our array
passs = New String(5) {}

'We assign the first spot to the String "hi"
passs(0) = "hi"

However, if you don't know how many items you are going to add in your array, I strongly suggest you to use a List(of T) instead :

Dim passs As New List(of String)

passs.Add("hi")
Martin Verjans
  • 4,316
  • 1
  • 21
  • 41