Proper use of Variables?

May I ask what I am doing wrong here? I created 9 global variables (C1 - C9). The block image shows C9 defined, and my attempt to create a new variable called "Hi" by mathematically combining variables C1 through C9.

MIT AI2 doesn't like what I am doing. I tried using text blocks to hold the variables but they do not plug in to mathematical operations. I thought I found the solution with the "Get (C1 - C9)" but my ways are error-bound.

I know the solution is simply a matter of choosing the proper representations for my variables to make the math work. May I request that someone direct me to the proper way to define a variable with other variables? Thank you!

When you initialize a global variable you tell the app what the variable is.

A text, a number, a list, a dictionary, etc, etc.

You don't do calculations in a global variable using data from text boxes for instance, what you did.

See below for an example.

https://ai2-appinventor-mit-edu.ezproxy.canberra.edu.au/reference/blocks/variables.html#global

Are you trying to reinvent value procedures?

Read the chapter all the way to the bottom.

You cannot do it like that, as @Peter said, and I am saying that you cannot be able to use any global variable as a value in initialisation of the variable you need to use set block in a procedure to get values.

AI2 variables are passive resting places for data in run time memory.

They don't change until your code changes them.

The global init is there to introduce the variable name for the blocks Editor and to avoid the horrors of uninitialized variables.

Initialization happens just once, at Screen startup

Hi, Everyone

I appreciate all of the information provided and I read, line-by-line the PDF chapters on variables and procedures - and I am close to tears with my ignorance and inability to internalize what everyone has been telling me. I just haven't cracked the code. I tried to replicate other blocks that work but no cigar. Please reference this screenshot:

I am pretty certain I am doing things backwards, as I cannot create blocks that agree with each other. On top of that, when the attached .ala is incorporated into my app, the app crashes - it is basically corrupted with gray boxes.

I am in so over my head, here. If someone with infinite patience and a healthy dose of empathy could show me how to call my "save_Heat_Index_Sunny" procedure from a checkbox, I promise to study and embody the information. I simply cannot turn the provided information into a meaningful outcome. Obviously, this file does not "stand alone," as it is just a partial function without the necessary code for displaying results. I do not need to create a result here from the code, I just need to be able to call the procedure from a checkbox.

The attached .ala has all of the named variables that apply to this one feature. I don't know if I am calling variables incorrectly, such that the app crashes when run or if it crashes because I am giving it garbage for data. Thank you, again.

Heat_Index_Sunny.aia (3.7 MB)

what about click the triangle in the red circle?

Here's the proper use of variables version (though I can't vouch for your formula or the numbers I fed it.)


Heat_Index_Sunny (1).aia (3.7 MB)
Making value procedures

image

I leave it to you to play with the numbers and check the formulas.

I don't like using Labels to pass data down the lines to other calculations.

Labels are the arseholes of the app.

Here's a better version, using local variables, since the rest of the app does not care about the constants or formulas used for this calculation, and they should be hidden from the rest of the app.


Heat_Index_Sunny (2).aia (3.7 MB)

You really should double check the formulas, again.

Greetings, Abraham

The formula was lacking proper C2 and C3 data. C2 is * Temp F and C3 is * Humidity. I really am thankful for your clear and concise explanation and visual aids. It had simply not sunk into my thinking parts that I cannot assign a function to a variable - I make it a procedure, instead! The biggest hurdle I overcame was realizing that I could click on the gear on the procedure tab to invoke "x."

It only took a few minutes to modify your clear example to my application. Speaking of the "arseholes" of the AI2 system, I guess you can tell what kind of logical company I keep by my continued insistence on using labels to hold the temperature and humidity data. My reasoning is that I have three sources of atmospheric data - the local NWS website, my remote Arduino sensor, and the make-believe data from the user input. When I select any of these three sources, they immediately update the labels with their take on the information. I just do not have the bandwidth to try to make this happen with variables, if that is even possible. If it is possible, it would take some major updating of my blocks. I have yet to find an instance where the label data failed to update my checkbox choices, or was corrupt or incorrect in some manner. What might I be in for sticking to my label guns? Is there a systemic failure in my future or am I simply writing less-than-pristine code? Inquiring mind wants to know;).

If I could only find the red circle, I would be all over the triangle;). @ABG was a coding saint for this issue and showed me how I was abusing variables.

Hi There,

@ABG was able to take your words and visually demonstrate what you were saying. At the time I read your post, I simply could not grock the meaning of your words but I now realize what you meant. Thank you.

Hi, Peter

I am slowly coming online re: variables, procedures, et al. @ABG was able to show me the proper way of utilizing both variables and procedures in my code. Thank you for your input.

He was trying to invoke a value procedure from the wrong type of socket, so the call block pulldown came up empty .

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.