Trouble with arrays and randomizing [duplicate] - arrays

This question already has answers here:
How to initialize properties that depend on each other
(4 answers)
Closed 7 years ago.
I have
var rockNamesArray:[String] = ["bird", "rock2", "rock3"]
var rockpos = Int(arc4random_uniform(UInt32(3)))
var firstrockString:String = self.rockNamesArray[rockpos]
But its telling me that rockNamesArray isnt a member. Help?

The following works perfectly in a playground.
import Foundation
var rockNamesArray:[String] = ["bird", "rock2", "rock3"]
var rockpos = Int(arc4random_uniform(UInt32(3)))
var firstrockString:String = rockNamesArray[rockpos]
it's not clear from your code if those variables are being declared inside a function or at the class level. The issue is the self. which refers to member variables so I assume the declaration is inside a function etc.
As ABakerSmith hinted - it's really easy to get to the bottom of these kinds of issues by copying & pasting the offending code into a playground.

Related

Assignment to constant variable (DISCORD.JS) [duplicate]

This question already has an answer here:
Assignment to constant variable exception
(1 answer)
Closed 11 months ago.
I have a probleme, I want to make a setprefix command like that:
if (message.content.startsWith(prefix + 'setprefix')) {
var usermsg = message.content.split(" ").slice(1).join(" ");
if (!usermsg[0]) return message.delete().then(console.log('[', 'ERROR'.red, ']', 'un argument est nécessaire'))
prefix = usermsg
const setprefix = (`prefix changé en: ${usermsg}`)
message.delete()
message.channel.send(setprefix)
}
but i have this error:
prefix = usermsg
^
TypeError: Assignment to constant variable.
What did I must do ?
According to your error, there appears to be a constant named as prefix and you are trying to change the value of the constant. This is a really easy mistake to make especially for beginners who have just started learning something in JavaScript. I would recommend that you actually take a course on learning basic JS before attempting Discord.JS since there are a lot of mistakes which can be made if you don't know the basics. For your question, all you have to do is, in the line of code where you declare the constant prefix, instead of using const prefix, change it to let prefix

Why does each neuron generate the same UUID when I instance a new one? [duplicate]

This question already has answers here:
Swift: Creating an Array with a Default Value of distinct object instances
(2 answers)
Closed last year.
import Foundation
struct Neuron {
let id = UUID()
func doSomeStuff() {
// stuff
}
}
let layer:[Neuron] = Array(repeating: Neuron(), count: 5)
When running:
for neuron in layer {
print(neuron.id)
}
Every Neuron object gets given the same UUID instead of its’ own unique ID. I feel like I’m just missing the obvious here, or I’m misunderstanding how structures work?
I’m having the same issue with other properties in my full code but this is a cut down version for ease.
Thanks!
You are not generating a new instance. You are repeating the same instance in your collection. If you would like to create new instances you would need to create a range and map your struct initializer.
let layer = (1...5).map { _ in Neuron() }

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

VB.Net MaskedTextBox array [duplicate]

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}

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.

Resources