VB.Net MaskedTextBox array [duplicate] - arrays

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
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.

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}

Related

For control variable already in use compile error

I am trying to write a nested for loop in visual basic macro which runs on excel.
Here is my simplified code
Dim intVar(2) As Integer
For intVar(1) = 0 To 4
For intVar(2) = intVar(1) To 4
Var = Var + intVar(1) + intVar(2)
Next intVar(2)
Next intVar(1)
When I try to compile the code "For control variable already in use" compile error is thrown. Is there any solution for using array variables for For Loop or should I declare different variables for each For Loop?
There are some questions with the same tag but none of them for the array type control variables. If you help me I would be glad.
Thank you for your interest

Array from Request.Form in classic asp [duplicate]

This question already has an answer here:
String to Array with Classic ASP
(1 answer)
Closed 6 years ago.
EDIT: I now realise after the help from those who replied that my question was about whether Request.Form Data is a string in the same way that a$="FooBar" is a string and the Array command in Classic ASP.
I'm trying to make an Array from data submitted in a Form.
The form fields are dynamically created and have the same name "subj".
The Response.Write(Request.Form("subj")) produces:
"Welcome and Introduction, Talk1, Talk2 ,Interactive review of the conference"
When I check the TypeName or VarType Request.Form("subj") is a string. Then I code:
subjs = """" & Replace(Request.Form("subj"), ", ", """,""") & """"
subjects = Array(subjs)
With the intention to give:
subjs = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
subjects(0) = Welcome and Introduction
subject(1) = Talk1
subject(2) = Talk2
subject(3) = Interactive review of the conference
The problem is that what I actually get is:
subjs = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
subject(0) = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
For some reason the Array isn't correctly formed as there is no subject(1) subject(2) or subject(3).
If I physically copy and paste the output of subjs into my code, then Array works fine but I can't get the Array to work on Form Data.
I've tried using CStr and checked all of the quotation marks.
Why doesn't it work?
Thank you to those who took the trouble to reply. Whilst Split does work in fields without commas, SET var = Request.Form("subj") as per #Kul-Tigin, I think is the key but would be keen to hear other thoughts
Since the request collection values may contain commas, using a split can cause unexpected results.
Creating an array through the collection is more reliable.
Set subject = Request.Form("subj")
ReDim subjects(subject.Count - 1)
For i = 1 To subject.Count
subjects(i - 1) = subject(i)
Next
The Array function expects a comma-separated list: "words","stuff","foo", but what you get from a Request.Form is more like "words,stuff,foo".
Ultimately, though, it doesn't matter, because as you've noted in your comment, the appropriate function to use is Split.

String array not accepting a string

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.
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.
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 :/
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")

setting object properties value for object array in matlab

I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.
A simplified example of the problem.
classdef clsMyClass < handle
properties
dblMyProperty1
end
methods
function obj = clsMyClass()
end
end
end
And when running
vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;
vecMyArray.dblMyProperty1 = 1:100;
We get the following error:
??? Incorrect number of right hand side elements in dot name
assignment. Missing [] around left hand side is a likely cause.
Any help would be appreciated.
I see what you're trying to do now. Use disperse from the MATLAB File Exchange:
>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans =
1
>> vecMyArray(10).dblMyProperty1
ans =
10
You can use the deal function for exactly this purpose:
[vecMyArray.dblMyProperty1] = deal(1:100);
See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html
Edit: No you can't, actually; that'll set them to all be the vector 1:100.
I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.
Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:
So you'll need:
for ii=1:100
vecMyArray(ii).dblMyProperty1 = ii;
end
I know it's not satisfying, but I think it at least helps us to definitively understand this error.

In vb 6.0, how can we know if a dynamic array have been initialized? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I determine if an array is initialized in VB6?
How do I check for an object being Nothing in VB6?
In a situation where a function is returning a dynamic array as result, there is the possibility that the dynamic array was not initialized (For example, error in the execution). Is it possible to check this situation?
Function IsNothing() is not working, and UBound() is creating an error in this case.
For example:
Function find(results() As String)
[Definition here...]
End Function
[...]
Dim results() As String
find(results)
If UBound(results) > 0 Then '<-- This line will fail when results was not defined
[...]
Thanks in advance!
I ran into this same problem and was unable to find a clean way to do this. I ended up creating my own function to that implements a Ubound wrapped with an error handler. If it fails, I return a -1.
Private Function custom_UBound(ByRef ToTest() As String)
On Error GoTo errHandler
custUBound = UBound(ToTest)
Exit Function
errHandler:
custUBound = -1
End Function

Resources