By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

global variable

P: n/a
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

Jun 2 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Sat, 02 Jun 2007 09:45:07 -0700, Ro********@yahoo.co.uk
<Ro********@yahoo.co.ukwrote:
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
Jun 2 '07 #2

P: n/a
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

Jun 2 '07 #3

P: n/a
On Sat, 02 Jun 2007 10:02:35 -0700, Ro********@yahoo.co.uk
<Ro********@yahoo.co.ukwrote:
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
Jun 2 '07 #4

P: n/a
<Ro********@yahoo.co.ukwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
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...
--
http://www.markrae.net

Jun 2 '07 #5

P: n/a
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

Jun 2 '07 #6

P: n/a
<Ro********@yahoo.co.ukwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...

How are you actually instantiating the class...?

Please provide the code in buttonbackbet.Click() and button2laybet_Click()
--
http://www.markrae.net

Jun 2 '07 #7

P: n/a
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

Jun 2 '07 #8

P: n/a

<Ro********@yahoo.co.ukwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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();

}

Jun 2 '07 #9

P: n/a
<Ro********@yahoo.co.ukwrote in message
news:11**********************@m36g2000hse.googlegr oups.com...
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...
--
http://www.markrae.net

Jun 2 '07 #10

P: n/a
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

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Ro********@yahoo.co.uk" wrote:
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

Jun 2 '07 #11

P: n/a
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

Jun 3 '07 #12

P: n/a
<Ro********@yahoo.co.ukwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...

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-Dummies...0879762&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...
--
http://www.markrae.net

Jun 3 '07 #13

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

Jun 3 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.