Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 16th, 2010, 08:10 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Question Global variable withing a project

I have a project that has multiple .cs. The project is an xml read without any user interface. Is it possible to instanciate a variable in one .cs that can be read by another .cs? Or is there another way to do this? Thank you.
 
Old August 16th, 2010, 10:34 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default solution

hello
you can declare a static variable. In C# when you want to use a global variable you should declare a class that contains a static variable like this:
Code:
public class myVars
{
   public static string myGlobalVariable = "";
}
you can use this variable when you need:
Code:
myVars.myGlobalVariable = "test text";

// in other place
... = myVars.myGlobalVariable;
 
Old August 16th, 2010, 12:38 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Question

How would I set a value in one .cs and be able to use the variable in another .cs. Will that not cause an issue when I try to instantiate the class the the .cs where I assign a value and trying to use that value in another .cs where instantiation is not known?
 
Old August 17th, 2010, 06:31 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default hello

Hello dear,
in C# when you want to access to a property or variable of a class, you should declare that variable as static:
Code:
public class firstClass
{
   public static string myVar = "";

   // set value in some function
   void mySetFunction()
   {
       myVar = "sample text";
   }
}

public class secondClass
{
    // read value in other function in secondClass
    void UseMyVar()
    {
        ... = firstClass.myVar;
    }
}
notice that the only way you can share a variable between classes, is declaring variable as static
 
Old August 17th, 2010, 07:34 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Smile

Hello irProject,

Thank you. that helps....





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
global variable melvik C# 37 March 2nd, 2005 02:07 PM
global variable? milk_vanilla Classic ASP Basics 3 January 8th, 2005 06:50 PM
global variable in c# kobystud C# 3 April 22nd, 2004 05:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.