471,582 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Global varible

I am a C# newbie.
I believe I need a Global variable or shared variable or universal variable,
but do not know how to create it.
As an example. I have a function that places a circle on a picturebox when
a button is pushed. I would like to change the position of the circle with
each button push, but do not know how to create a global variable to store
the last position of the circle.
As a work around I store the value in a textbox.
How do I create a variable that can be shared between functions and whose
scope is not ended when the function has ended?
This must be easy, but I can't figure it out.
Thanks,
Jim
Nov 17 '05 #1
3 3295
Jim McGivney <mc******@nospam.winid.com> wrote:
I am a C# newbie.
I believe I need a Global variable or shared variable or universal variable,
but do not know how to create it.
As an example. I have a function that places a circle on a picturebox when
a button is pushed. I would like to change the position of the circle with
each button push, but do not know how to create a global variable to store
the last position of the circle.
You don't need a global variable for that. You need an instance
variable.
As a work around I store the value in a textbox.
How do I create a variable that can be shared between functions and whose
scope is not ended when the function has ended?
This must be easy, but I can't figure it out.


Just declare it as an instance variable, the same as any other. If you
don't know about instance variables, I suggest getting a book or C#
tutorial. I know this *sounds* rude, and I don't *want* to be rude, but
newsgroups aren't the best place to learn a language from the basics -
they're better for specific questions.

The good news is that there are plenty of C# tutorials available for
free on the net.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
You need to seriously question your design. With OOP, global vars should be
absolutely kept to a minimum because they should not be necessary in 99.9%
of the time.
If you need to access an item across classes then expose the item via a
public property.
For the record, Global variables can be achieved using public static
variables.

The fact that you have asked this question leads me to think that you should
spend a little time in the C# /OOP books and read the language and OOP
essentials (and more) before attempting to implement something, otherwise
you are heading down a slippery path to a bad program and programming.

BTW it just sounds like you need to use a private class level variable to
store the circle position. This would then be available to all functions
within the class instance.

Br,

Mark.

"Jim McGivney" <mc******@nospam.winid.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I am a C# newbie.
I believe I need a Global variable or shared variable or universal
variable, but do not know how to create it.
As an example. I have a function that places a circle on a picturebox
when a button is pushed. I would like to change the position of the
circle with each button push, but do not know how to create a global
variable to store the last position of the circle.
As a work around I store the value in a textbox.
How do I create a variable that can be shared between functions and whose
scope is not ended when the function has ended?
This must be easy, but I can't figure it out.
Thanks,
Jim

Nov 17 '05 #3
Just a follow-up from my original post, and to add to Jon's; getting a good
book and reading from page 1 to the end and re-reading is really the best
way to get started. Its really one of the best ways that you could find out
about information and practices that you would probably not come across
otherwise. It also helps you realise other topics and books you need to
read.
My .Net library just seems to get bigger and bigger -the subject (including
OOP) is just huge, but the thirst for knowledge is too.

A few books for you...
1. A programmer's Introduction to C# by Eric Gunnerson (I've got the Second
Edition but there might be a newer edition now). It's very informative and
Eric is a big player to C# and .NET at microsoft.
2. C# A complete reference by Herbert Schildt. I just loved this book, it
was my bible to learning C# and I still go back to it to this day. Some
people don't like this book, but for me it answered nearly all my initial
questions and got me on my way to where I am now.
3. An Introduction to Object-Orientated Programming 3rd Edition by Timothy
Budd. You will probably find this book a little to advanced to start with,
but once you are ready for it, it is unsurpassed in my opinion. It discusses
many different OOP questions (using multi language examples) which I am
constantly wondering about and questioning myself. It is simply brilliant.

Also check out Jon's website, he has got a nice set of FAQ's, examples and
is imo one of the best ng posters here. Stay with the ng and keep an eye out
for questions that might interest you -and mark and read the thread. It is
great way to pick up things.

Br,

Mark.

"Jim McGivney" <mc******@nospam.winid.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I am a C# newbie.
I believe I need a Global variable or shared variable or universal
variable, but do not know how to create it.
As an example. I have a function that places a circle on a picturebox
when a button is pushed. I would like to change the position of the
circle with each button push, but do not know how to create a global
variable to store the last position of the circle.
As a work around I store the value in a textbox.
How do I create a variable that can be shared between functions and whose
scope is not ended when the function has ended?
This must be easy, but I can't figure it out.
Thanks,
Jim

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Soha El-Saeed | last post: by
3 posts views Thread by varun | last post: by
1 post views Thread by Phenix Smith | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
3 posts views Thread by Sachin | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.