473,382 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

global variable

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
13 2127
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
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
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
<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
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
<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
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

<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
<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
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
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.