global variable

R

RobcPettit

Hi, after reading up, c# doesnt appear to use gloal variables. Alot of
post advise against them also. So Im wondering how to get around this.
Im trying to do a calculation, retain the result, do another
calculation then add the result to my retained value etc. my values:
Im backing then laying horses. So I back first, say with a return of
£20 if it wins. Then I lay the horse with an offset bet giving me an
overall return of £1. Ok, so I need to retain this value, at the
momment I put it in textbox 'a'. Now say on the same horse I bet
again. Same bet £20 if it wins, except Ive got to add my £1 giving
£21. Ok so far, but I must retain the £20 outcome for this bet as it
is used to calculate the offset bet. Not the £21. At the momment Im
putting this figure into textbox 'b'. Ive written a class to do all
the calcs. These calcs are done when a button is clicked, so once
clicked the variables go out of focus, whence putting the values in a
text box. But this doesnt feel very proffesional. Also I have to use
another text box to retain the first odds backed a, so I can check
that the lay odds are smaller. Hope you get the jist. Can anybody
advise if there is a better way to do this.
Regards Robert
 
P

Peter Duniho

Hi, after reading up, c# doesnt appear to use gloal variables. Alot of
post advise against them also. So Im wondering how to get around this.

The usual "way" is to put variables into classes. In fact, that's the
only way.
Im trying to do a calculation, retain the result, do another
calculation then add the result to my retained value etc.

Well, you must have a class that performs the calculations. It may make
sense to put the intermediate values in that class.

I don't fully understand the example you give, not being someone who bets
on horses. But it sounds to me as though the data that you're keeping
definitely is *not* of the sort that one would normally put in a global
variable. You seem to have a per-horse state (what you've bet, how the
horse did, etc.) and so those values should be in a per-horse class. You
could even name the class "Horse" if you like. :)

Pete
 
R

RobcPettit

Hi, thanks for a quick reply. I know what you meen, I did try that but
I found that between the click events I lost the values, Im guessing
because they go out of focus. Does this sound right.
Regards Robert
 
P

Peter Duniho

Hi, thanks for a quick reply. I know what you meen, I did try that but
I found that between the click events I lost the values, Im guessing
because they go out of focus. Does this sound right.

Sound right? I don't even have any idea what you're talking about.
Clicks and textboxes are ways to display data to the user and for the user
to provide input. They aren't places you store data. And losing focus
shouldn't result in displayed data (in a textbox, for example) to change
or be lost. It just means the focus goes somewhere else.

Perhaps if you could be more explicit about the *code* you're having
trouble with, that would help. Posting a concise-but-complete example of
what you're trying, along with an explanation of what about it doesn't
work, would help.

Pete
 
M

Mark Rae

I found that between the click events I lost the values, Im guessing
because they go out of focus. Does this sound right.

No - it makes no sense at all...

As Peter has advised, please show your code...
 
R

RobcPettit

Sorry Ive really messed this question up. Ok here is a simplified
version of my cass.
using System;
using System.Collections.Generic;
using System.Text;

namespace exceltest.Tasks
{
public class returns
{
public double backprice;
public double layprice;
public double back;
public double lay;
public double totReturn;
public string bet;


public void backbet()
{
back = ((backprice - 1) * 5);
totReturn += back;
bet = "on";
}

public void laybet()
{
lay = ((layprice - 1) * 5);
totReturn += lay;
bet = "on";

}


public void offsetback()
{
// horse backed = (profit+stake(original stake * original
odds) * odds-1(total return - stake) - return
// horse backed = (profit+stake(original stake * original
odds) * odds-1(total return - stake) - return
lay = (((back + 5) / layprice) * (layprice - 1));
totReturn -= lay;
bet = "off";";
}



}
}

with buttonbackbet I use
rt.backprice = 4.1;
rt.backbet(); making back within the class = 15.50.
and totreturn = 15.50.
then with button2laybet I use
rt.layprice = 4;
rt.offsetback(); which should make totreturn = 0.13p.
This doesnt happen though because, the back and totreturn from the
backbet havnt been retained.
I hope this is better.
regards robert
 
M

Mark Rae

How are you actually instantiating the class...?

Please provide the code in buttonbackbet.Click() and button2laybet_Click()
 
R

RobcPettit

lay button =
returns rt = new returns();
rt.totReturn = 15.50;
rt.back = 15.50;
rt.layprice = 4;
rt.offsetback();
back button =
returns rt = new returns();
rt.backbet();
totreturn = 15.50.
the class is called returns.
Regards Robert
 
M

Mr. Arnold

Hi, after reading up, c# doesnt appear to use gloal variables. Alot of
post advise against them also. So Im wondering how to get around this.
Im trying to do a calculation, retain the result, do another
calculation then add the result to my retained value etc. my values:
Im backing then laying horses. So I back first, say with a return of
£20 if it wins. Then I lay the horse with an offset bet giving me an
overall return of £1. Ok, so I need to retain this value, at the
momment I put it in textbox 'a'. Now say on the same horse I bet
again. Same bet £20 if it wins, except Ive got to add my £1 giving
£21. Ok so far, but I must retain the £20 outcome for this bet as it
is used to calculate the offset bet. Not the £21. At the momment Im
putting this figure into textbox 'b'. Ive written a class to do all
the calcs. These calcs are done when a button is clicked, so once
clicked the variables go out of focus, whence putting the values in a
text box. But this doesnt feel very proffesional. Also I have to use
another text box to retain the first odds backed a, so I can check
that the lay odds are smaller. Hope you get the jist. Can anybody
advise if there is a better way to do this.
Regards Robert

---------------------------------------

You should use a Global accessor object that has been instantiated to hold
your variables.

However, you can this, which I don't recommend this for a Web solution.

You can just add a class to the project and use a public static variable.

It's kind of like you did a Module.vb in VB.Net to hold Global Variables and
added it to the project, which can be seen globally.

class GblVars
{

public static int someno = 0;

}

Then in your button click you can do this.

private void butTest_Click(object sender, EventArgs e)

{

GblVars.someno ++;

textBox1.Text = GblVars.someno .ToString();

}
 
M

Mark Rae

lay button =
returns rt = new returns();
rt.totReturn = 15.50;
rt.back = 15.50;
rt.layprice = 4;
rt.offsetback();
back button =
returns rt = new returns();
rt.backbet();
totreturn = 15.50.
the class is called returns.

Well there's your problem! You're creating a new instance of the class
behind each button...

You need to instantiate the class variable at the form level so that it's
available to all methods within the form e.g.

public partial class Form1 : Form
{
returns rt = null;

private void Form1_Load(object sender, EventArgs e)
{
rt = new returns();
}

protected void Button1_Click(object sender, EventArgs e)
{
rt.totReturn = 15.50;
rt.back = 15.50;
rt.layprice = 4;
rt.offsetback();
}

protected void Button2_Click(object sender, EventArgs e)
{
rt.backbet();
totreturn = 15.50.
}
}

Incidentally, the above isn't particularly good style, and is certainly not
very robust, but it will at least solve your variable scope issue...
 
G

Guest

Rob,
after reading through this thread, it sounds to me that if you intend to
make your application really usable (or to sell it to others) then you need
to stop and think about how to store your data in some sort of database or
local file storage that gets reloaded when somebody restarts your
application.
When you get your first Martingale system running let me know and I'll buy a
copy.
Peter
 
R

RobcPettit

Thankyou all for our replys. Plenty to work with now. Storing the data
is my next task, although martingale will not be the system. Simply
trying to trade the prices/money. Ive tried the static class and that
works. Going to try your suggestion also Mark. thanks again.
regards robert
 
M

Mark Rae

Robert,
Thankyou all for our replys. Plenty to work with now. Storing the data
is my next task, although martingale will not be the system. Simply
trying to trade the prices/money. Ive tried the static class and that
works. Going to try your suggestion also Mark. thanks again.

Please don't take this the wrong way, but OOP generally and classes
specifically are *absolutely fundamental* to C#...

You really would be well advised to get a copy of this:
http://www.amazon.com/C-2005-Dummie...9970862?ie=UTF8&s=books&qid=1180879762&sr=8-1

and work your way through it. Part 4 particularly will be of great benefit
to your particular requirements...

Without a basic grasp of how a C# app is structured, and what the various
bits do, you're really going to struggle...

Also, if you get into bad habits from the beginning, you'll find it much
more difficult to "unlearn" them in the future...
 
R

RobcPettit

Totaly agree. No offence taken. Thats the good thing about asking
question, you soon find out how little you know.
Regards Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top