| | |
I need share variables between this various classes
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
I had this three classes:
I want to see and edit variables from class Book in other classes. I want to do as simplest as possible. I had some experience in structural programming but some difficulty understand OOP programming in Visual Studio 2005 C# programming.
C# Syntax (Toggle Plain Text)
Start.cs: --------- using System; using System.Windows.Forms; namespace Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static void Main(string[] args) { Uruchom(); } public static void Uruchom() { Book written = new Book(); Book bought = new Book(); written.Author = "Test"; } } } Variables.cs: ------------- using System; using System.Collections.Generic; using System.Text; namespace Test { public class Book { public string Title; public string Author; public short YearPublished; public int NumberOfPages; public char CoverType; } } Checking.cs: ------------ using System; using System.Collections.Generic; using System.Text; namespace Test { private class Outside { private void SomethingElse() { Book written = new Book(); Console.WriteLine("Author: {0}",written.Author); } }
I want to see and edit variables from class Book in other classes. I want to do as simplest as possible. I had some experience in structural programming but some difficulty understand OOP programming in Visual Studio 2005 C# programming.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
all classes under a name space can see each other, so, as long as you have made the variable/property under the classes that you need to see from one class to another, public, the other classes can see and use them.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
Ok. Even if I change in class outside all private to public - still doesn't work. I notice that if can use variable from another class (each class is in separate file - if they are in one file, this works fine!).
In order to access this kind variable/property I must write something like this:
Book written = new Book()
otherwise intellisence not showing avaliable variables when I tried access it to put it down into my code.
Problem lays in it, if I use this declaration from file start.cs and repeat it from class checking.cs in this configuration - VARIABLE INITIALIZES AGAIN (it's a main problem). Not working either if I change class Book to this:
In order to access this kind variable/property I must write something like this:
Book written = new Book()
otherwise intellisence not showing avaliable variables when I tried access it to put it down into my code.
Problem lays in it, if I use this declaration from file start.cs and repeat it from class checking.cs in this configuration - VARIABLE INITIALIZES AGAIN (it's a main problem). Not working either if I change class Book to this:
C# Syntax (Toggle Plain Text)
namespace Test { public class Book { public string Author; public string ZmienCiag { get { return Author; } set { Author = value; } } } }
Last edited by piotr_kast; Jan 7th, 2009 at 6:16 am.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Im speaking from the many years of programming I have.
the problem is you're looking at a class template and wondering why you cant use it without an instance.. which you cant, you're also running into issues of scope.
your code for example
Once you leave that procedure, both bought and written cease to exist. So, you cant use them.
the problem is you're looking at a class template and wondering why you cant use it without an instance.. which you cant, you're also running into issues of scope.
your code for example
C# Syntax (Toggle Plain Text)
public static void Uruchom() { Book written = new Book(); Book bought = new Book(); written.Author = "Test"; }
Once you leave that procedure, both bought and written cease to exist. So, you cant use them.
Last edited by LizR; Jan 7th, 2009 at 6:20 am.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
OK. So far I resolved problem in this way:
What in this is little confusing, that I had form with element from toolbox (buttons and texboxes). And I can use them if I am inside class Obliczenia : Form (using operator this). Outside this class - not working.
1. How can I access to this elements from outside this class? Is there any solution?
Another confusing thing from school: the teacher was strongly against using declaration public in any variables.
2. Is that true that this type of declaration you use as the last resort?
And last questions:
3. How big may be one class?
4. And how many methods may I put this (I mean differences between structural and OOP programming)?
C# Syntax (Toggle Plain Text)
File Obliczenia.cs ------------------ namespace Test { public partial class Obliczenia : Form // all variables made public { FixedString FixLength = new FixedString(); } } File FixedString.cs ---------------------- namespace Test { // here i put procedure which not use variables from other classes class FixedString { } } File Track.cs ------------- namespace Test { public partial class Obliczenia : Form } File Cells.cs ------------- namespace Test { public partial class Obliczenia : Form }
What in this is little confusing, that I had form with element from toolbox (buttons and texboxes). And I can use them if I am inside class Obliczenia : Form (using operator this). Outside this class - not working.
1. How can I access to this elements from outside this class? Is there any solution?
Another confusing thing from school: the teacher was strongly against using declaration public in any variables.
2. Is that true that this type of declaration you use as the last resort?
And last questions:
3. How big may be one class?
4. And how many methods may I put this (I mean differences between structural and OOP programming)?
Last edited by piotr_kast; Jan 7th, 2009 at 7:15 am.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
1. Your statement in your code saying "All variables are made public" is not true.
Your other code doesnt seem to be overly relevant as you have a number of forms defined, but include no detail. You have an empty class of "FixedString".. I cant help you with why things arent working without detail. If the items you try and access are private, then it cant be seen.
if you have 2 forms, for ease of typing Form1 and Form2. You create instances of them.. For you to then access 1 from the other you *can* as long as you can see the variable holding the instance of the other form, use that. If you cant, then its a simple design problem.
2. yes and no. Use of properties is a better standard to adapt and saves changes later, etc, but its not necessarily *bad* but public *is* required for some things, you cant avoid it, if your property is not public, nothing can use it.. public variables persay is just lazy, but if you dont need anything else, its a practical place to start
3. As big as you like pretty much
4. See above, as pretty much as big as you like, however, the more you cram in, the more memory it will use.
Your other code doesnt seem to be overly relevant as you have a number of forms defined, but include no detail. You have an empty class of "FixedString".. I cant help you with why things arent working without detail. If the items you try and access are private, then it cant be seen.
if you have 2 forms, for ease of typing Form1 and Form2. You create instances of them.. For you to then access 1 from the other you *can* as long as you can see the variable holding the instance of the other form, use that. If you cant, then its a simple design problem.
2. yes and no. Use of properties is a better standard to adapt and saves changes later, etc, but its not necessarily *bad* but public *is* required for some things, you cant avoid it, if your property is not public, nothing can use it.. public variables persay is just lazy, but if you dont need anything else, its a practical place to start
3. As big as you like pretty much
4. See above, as pretty much as big as you like, however, the more you cram in, the more memory it will use.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- What is the cleanest way to share variables and functions between classes??? (C++)
- Share global variables between classes (C++)
Other Threads in the C# Forum
- Previous Thread: c# windows server 2003
- Next Thread: Initiating form on button click
Views: 2293 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






