Variable's value doesn't change in coffeescript when using onclick - variables

I have the following Coffeescript code:
for name, data of statistics
row = document.createElement 'tr'
row.onclick = ->
alert name
However, when I compile and run it (in the context of a large webpage), it alerts the same name, no matter what row I click on. It seems to be remembering the variable, as if it's constant.
What am I doing wrong?
EDIT:
I've discovered the issue, but I'm not sure how to go about fixing it: Javascript/Coffeescript does not evaluate the 'name' variable until the end of the loop is reached.

The functions that you are defining (and assigning to the row's onclick attribute) all have access to the same variable outside that function (name). At the end of the loop, name has one value (the last item in the loop, as you mention), so each of the onclick functions alerts that value.
You can fix this by binding 'name' to a value that doesn't change. This question presents one solution. This question has some useful background that's worth reading.

Related

Is SetFocus a Function?

I saw this line of code txtNewCaption.SetFocus and was told that SetFocus is used as a sub
here but it is also a function. I also read online that the difference between a sub and a function is that a sub doesn't return any value but a function returns a value. I couldn't imagine what kind of value SetFoucs could return, so I searched quite some articles online about SetFocus but none of them gave me an example of SetFocus used as a function to return a value. I am assuming there is something inaccurate or wrong with either my understanding or what I was told.
Could you please help me to clarify the confusion?
Thank you!
Well, in this case and example, .setFocus is not a sub, nor is it a function.
A sub is a separate bit of code, you call it like
Call MySub
(or a shorter form skips the word "call" - it is optional)
eg:
MySub
Or, if there are parameters, you might go:
Call MySub(InvoiceNumber)
In the case of a function? That is again a external bit of code. You can use functions that return a value and IGNORE that return value.
So
MyVar = MyFunction()
And just like subs, the function can accept parameters:
MyVar = MyFunction(InvoiceNumber)
And as I noted, you can "ignore" the return value of a function like this:
MyFuncton InvoiceNumber
If you write as per above, the you are passing the value, but the return value of the function is not "placed" or put into anything. so you can "use" a function that returns a value. You also skip use of the () around the value you pass.
However, in the above two examples we are talking about VBA sub and functions YOU write.
SomeTextBox.SetFocus is NOT sub, nor is it a function.
What the above is called? It is called a Method of the object.
So, when you use Access objects, say record Sets, controls etc? Well that "object" will have what we call properties (usually things that you can set or get a value from).
So, for a text box, you have:
MyControl.Value = "Hello"
The above would set the value (text) of a text box to Hello. Thus ".value" is the property of that text box. It also happens to be the default property. So you can go:
MyControl = "Hello"
Now, if you go:
Msgbox("Value of MyControl = " & MyControl.Value)
So a property of a "thing" (a object) often can be set a value, and you can get (retrieve) a value. This is called a property.
However, these objects also have what are called methods. Methods are a "action" and are similar to a sub/function (but YOU did not write that method).
And such methods are "some code" that runs that is part of the object. So sometimes the difference between a property of a object, and a method of a object is "gray". Some methods can accept values and return values, but the distinction between the two (method and property) is that MORE then setting a value can occur with a method. (so it not just limited to get and set).
A "method" can be thought of some "action" and code. Use of that Method tends to be assumed that some code will run. (and it not your code in most cases - it is "code" attached to the built in object.
So
MyTextBox.SetFocus
It is a method of the text box control, or the given control in question. Not all controls necessary may have this "method".
So .SetFocus is not sub, not a function, but is in fact a method of the control in question.
Functions don't necessarily have to return a value. You can call functions just like you can call subs. Try for yourself: write a function, step through the function in a sub, and then check your locals window.
Functions have the added bonus of being able to return values, but don't have to.
EDIT: Aside from the need to return a value, I'm sure there is a reason to use one over the other, but I generally determine whether to use a sub or a function by the complexity of the task. I use functions to perform simple tasks and subs to perform more complex ones.
If you search the Microsoft Documentation (MSDN) for SetFocus, you will find it defined as:
method on a user-interface control object, moving the focus to it ... with no return value
(above quotation is not literally cited, but conveys the meaning)
SetFocus Method [Access 2003 VBA Language Reference]
TextBox.SetFocus method (Access)
SetFocus method (Microsoft Forms)
Function?
Thus it is not a function in VBA syntax.
Besides: a function's return value in VBA is optional. See VBA language reference: Function statement:
Optional. Return value of the Function.
Common method naming conventions
In most programming languages the common naming convention (case ignored) for methods apply:
use get (often without parameters) to return a value. Such methods are called getter or accessor.
use set (often with parameter as value to set) to set a new state (or value). Usually the have no return value, because the new value or state is predicted by the argument passed (their parameter). Such methods are called setter or mutator.
use is, has, exists, contains, starts, ends, etc. to return a boolean value. Such methods are used to check flags (on/off, true/false, yes/no).
use calculate, locate, indexOf, find, fetch, lookup, determine, etc. (often with parameters) to return a value resulting of some processing based on specified parameters (by some logical, mathematical algorithm or queried by data-retrieval)

Access VBA Combobox Store Value in Column

I have an MS ACCESS Combo Box and I wish to change the value of one of the columns in a particular row. I get error "object required" when I run this line:
Me.ComboName.Column(12, intUseRow) = myVar
(If I am unable to use the above syntax then you should also know that the row I am trying to change is always going to be the "current" visible row so there may be another way of solving the problem due to this fact).
Thanks!
If you have a recordset that is bound to a Table/Query, you will need to change the underlying data then requery the combobox to see changes.
If you load it manually (like in the form load event) and have the comboBox Row Source Type to "Value List" - you should be able to update it like this:
Copy all the data from the selected row into variables.
Combobox.RemoveItem (selected index)
change the required variable to the new value.
construct the semicolon separated string for the value list entry
combobox.AddItem new-string.
a bit messy, but it works correctly!

Conditional parallelism on LabVIEW

I am writing an application to update the numeric value given user's input value and depending on the user's input value the program checks if it is greater than 10 if it is greater than 10 then the program waits for 1 second and then will have a popup message says "true".
My intention was to call the conditional check, printing true every one second if the user's input value is greater than 10; in other words, the case structure was to be called regardless of the event structure in the same loop infinitely.
But it doesn't seem to work the way I expected. Unless there is user's new input value, getting into the event structure, the program doesn't get to the case structure even though the case structure is in a loop.
Is there any way to call the case structure not dependent to the event structure but I want to use a shift register for the numerical value and also having an event structure and a case structure in parallel.
Thanks.
You've created an Event Structure that handles the Value Change input for your numeric control, so that will do exactly what it says: wait for a Value Change event to happen. When that event is received, the code in its Event Structure frame will execute and then the Event Structure will exit.
It looks as if you've wired a value from the shift register to the timeout terminal of the Event Structure, so I assume you must also have created a Timeout event case? If so, the event structure should stop waiting after the number of milliseconds wired to the timeout terminal.
The value you check in order to decide whether to show your true message is the value that was passed in to the shift register on the previous loop iteration. If the control value changes, that comes from the NewVal terminal in the Value Changed event case. But where does it come from in the timeout case? It looks to me as if you haven't wired it in that case, because the terminal coming out of the event structure has a little dot in it instead of being solid orange. That means you will get a default value for any case where the terminal wasn't wired. The default value for numerics is zero. So if the event structure times out, the value going in to the shift register is zero, you get zero out of the shift register on the next iteration, zero is not greater than 10, so you don't see the message again.
I don't understand what you're trying to do in the event case where you've wired the NewVal terminal to a Value property node of the same control. Can you explain what that is supposed to achieve?
Your question reads as if this is a programming exercise where you have to use these specific LabVIEW structures, so rather than suggest better ways of achieving what you say you want this code to do, I'll leave it to you to decide how to change it. In the meantime though I do recommend re-reading the Event Structure help and the caveats and recommendations it links to.

Getting value of previous selected item (dropDown)

When a user changes the selected item on a dropdown I need to get the PREVIOUS item selected,
EX:
dropdown items:
1) Questions
2) Jobs
3) Tags
4) Badges
User has #2 Selected and then changes to #4 -- How can I get the value of #2 when they change the selection?
Declare an instance variable in your form (WinForms) or window (WPF).
When a user selects an item:
Do what you want to do.
Save the current item index in the instance variable.
In step 1, you can now access the instance variable to get the previously selected item.
Declare a global variable that will contain the previous value.
When the user changes the selection in the combobox, set the variable to the currently selected value. Allow the selection to be changed. You now will have the previous value.
If you need to have the history of changes, then the global variable would be a collection. Then on changed event, add the current selection to the collection.
If your control is bound to data, there is no need to "Squirrel" the old value away, your data provider usually does this for you.
For example, if you are bound to a DataRow, this code will get the previous value.
? = [Your DataRow].item("[Your column name]",OrigialVersion)
This varies based on your data but ultimately, you could always re-query the database to get the original value as well.
Regardless of what you are bound too, if you ask the datasource for it's value during the Validating event of the control, it will have not changed yet so it will give you the old value, which you can then compare against the current selection.
Lastly, if you are not bound to data, I typically store the old value in the TAG property on the GotFocus event of the control. Then you can compare against that.
Hope this provides some other options that might help you, depending on your case.

How to assign the value of textbox in first form into a checkbox on another form

Of course this must be simple. I have done this a thousand times but today I cant see the wood for the trees somehow. I want to do this... Me!ProjectID = Forms!Another.ProjectID
But I have the Another field reporting in the watch window correctly as value 50, and after assignment Me!ProjectID = 1....Dur!
I have tried it using periods instead of exclamation marks and using the .value property but nothing is altering the value post assignment to what it should be.
Its in the Form_open procedure. the checkbox, it has a lookup and ProjectID is the bound column.
Any thoughts appreciated.
Yep Braindead day - the answer is simple an OnCurrent event was also firing and resetting the ProjectID value!