Visual Basic - Creating new array during execution? [duplicate] - arrays

This question already has answers here:
Resizing an array at runtime in VB.NET
(8 answers)
Closed 8 years ago.
Okay so basically I need to create a grade book that allows the user to input their name, and the grades they received (say 5 test scores for example, some set amount).
It'll be inputted through a text box and displayed on a list box.
(And basically that's all it does.)
What I want to do now is, instead of overwriting the values each time when there's a new student, I want to store them inside an array.
Problem is, if I were do something like
Dim studentArray() As Integer
After an event, say a button click, it'll still overwrite it each time, once I click the button, right?
I was wondering if there was anyway around this?
My professor suggested that I use the inputted name as the array name, so that each time the button is clicked, the variable name would change so it won't constantly overwrite.
But I don't know how to do this.
Thank you in advance
EDIT: I was thinking about putting everything into a 2D array instead. Where the first column would be the names of the students followed by everything else? Would that be a good idea? (At the moment, we can assume the number of test is no more than 5, so a set amount is okay. Likewise for amount of students.)

you can use a Dictionary(Of string, integer()). And add the array to the dictionary. For example, to create the dictionary,
Dim dicStudentArrays As New Dictionay(Of String, Integer())
to add the array, being sName the inputted name
dicStudentArrays.Add(sName, studentArray)
to retrieve the array
studentArray = dicStudentArrays.Item(sName)
what I don't see is changing the name of a variable in the code while in execution

Related

Convert content of array to short string

Not sure if there is any good solution to do this but trying to wrap my head around it anyway.
Lets say that I have an array with items, maybe around 1000 items, representing all objects a user can add to their "inventory". Next, the user can select as many items it wants from that array and add to their own inventory.
Next I would like the user to be able to copy the content of that inventory, into a short string of numbers/letters that the user could send to another user (in plain text) and that user would be able to get the exact same inventory by just typing in the same short string, like "aFdEdfSf998872" or something smilar. Kinda like a url-shortner I guess.
To clarify, the order of the array that the items are picked from is always the same. The order of copied inventory does not need to be in the original order
My first thought was of course to just copy the index of each object into plain text but that could potentially make the string very long if there is a lot of objects.
Any one has any good ideas or pointer how this could be achieved or am I trying something impossible?

Converting array element to string in VB (using Visual Studio 2015)

So I'm trying to set up a VB Windows Form that reads data from an array and displays it in a label (and sometimes textboxes so it could be edited). The array contains strings and I know they are stored properly because I can make them appear properly when using MsgBox.
For example
MsgBox(ArrayName(0,0))
works, but when I use
textboxname.Text = ArrayName(0,0)
I get a build error. I thought adding ".ToString" at the end might solve this, and the build error is gone, but then the textbox says "System.Char[]" instead of the value that I put inside the array. The MsgBox shows this as well, if I put ".ToString" at the end.
Thank you in advance for your help!
Edit: The array is declared via the code:
Module ModuleName
Public ArrayData(20, 6) As Array
End Module
so that I can edit it outisde of any specific subroutines.
The array only contains the characters (delimiter is comma): 0,1,2,3,4,5,6 in the first row and q,w,e,r,t,y in the second row.

VBA Array - addressing to it by its name

this is a general VBA Array issue, it is not for MS Office apps (no tables involved).
I'm looking to find out how to create multiple one-dimension arrays at runtime (maybe even public ones), using data from a .csv file.
I can explain. This is an example of how the csv file would look:
------- CSV FILE ----------------------------
Colors,white,red,blue,green (... and so on)
Animals,cat,dog,wolf,bear (...and so on)
Food,cake,bread,garlic (...and so on)
...and so on, more rows
The opening part is solved,
even the part where each row is assigned to a temporary variable,
and more - the row is split into values and assigned to a temporary array.
So, I have:
tempArray1, containing ("Colors", "white", "red" ...etc)
tempArray2, containing ("Animals", "cat", "dog" ...etc)
...
The goal is to create (or to address to) an (existing) array
NAMED after the first value of each row and then assign the rest of the values from row to that array.
Please do not ask me why am I not using a multi-dimensional array.
I have my reasons.
A similar question related to this case is:
if I already have a one-dimension public array, defined, named and populated - let's say it is Colors() - how can I address to it using the value "Colors"?
Not only to address, but also to erase, redim or change values in it?
When I say "Colors" I mean a string value, not 'hard-coded' Colors() into the sub or function.
With respect to your "a similar question related to this case", you can do the following:
Create a public class module containing your array Colors()
Then, add a "Microsoft Script Control" ActiveX control (possibly to your form), and keep it hidden
Add code (as string) dynamically to your ScriptControl, and execute it. Now, if this code contains (as a string), say " Colors(1)="red" " , then it will actually modify the Colors array in your class-module.
Note: However, there's a catch. Since it is a class module, and not a
normal module, it will only modify the object created inside the
script-control. So, you might have to do all the rest of the coding
too in that script-control (by dynamically adding code to it and
executing it), otherwise, all changes would be lost as the scope of
that object would be limited to that code contained inside the
script-control

creating a character array with gff file

Could someone please help me with creating a "character array" with this sort of data:
ftp://ftp.ncbi.nih.gov/genomes/Bacteria/Escherichia_coli_ATCC_8739_uid58783/NC_010468.gff
Instruction from our collaborator:
One can copy and paste a gff file into a character array by typing gff={} and then copy and “paste excel data”
But I can somehow only save it as a cell array and hence the script downstream can't run properly.
additional info
I need to use the .gff file in a .m script (that I got from someone else) to parse my sequences.
Here's the part of my script that I think pertains to my question:
genelist=gff(:,3);
starts=gff(:,4);
ends=gff(:,5);
names=gff(:,9);
genelogical=strncmp('gene',genelist,4);
Therefore I want 9 columns with information like this:
1. seqID;
2. source;
3. type;
4&5. start and end;
6. score;
7. strand;
8. phase
9. attributes
Also when I saved the cell array as "cell", and tried to convert it into a character array by typing
char(cell)
I got this error message:
cell elements must be character arrays
So guess that doesn't work...
edit over
Anyone has any idea?
Thanks,
Joyce
The instructions you were given are referring to the following context menu item accessible from the "Variables" editor:
First, run at the command line gff={}; to make an empty cell array. Then double click the gff variable in the Workspace tab to get the above editor window. Click on the square regino to the top left of the table to select all cells, then right click to get the displayed menu, and paste the data. You should end up with the following if the paste worked correctly (it could actually take a couple of minutes - check CPU usage):
Then the commands you posted work. The strncmp says definitively that as cell array is needed.

Java programming in files , random , GUI

I want to write a Java program that reads the data from a file and stores it in an array. And then generates a random array index and uses it to retrieve a random word from the array to display. Each time the user clicks Next button, the program chooses a new random word to be displayed. I'm trying to add buttons in frame but they won't appear on the JFrame. Following is the code I wrote:
okButton = new JButton ("OK");
Pane.add(okButton);
nextButton = new JButton ("Next");
Pane.add(nextButton);
When the user enters a right answer I want a JLabel to appear saying " Correct answer ". How do I make a JLabel Appear? and when the user closes the frame all the right answers show in a JTextArea.
Can you please help me?
Your question is too much cluttered and vague to me, so providing perfect answer is quite not possible. Following are some useful tips you can use to solve your problem.
read file which contains the word. See File operations in Java and
How to use FileChooser?
Tokenize file and create an array of words. You can use StringTokenizer class for this
Create a method to randomly word from the array. You can use Random class for this.
For the GUI part:
the buttons won't appear on the JFrame
Are you adding panel containing buttons to frame properly?
how do I make a JLabel Appear ?
Add that JLabel to panel/frame and repaint it.
when the user closes the frame all the right answers show in a
JTextArea
Save right answers in some other array (call it answerArray).
Add Window listener to frame to monitor closing of frame.
In close method create a JDialog containing JTextArea and loop over answerArray and append string to textArea. Finally display this new JDialog.

Resources