0

this is probably very simple, but I have always just made one big class and never tried make clean code. Now I am trying and experiencing errors..

So, this is the idea:

class1
{
    method1 { value 1; value 2 }
    method2 { value 3; value 4 }
    method3 { uses method4 from class2 }
}

class2
{
    method4 { uses values 1-4 from class1 }
}

I am doing it by calling: class1 c1 = new class1() in method4 and class2 c2 = new class2 in method3.

So this is what happens:

  • method1, method2 produce values 1-4
  • method3 calls class2 c2 = new class2
  • I get into class2, then into method4 and get null/0 values instead of what I made in first step.
  • are you sure this is c#? – smile.al.d.way May 20 '15 at 15:37
  • If you want to share _data_ then those should be class _properties_, not local variables. `class1` will then need an _instance_ of `class2` (e.g. `class2 c2 = new class2()` to use its methods and properties. – D Stanley May 20 '15 at 15:38
  • 2
    Can you post some *actual* code, not some very difficult to understand pseudo-code? Don't worry if its wrong, we can fix that. – Ron Beyer May 20 '15 at 15:38
  • 1
    Instead of showing the idea, could you maybe show actual code instead? – poke May 20 '15 at 15:38
  • @RonBeyer yes sure, I'll make edit – Ken'ichi Matsuyama May 20 '15 at 15:44
  • There's simply too many places we have to make guesses/assumptions as to what your code actually looks like – Jonesopolis May 20 '15 at 15:48
  • Lots of code here. Might as well be a plate of spaghetti. However to answer the base question - anything of your variables declared with the public modifier can be accessed outside of the class. – JEV May 20 '15 at 15:54
  • @Glitch100 well, this is not true, because my dictionaries are public and I cannot acces them in other class in the same namespace. – Ken'ichi Matsuyama May 20 '15 at 16:16
  • But they are part of this MainWindow.xaml? I am not sure how .xaml files work, but if you made another class with those dictionaries and rather new'd it up or made them static you would be able to see them – JEV May 20 '15 at 16:18

5 Answers5

0

So when you need to access variables within a class you can obviously do this simply via the 'public' modifier, however the below example is not best practice but we will get onto that shortly...

public class MyTestClass
{
    public int MyAge;
}

This is a field - fields should really be private, and we should use a property to expose the field. However if you did do this, then you can access that like so:

var foo = new MyTestClass();
var hisAge = foo.MyAge;

Of course based on your requirements maybe you don't want the user to access the variable directly, but rather get a value after some computation has been done on other variables.

You can do this like so:

public class MyTestClass
{
    private int _gamesPlayed;
    private int _gamesLost;
    public int NumberOfWins { get { return _gamesPlayed - _gamesLost; } } 
}

NumberOfWins is a Property. It computes the values of two of our fields and reutrns it. See how we have the private modifier, these can't been seen outside of the scope of that class. NumberOfWins can be accessed the same way as MyAge in the previous example.

To be honest, it sounds like you are rather using pseudo-code or are a beginner.

I recommend checking out the following articles for a bit more information on what I have stated.

Community
  • 1
  • 1
JEV
  • 2,494
  • 4
  • 33
  • 47
  • 2
    Please make `MyAge` a property instead. – poke May 20 '15 at 15:42
  • thanks for tips, I think I am slowly getting what should I do, in the meantime I have added code, if you want to look at it would be great and give me some tips how to change some values into properties. – Ken'ichi Matsuyama May 20 '15 at 15:51
0

Instead of creating a new instance of class1 in method4 you should pass the current class1 instance (accessible through this inside method3) as a parameter to this method to get the same result.

Robert S.
  • 1,942
  • 16
  • 22
0

You need to be more specific...

    class Class1
    {
        Class2 _class2;
        public Class1(Class2 class2)
        {
            _class2 = class2;
        }
        public void method3()
        {
            //call _class2.method4()
        }
    }
    class Class2
    {
        Class1 _class1;
        public Class2(Class1 class1)
        {
            _class1 = class1;
        }
        public void Method4()
        {
            //call _class1.MethodWhatever()
        }
    }

adPartage
  • 829
  • 7
  • 12
  • thanks for tips, I think I am slowly getting what should I do, in the meantime I have added code, if you want to look at it would be great and give me some tips how to change some values into properties – Ken'ichi Matsuyama May 20 '15 at 15:52
0

it is really unclear what you want to achieve and how class1 is linked to class2. If your class2 is ONLY useful for the first class then (and only then) you could use nested classes...

class OuterClass {
    string value1;
    string value2;
    string value3;
    // ...

    class InnerClass 
    {
      OuterClass o_;
      public InnerClass(OuterClass o) 
      {
         o_ = o; 
      }

      public string GetOuterString()
      { 
        return o_.value1 + o.value2 + o.value3; //... 
      }
    }

    void SomeFunction() 
    {
       InnerClass i = new InnerClass(this);
       i.GetOuterString();
    }

}

This would create a clear binding from the inner class (class 2) to the outer one. It is not easier though.

Edit: OK, after your edit I see a whole different story..

Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
-1

Well, here is some code for you. I'm not sure it it's what you require. It might help you get started, though. You can try running it here: https://dotnetfiddle.net/#

This is Class1. It exposes some of its data via properties.

public class Class1
{
    // these are properties
    public int Value1 { get; set; }
    public int Value2 { get; set; }
    public int Value3 { get; set; }
    public int Value4 { get; set; }

    public void Method1()
    {
        Value1 = 1;
        Value2 = 2;
    }

    public void Method2()
    {
        Value3 = 3;
        Value4 = 4;
    }

    public void Method3()
    {
        // uses method4 from class2 
        var c = new Class2();
        c.Method4();
    }
}

This is Class2. It calls methods from Class1 and accesses its properties.

public class Class2
{
    public void Method4()
    {
        //uses values 1-4 from class1 
        var c = new Class1();

        c.Method1();
        c.Method2();

        Console.WriteLine(c.Value1);
        Console.WriteLine(c.Value2);
        Console.WriteLine(c.Value3);
        Console.WriteLine(c.Value4);
    }
}

This uses both closes and shows the result:

using System;

public class Program
{
    public static void Main()
    {
        var c1 = new Class1();      
        c1.Method3();
    }
}
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • Yes. You will have to rewrite your code. The code that you posted initially wasn't actually C# code. It was some other syntax. – Shaun Luttin May 20 '15 at 15:52