I know that global variables are traditionally not the way to go, but I figured I'd present my question anyway. For a college senior project for a software engineering class we are supposed to create a 3AC calculator using quads. One of the requirements is to have a display for the instruction memory (the 3AC in quadruple format) and the data memory (the integer value of the quad). Furthermore, the program should tell the user how many temporaries exist in memory and display them to the user in the following format:

Operand3 ::= Operand1 operator Operand2

Thus, my question is:

When I create my function to take in the quad to determine the value to be put into the data memory "register" I am going to be essentially creating a standard form equation. Therefore,

(+, 3, 4, t0)

would become

t0 = 3 + 4

which very closely resembles the format for the 3AC temporaries. If I wanted to store these into, say, a List or some sort of collection and use them in another function, is there any way I could do this without establishing the collection as a global variable?

I can't see a means of doing this due to the limitations of variable passing between functions relating to specific elements on the GUI. We're supposed to display the temporaries one at a time with a means to go between them (e.g. a scroll bar) but this scroll bar would need access to the collection of temporaries (due to the handler for the scroll bar being a separate function). So, if there's a way to accomplish this without creating the collection as a global variable please enlighten me as it will help me in not only this small project but in the future as well.

Thanks!

Recommended Answers

All 5 Replies

You can declare a private member inside the instance of the form's class. I would think "global" means more along the alones of public static which is not what you want here.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmWord : Form
  {
    private List<string> lst;
    public frmWord()
    {
      InitializeComponent();
      lst = new List<string>();
    }
  }
}

That member will have the same life cycle as the form so you can use it to display as needed.

Thank you for the reply.

Yeah, that's exactly how I had planned on doing it. My professor told me that the variable declaration just within the form class was considered a "global variable" and it was very "poor form." I mean, I get that globals are bad, but if the variable is only being modified in a particular function and read in all other methods it appears then is that really a risk within a program if the variable is clearly defined?

So, is my professor just confused as to what a "global variable" is in this situation?

As i understand it a global variable is one that is accessible to ANY code in the program...that would be public static (as sknake mentioned) if im not mistaken. Global variables are best avoided, if you need to pass data between forms/classes use public properties.

However, a private class level variable is an instance variable and still encapsulated within its class and only accessible from within that class so i dont see that it is bad practice...if it is its news to me :p

I'd advise speaking to your tutor to clarify what he considers a Global Variable.

Every property of a form would be "bad" according to your professor :P I think he probably misspoke or didn't explain himself clearly. You can declare a public static inside the form's class so technically if you do that he could have meant bad form, or maybe he meant instance members. Either way its an everyday thing and it is not considered bad form if used properly

I assumed that he was just misspeaking (if that's a word) about this because I had always thought that a variable declared as private within the class wouldn't technically be "global." Thanks for reassuring me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.