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: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

I need share variables between this various classes

 
0
  #1
Jan 7th, 2009
I had this three classes:

  1. Start.cs:
  2. ---------
  3. using System;
  4. using System.Windows.Forms;
  5.  
  6. namespace Test
  7. {
  8. public partial class Form1 : Form
  9. {
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. }
  14.  
  15. static void Main(string[] args)
  16. {
  17. Uruchom();
  18. }
  19.  
  20. public static void Uruchom()
  21. {
  22. Book written = new Book();
  23. Book bought = new Book();
  24. written.Author = "Test";
  25. }
  26. }
  27. }
  28.  
  29. Variables.cs:
  30. -------------
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Text;
  34.  
  35. namespace Test
  36. {
  37. public class Book
  38. {
  39. public string Title;
  40. public string Author;
  41. public short YearPublished;
  42. public int NumberOfPages;
  43. public char CoverType;
  44. }
  45. }
  46.  
  47. Checking.cs:
  48. ------------
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Text;
  52.  
  53. namespace Test
  54. {
  55. private class Outside
  56. {
  57. private void SomethingElse()
  58. {
  59. Book written = new Book();
  60.  
  61. Console.WriteLine("Author: {0}",written.Author);
  62. }
  63. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I need share variables between this various classes

 
0
  #2
Jan 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: I need share variables between this various classes

 
0
  #3
Jan 7th, 2009
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:
  1. namespace Test
  2. {
  3. public class Book
  4. {
  5. public string Author;
  6.  
  7. public string ZmienCiag
  8. {
  9. get
  10. {
  11. return Author;
  12. }
  13.  
  14. set
  15. {
  16. Author = value;
  17. }
  18. }
  19. }
  20. }
Last edited by piotr_kast; Jan 7th, 2009 at 6:16 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I need share variables between this various classes

 
0
  #4
Jan 7th, 2009
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
  1. public static void Uruchom()
  2. {
  3. Book written = new Book();
  4. Book bought = new Book();
  5. written.Author = "Test";
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: I need share variables between this various classes

 
0
  #5
Jan 7th, 2009
OK. So far I resolved problem in this way:

  1. File Obliczenia.cs
  2. ------------------
  3. namespace Test
  4. {
  5. public partial class Obliczenia : Form
  6.  
  7. // all variables made public
  8. {
  9. FixedString FixLength = new FixedString();
  10. }
  11. }
  12.  
  13. File FixedString.cs
  14. ----------------------
  15. namespace Test
  16. {
  17. // here i put procedure which not use variables from other classes
  18. class FixedString
  19. {
  20. }
  21. }
  22.  
  23. File Track.cs
  24. -------------
  25. namespace Test
  26. {
  27. public partial class Obliczenia : Form
  28. }
  29.  
  30. File Cells.cs
  31. -------------
  32. namespace Test
  33. {
  34. public partial class Obliczenia : Form
  35. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I need share variables between this various classes

 
0
  #6
Jan 7th, 2009
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: I need share variables between this various classes

 
0
  #7
Jan 7th, 2009
Thanks. You explain my doubts for now.
By the way I had really strange programming teacher, which only saw his way programming (only in Java - not even Visual Java, but I learn Visual C# on my own) and for every question which may enchanced my knowledge, had one answer "I haven't time for this".
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: I need share variables between this various classes

 
0
  #8
Jan 7th, 2009
That was helpful of him.
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2293 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC