I had this three classes:

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.

Recommended Answers

All 7 Replies

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.

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:

namespace Test
{
    public class Book
    {
        public string Author;

        public string ZmienCiag
        {
            get
            {
                return Author;
            }

            set
            {
                Author = value;
            }
        }
    }
}

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

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.

OK. So far I resolved problem in this way:

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)?

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.

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".

That was helpful of him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.