Global Variables

rexalbel

New member
Mar 29, 2011
22
0
0
Hey all. New to developing. I used to be a webos user and always found precentral as the best forum for info and help so hopefully its the same here.

I've found that learning to develop for windows phone is much easier thanks to the assistance of visuals (visual studio) It's not so much that it makes it too simple, but rather being able to see what your designing and then see the design code, makes it much quicker to learn. and i've been enjoying it over webos. Ive watched several videos, got my hands very dirty with messing with code and am learning.

anyway i've learned that not all questions are easily answerable my main question is regards to global variables.

right now i have the same codes repeated several times because the calculations i am using are based of user settings. problems is because many of the calculations have if functions i have to paste the exact same variables in each instance, and i am concernet that may affect performance?

variables are fairly simple an example is

if (radio.IsChecked == true)
{
decimal a = decimal.parse(textBox1.Text)
decimal b = decimal.parse(textBox1.Text) / a
}
if (radio2.IsChecked == true)
{
decimal a = decimal.parse(textBox1.Text)
decimal b = decimal.parse(textBox1.Text) * a
}

i need the above to be available throughout atleast the page.
 

CommonBlob

New member
Jul 18, 2011
160
1
0
I guess it depends on how many of these you do. If we are talking 5 or so, its not going to affect performance much.

However, what you could do to simplify your code is to put these into a sub routine.

Like this:
Code:
void DoSomething(RadioButton radio)
{
if (radio.IsChecked == true)
{
decimal a = decimal.parse(textBox1.Text) 
decimal b = decimal.parse(textBox1.Text) / a
}
}
There are other ways of doing this too. It could be that Binding is your best option, but it is more complicated and I won't suggest as Im not sure what else you have going on ;)
 

rexalbel

New member
Mar 29, 2011
22
0
0
Is a sub routine like a helper method? If so that would only apply in the block i mak3 it right? I tried making a method under initialization but the variables didn't seem to work for any of the other blocks. All the variables within the calculations should errors that the variable wasnt found so i guess because they were in an if statement they didn't work.as far as variables. I have somewhat shortened it, but would still like to shorten things with if statements i have several

if(x == "")

x:text = "0"

i have to do this for every every textbox field in which is used in the calculation otherwise it gets thrown an exception if i leave it blank when calculating and i have 7 variables, which have four ways of being used i put all the variables and ifs that change blanks to 0 ( 7 of em) in the parent method, but i still have use the variables to multiple times since one way yeilds the variable is divided and the other yeilds it multiplied. I guess im concerned with performance. Im always hearing about code being slugish if not done right, and im noticing some performance issues so i want to make sure this app performs top notch. It is fairly visually intensive, but i tested it in an earlier build with less code and it ran like butter so it isn't the design (its not like 3d graphics or anything but i did push the capacities of visual basic and expression blendto give me a unique experience i dont want to sacrifice :) )
 

thed

New member
Jan 6, 2011
992
3
0
As CommonBlob said, doing this calculation is going to have a pretty negligible effect on your code's performance unless you're doing it many many times.

As for making a and b class-scope (class scope is a more accurate term here than global) variables, without knowing what your code actually does it's hard to say whether that will improve performance or not. If the value in the text box changes you will always have to recalculate them anyway.

I'm not sure what this code is doing here, but in the first if block b will always be 1, since you're essentially dividing decimal.parse(textBox1.Text) by itself. You don't need to do the calculation there.

Also, I'm assuming that radio and radio2 are in the same group. If that's the case then you should use else if for the second block. That way if radio is checked, the code will not check to see if radio2 is checked. Not a big performance difference but it's good coding practice.

If you are doing a lot of decimal.Parse, there is one way to speed it up a little bit. The decimal.Parse is the most expensive operation here, so you could store those results in a variable so it only runs once.

This is how I would write this code. It's functionally equivalent to what you wrote:

Code:
decimal a = decimal.parse(textBox1.Text);
decimal b;

if (radio.IsChecked)
{
  b = 1;
}
else if (radio2.IsChecked)
{
  b = 2 * a
}
If you know you are only dealing with 2 radio buttons in the group then you could simplify even more:

Code:
decimal a = decimal.parse(textBox1.Text);
decimal b = 1;

if (radio2.IsChecked)
{
   b = 2 * a;
}
If you really want a and b to be class scope variables then you can put the calculation code in a function as CommonBlob said:

Code:
public partial class Whatever : PhoneApplicationPage
{
   decimal a, b;

   private void Calc()
   {
     a = decimal.parse(textBox1.Text);
     b = 1;

     if (radio2.IsChecked)
     {
       b = 2 * a;
     }
   }
}
Then whenever you call Calc() a and b will be set to the correct values. Since a and b are class scope you can use them anywhere on the page.

But still I would guess that if you're having performance issues they're probably occurring somewhere else.

Sorry I went off on some tangents there, your post just made me think of a lot of things. Hope this makes sense.
 

CommonBlob

New member
Jul 18, 2011
160
1
0
I think thed has covered most of the ways to do it. I guess if we saw more code we could be a little more accurate on our suggestions.
 

rexalbel

New member
Mar 29, 2011
22
0
0
it's kind of hard to get the exact help when not really giving the exact code, I'm new to this so I'm not sure if i should post parts of my code and then if someone takes it, or if someone would even take it. i guess it really doesn't matter because the initial calculation is public and my coding is not great yet so anyone who wants to steal it would be better off writing it themselves

private void calcButton_Click_1(object sender, RoutedEventArgs e)
{

// these next items all make sure the empty textboxs are equal to 0
if (calBox.Text == "")
{
calBox.Text = "0";
}
if (fatBox.Text == "")
{
fatBox.Text = "0";
}
if (fibBox.Text == "")
{
fibBox.Text = "0";
}
if (carbBox.Text == "")
{
carbBox.Text = "0";
}
if (proteinBox.Text == "")
{
proteinBox.Text = "0";
}
if (sodiumBox.Text == "")
{
proteinBox.Text = "0";
}
if (servingsBox.Text == "")
{
servingsBox.Text = "1";
}
// these are all the variables
decimal x = decimal.Parse(servingsBox.Text);
decimal u = decimal.Parse(unitsblock.Text);
decimal a = decimal.Parse(calBox.Text);
decimal b = decimal.Parse(fatBox.Text);
decimal c = decimal.Parse(fibBox.Text);
decimal d = decimal.Parse(carbBox.Text);
decimal g = decimal.Parse(proteinBox.Text);
decimal h = decimal.Parse(sodiumBox.Text);
decimal j = decimal.Parse(calBlock.Text);
decimal k = decimal.Parse(fatBlock.Text);
decimal l = decimal.Parse(fibBlock.Text);
decimal m = decimal.Parse(carBlock.Text);
decimal n = decimal.Parse(proBlock.Text);
decimal o = decimal.Parse(sodBlock.Text);
//This is the first calculation (yes i know i used a menuitem as a button. i wanted something i can click without seeing the white background. i tried using expression blend to change the templetes focus attribute background to transparent but i get thrown an exception everytime i do that for some reason so when i figure out why, i will revert it back to a button. if this is an issue please let me know)
if (menuItem1.Header as string == "on")
{
// each variable is now declared again. the main reason is that based on the menu item button being on or off , each individual variable needs to be divided or multiplied. if there was a way to use a variable to represent an operator i would rather do that as i'd just have to make a variable equal '/" or "*" ive been researching for days and can't seem to find what is going on.
decimal cal = j + (a / x);
calBlock.Text = cal.ToString();
decimal fat = k + (b / x);
fatBlock.Text = fat.ToString();
decimal fib = l + (c / x);
fibBlock.Text = fib.ToString();
decimal car = m + (d / x);
carBlock.Text = car.ToString();
decimal pro = n + (g / x);
proBlock.Text = pro.ToString();
decimal sod = o + (h / x);
sodBlock.Text = sod.ToString();

if (HP.IsChecked == true)
{
if (c > 4)
{
c = 4;
mBox.Text = "Only 4 grams of fiber count towards this items units";
}
decimal y = decimal.Round((a / 50) + (b / 12) - (c / 5), 0);
//this is taking the calculation, adding it to the previous calculation divided by the number of servings
decimal hp = u + (y / x);
unitsblock.Text = hp.ToString();
}
if (HPplus.IsChecked == true)
{
decimal y = decimal.Round((g / 10.9m) + (d / 9.3m) + (b / 3.9m) - (c / 35), 0);
decimal hpp = u + (y / x);
unitsblock.Text = hpp.ToString();
}
}
//the entire section past the menubutton if statement is then repeated if the menu button is "off"

the ideal situation would be to make the operators to divid and multiple variables so i can just call one if statment for that variables value, or something. I'm messing with it now so maybe i can figure it out. playing around with code is how i figured out how to turn the formulas into code in the first place
 

thed

New member
Jan 6, 2011
992
3
0
Since those calculations are all similar, you can create a function to handle this.

It could look something like this:
Code:
private decimal Calc(decimal var1, decimal var2, decimal var3, bool b)
{
  decimal val = 0m;
  if(b)
  {
    val = var1 + (var2 / var3);
  }
  else
  {
    val = var1 + (var2 * var3);
  }
  return val;
}
Then instead of the if (menuItem1.Header as string == "on") ... blocks you can just do this:
Code:
bool bMenu = menuItem1.Header as string == "on";
calBlock.Text = Calc(j, a, x, bMenu).ToString();
...
sodBlock.Text = Calc(o, h, x, bMenu).ToString();
You'll have to check and make sure I got the logic right, but this way you don't have to declare all those new variables or repeat all that code, since the TextBlock values will be set based on which menu item is selected.

Note that doing this won't help performance at all, but it will make your code cleaner and easier to maintain.
 

Members online

Forum statistics

Threads
327,728
Messages
2,250,533
Members
428,666
Latest member
GoriGrea