473,698 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2156
On Sat, 02 Jun 2007 09:45:07 -0700, Ro********@yaho o.co.uk
<Ro********@yah oo.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********@yaho o.co.uk
<Ro********@yah oo.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********@yah oo.co.ukwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.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.Collecti ons.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(o riginal stake * original
odds) * odds-1(total return - stake) - return
// horse backed = (profit+stake(o riginal 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********@yah oo.co.ukwrote in message
news:11******** **************@ o5g2000hsb.goog legroups.com...

How are you actually instantiating the class...?

Please provide the code in buttonbackbet.C lick() and button2laybet_C lick()
--
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********@yah oo.co.ukwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.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(o bject sender, EventArgs e)

{

GblVars.someno ++;

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

}

Jun 2 '07 #9
<Ro********@yah oo.co.ukwrote in message
news:11******** **************@ m36g2000hse.goo glegroups.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(obje ct sender, EventArgs e)
{
rt = new returns();
}

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

protected void Button2_Click(o bject 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
102754
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> <instructions> def bar: <instructions>
4
24177
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(); function loadUrlVariables() { varString = location.search;
4
7127
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
8827
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 { public: Book()
8
2548
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 in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
17
5619
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
10
2651
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 the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
3025
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
29362
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
112
5435
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 that may print some messages. foo(...) { if (!silent)
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.