Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Global varible

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim McGivney - 26 Jun 2005 21:35 GMT
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
Jon Skeet [C# MVP] - 26 Jun 2005 21:52 GMT
> I am a C# newbie.
> I believe I need a Global variable or shared variable or universal variable,
[quoted text clipped - 3 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Mark Broadbent - 26 Jun 2005 22:00 GMT
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.

>I am a C# newbie.
> I believe I need a Global variable or shared variable or universal
[quoted text clipped - 9 lines]
> Thanks,
> Jim
Mark Broadbent - 27 Jun 2005 10:15 GMT
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.

>I am a C# newbie.
> I believe I need a Global variable or shared variable or universal
[quoted text clipped - 9 lines]
> Thanks,
> Jim
A C# programmer - 28 Jun 2007 16:23 GMT
I use global variable as following:

//Global.cs
namespace my_name
{
    public class G
    {
        public static int g_var;
               ...
        public G() { }
    }
}

//other.cs
namespace my_name
{
    public class my_class
    {
           G.g_var = 5;
               ...
        Console.WriteLine(G.g_var.ToString());
               ...
    }
}

I don't know if this answer your question. Good luck!

From http://www.developmentnow.com/g/36_2005_6_0_0_549728/Global-varible.ht

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.