I am new to Visual Basic and have a homework assignment as follows:
"You are to create an array of 10 elements, String type, and load 10 unique values into the array, one value per element. Your program must then output those values into a Listbox control.
Note: Your program MUST use a loop to load the data into the array(Hint: use InputBox function), and another loop to output it to the Listbox control. How your program acquires the information is up to you, but processing must be done using loops."
I am confused about how to enter the data that I want (arrayvalue(0) - arrayvalue(9)) via loop to a listbox.
I started with:
Dim n as integer = 10
Dim fruitarray(n) as string
Do Until fruitarray(n)=10
fruitarray(0) = "watermelon"
fruitarray(1) = "apple"
fruitarray(2) = "pear"
fruitarray(3) = "plum"
fruitarray(4) = "pineapple"
fruitarray(5) = "grapes"
fruitarray(6) = "strawberry"
fruitarray(7) = "raspberry"
fruitarray(8) = "banana"
fruitarray(9) = "blackberry"
ListBox1.Items.Add(CStr(n))
Loop
Thanks in advance!
You are doing all the things in one loop and without using InputBox as per requirement.
so here is your solution.
int n = 10;
string[] fruitarray = new string[n]
Dim message, title As String
message = "Enter your value"
title = "InputBox Demo"
for(int i=0;i<10;i++)
{
fruitarray[i]=InputBox(message, title, defaultValue)
}
for(int i=0;i<10;i++)
{
ListBox1.Items.Add(fruitarray[i]);
}
Related
This is probably easy but I just cannot get the answer. Here is a simple Array:
I want the information to be distributed from an Input textBox to different dynamic textBox after a click. I am OK with buttons.
var ERLQ1:Array = ["ERLQ1", "N09°02.61 / E100°49.11", "ErawanLq"];
InputText = "ERLQ1";
//I want to display:
Txt1 = "ERLQ1" //Being first part of the array as main reference.
Txt2 = "N09°02.61 / E100°49.11" // Should be: String(ERLQ1[1])
Txt3 = "ErawanLq" // Should be: String(ERLQ1[2])
First time I write in a forum like this. Please forgive if not perfect. Thanks in advance.
Andre
If I understand correctly, an array of objects would work well. Since you have a set number of textfields I assume that you also have a set number of details that you want to display in them. If that is the case, this solution should work fine.
arr:Array = [{_name:"ERLQ1",ans1:"N09°02.61 / E100°49.11",ans2:"ErawanLq"},
{_name:"ERLQ2",ans1:"question 2 answer 1",ans2:"ques2ans1"}];
So, I don't really "get" your application, but if it were some sort of a quiz, you'd have a new array element for each question, and that element has a name, and two answers. Easy to modify it to grab answers from an answer pool. Now to find the element in the array that has ._name == "ERLQ1" you will need to loop through all the elements and return the one that has the ._name property that matches your search. Here is an example function:
private function matchName(arr:Array, term:String):int{
for (var i:int = 0; i < arr.length; i++){
if (arr[i]._name == term){
return i;
}
}
return -1;
}
This function will return the array index number of the matching term. If no match exists, it returns -1. So you could use it like this (pseudocode):
// on submit search{
// find the index number in the array of the element that matches the search term
var ind:int = matchName(arr, searchTerm);
// assign the textfield texts to the corresponding associated values
textBox1:text = arr[ind]._name;
textBox2:text = arr[ind].ans1;
textBox3:text = arr[ind].ans2;
}
I perhaps misunderstood (because of my English), but :
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
var ERLQ1:Array = ["ERLQ1", "N09°02.61 / E100°49.11", "ErawanLq"];
var Txt1 : TextField = new TextField();
Txt1.autoSize=TextFieldAutoSize.CENTER;
Txt1.type = TextFieldType.INPUT;
Txt1.border = true;
var Txt2 : TextField = new TextField();
Txt2.autoSize=TextFieldAutoSize.CENTER;
Txt2.type = TextFieldType.INPUT;
Txt2.border = true;
var Txt3 : TextField = new TextField();
Txt3.autoSize=TextFieldAutoSize.CENTER;
Txt3.type = TextFieldType.INPUT;
Txt3.border = true;
addChild(Txt1);
addChild(Txt2);
addChild(Txt3);
Txt1.x = 20, y =40;
Txt2.x = 180, y =40;
Txt1.x = 300, y =40;
Txt1.text = ERLQ1[0]; // is now : first part of the array as main reference (String(ERLQ1[0]).
Txt2.text = ERLQ1[1]; // is now : String(ERLQ1[1]);
Txt3.text = ERLQ1[2]; // is now : String(ERLQ1[2]);
This will display 3 TextFiels as input text like this :
If I misunderstood your question, please tell me more about what You expect!
Best regards.
Nicolas
Programming an educational program with fractions, you can touch a part of a fraction on a digiboard (or pc screen) that becomes colored/active. Touch or click it a second time and it becomes white/unactive again. And so on...
To reset a fraction with all of its colored parts to white I want to press a single button once that's calling the function resetFraction. Please take a look at the code below, it seems to go wrong at the two-dimensional array with movieclips - no change from colored to a white part. All arrays containing names of movieclips:
//array with fractions containing the (real) movieclips of parts
//mc_frac1 has one part, mc_frac2 has two parts, and so on
var fractionAr : Array = new Array(mc_frac1, mc_frac2, mc_frac3, mc_frac4, mc_frac5);
//array with max-index for partAr, see below
var maxPart : Array = new Array(1,2,3,4,5);
//array with parts, 2 dimensional with a variable index per fraction
var partAr : Array = new Array(
new Array(mc_part1_1),
new Array(mc_part2_1, mc_part2_2),
new Array(mc_part3_1, mc_part3_2, mc_part3_3),
new Array(mc_part4_1, mc_part4_2, mc_part4_3, mc_part4_4),
new Array(mc_part5_1, mc_part5_2, mc_part5_3, mc_part5_4, mc_part5_5));
//put all parts back to 'untouched' (color white)
function resetFraction(var FracNum : Number) {
var p : Number = FracNum;
for (var i = 0; i < maxPart[p]; i++) {
fractionAr[p-1].partAr[p-1][i].gotoAndStop(1);
}
}
This code below is split up in portions to see where it goes wrong:
p = 4;
mc_frac4.mc_part4_2.gotoAndStop(1); //works
fracAr[p-1].mc_part4_2.gotoAndStop(1); //works
//but
fracAr[p-1].partAr[3][1].gotoAndStop(1); //does nothing
The two-dimensional array seems to be the problem.
Anyone can help me? It saves a lot of code to realise this with one function.
Thanks a lot.
I'm out!
Instead of the dot (.) I use the array acces operators ([ ]) and quotes (" ") in the partAr array:
var partAr : Array =
new Array(
new Array("mc_part1_1"),
new Array("mc_part2_1", "mc_part2_2"), and so on...
while the fourth line in the function becomes this:
function resetFraction(var FracNum : Number) {
var p : Number = FracNum;
for (var i = 0; i < maxPart[p]; i++) {
fractionAr[p-1][partAr[p-1][i]].gotoAndStop(1);
}
}
Simple, isn't it?
I have an array in which the response to each question is recorded using a list response. A for loop where once the last question in the array is reached, another button (titled "continue") is made visible. A person clicks this now visible button to continue the task. I have two trace commands in my code, one in the for loop and one in for when the continued button is clicked. The trace function in the for loop works; however, the trace executed in the function when the continue button is clicked returns "undefined" values. (If my description isn't clear, I will make this more concrete using my code below.)
My question is why is it that the exact same value that is being trace would return values in one instance and not the other? My goal is to store the responses to the questions in the array into a String.
var listOfQuestions:Array = new Array;
var listOfAnswers:Array = new Array;
var i:int = 0;
listOfQuestions[0] = "Question 1";
listOfQuestions[1] = "Question 2";
listOfQuestions[2] = "Question 3";
lstResponses.addItem({label: "Response 1", data: "1"});
lstResponses.addItem({label: "Response 2", data: "2"});
lstResponses.addItem({label: "Response 3", data: "3"});
btnNextQuestion.addEventListener(MouseEvent.CLICK, presentNextQuestion);
function presentNextQuestion(evt:MouseEvent){
listOfAnswers[i] = lstResponses.selectedItem.data;
lstResponses.selectedItem = null;
i++;
//Present the element stored in index “i”;
if(i == listOfQuestions.length)
{
txtQuestion.htmlText = "<b>End of list. Click the Continue to Part II for the next part.</b>", btnContinue.visible = true, btnNextQuestion.visible = false;
//Output all the questions and answers;
for (i = 0; i <listOfQuestions.length; i++)
{
trace(i, listOfQuestions[i], listOfAnswers[i]);
}
}
/*If there are more elements left, present the element stored in index “i.”*/
else
{
txtQuestion.htmlText = listOfQuestions[i];
}
}
btnContinue.addEventListener(MouseEvent.CLICK, continueClicked);
function continueClicked(evt:MouseEvent){
trace(listOfAnswers[i]);
}
To reiterate my issue using my code above:
trace(i, listOfQuestions[i], listOfAnswers[i])
yields the anticipated result, i.e., 0 Question 1 [response]. However, the
trace(listOfAnswers[i]);
in the last line of the code yields "undefined".
I've also wondered whether this error was due to the data needing to be converted to a string. In that regard, I added the following code (see below), but the error I received was: Error #1010: A term is undefined and has no properties.
var b:String = new String;
b = listOfAnswers[i].toString()
b = listOfAnswers[i].join("");
Thanks for your time and patience.
The issue appears to be how you're tracing the answer when you click on the continue button. You are tracing the answer at the index of i but your code doesn't prevent i from being incremented past the length of your list.
My guess is you're clicking the next button until it tells you that you have reached the end of the list, then only do you click the continue button. If so, i will be 3, and you're trying to trace listOfAnswers [3] and that is one more than the length of your list, giving you undefined.
Try replacing
trace(listOfAnswers[i]);
with
for (i = 0; i <listOfQuestions.length; i++)
{
trace(listOfAnswers[i]);
}
was wondering if someone could show me what I'm doing wrong here.
I have some old AS2 flash code I'm trying to get working.
First I create a few arrays in frame 1 of the main timeline like so-
var typeArr:Array = new Array();
for (var i:Number = 1; i < 5; i++)
{
_root.typeArr[i] = "data goes here";
}
Then I have a movieclip dynamically attached on the main stage that when clicked appends one of the arrays we created by pushing the string 'foo' to it-
stop();
_root.myType=3;//this can be any of our array numbers
this.onPress=function(){
var foo:String="test";
_root.typeArr[_root.myType].push(foo);
trace(_root.typeArr[_root.myType]);
}
Where _root.typeArr[_root.myType] is the array name and number _root.typeArr3, but pushing the data does not work and returns nothing.
However, if I test it directly using-
_root.typeArr[_root.myType]=foo;
It will store the data once (_root.typeArr3=test), so I can't see why it won't push to that array as multiple elements each time like- "test,test,test"
It's driving me crazy.
Thanks! :)
_root.typeArr[_root.myType] is equal to "data goes here" so you are pushing string to a string, which doesn't work.
If you would like to append the new string, you should do something like:
_root.typeArr[_root.myType]+=foo;
and you will get: data goes heretest
If you have different data structure instead of "data goes here" the key may lie in the format of this data.
var typeArr:Array = new Array();
// 'i' must start from 0 because the first element is typeArr[0]
for (var i:Number = 0; i < 5; i++)
{
typeArr[i] = i;
// trace(typeArr[i]); // 0,1,2,3,4
}
// trace(typeArr); // 0,1,2,3,4
myType = 3;
bt.onPress = function()
{
var foo:String = "test";
// push method puts the element at the end of your array
// typeArr.push(foo);
// trace(typeArr); // 0,1,2,3,4,test
// splice method replace the 4e element (index 3) of your array
typeArr.splice(myType, 1, foo);
trace(typeArr); // 0,1,2,test,4
}
I am running into some trouble adding an array into another array to create a multi-dimensional array.
The code appears as below:
var slideDataArray:Array = new Array();
var slideShowDataArray:Array = new Array();
slideDataArray[0] = xmlData.SlideShowParameters.SlideShowImagesDirectory;
slideDataArray[1] = xmlData.SlideShowParameters.SlideShowTimeInterval.valueOf();
slideDataArray[2] = xmlData.SlideShowParameters.SlideShowWidth.valueOf();
slideDataArray[3] = xmlData.SlideShowParameters.SlideShowHeight.valueOf();
slideDataArray[4] = slides.length();
slideShowDataArray[0] = slideDataArray;
for (i = 0; i < slides.length(); i++) {
// Read data from Slides tag in the XML file into slideDataArray
slideDataArray[0] = slides[i].SlideImage.toString();
slideDataArray[1] = slides[i].SlideText.toString();
slideDataArray[2] = slides[i].SlideLink.toString();
// Input the data from slideDataArray into the array for the slideshow (slideShowDataArray)
slideShowDataArray[i + 1] = slideDataArray;
}
// end of FOR loop
I am looking for a means of placing the slideDataArray into a 'slot' or value of slideShowDataArray so that I can in the end pass the slideShowDataArray as a parameter to another function.
As of now, the last slideDataArray appears 11 times (the loop runs 11 times) in slideShowDataArray and the way the code is written the slideDataArray is unique every iteration of the loop.
Any help is appreciated.
Thanks in advance...
Remember you are not adding an array, but a reference to slideDataArray to your multidimensional array. Each reference points to the same array - which just contains different values on every iteration of the loop. In other words: Every time you add that reference, you "link" to the same address in memory.
To get around this, move the inner part of the loop to a separate function and create a new local array on every call:
function createDataArray ( slide:Object ) : Array {
var slideDataArray:Array = [];
slideDataArray[0] = slide.SlideImage.toString();
slideDataArray[1] = slide.SlideText.toString();
slideDataArray[2] = slide.SlideLink.toString();
return slideDataArray;
}
Then call it from your loop:
for (i = 0; i < slides.length(); i++) {
slideShowDataArray.push( createDataArray (slides[i]) );
}
You should end up with 11 unique arrays instead of one array that is overwritten 11 times.