still not getting communication between classes

alright, let me try and explain this:

I have a main class that creates new instances of two other classes (class 1, class 2)

"class 1" needs to do a lot of work with info from "class 2", but unfortunately can't access it. I could pass it all in as parameters from the main class (which obviously can acess both "class 1" and "class 2" as it created them), but this would be insanely difficult ( due to how much stuff it works with).

I thought that i could simply create a public version of class 2 in the code for the main class by saying

public classname Class2
(
get { return class2; }
set { class2 = value; }
)


I then tried to access "class 2" in "class 1"'s code by saying main.Class2.blahblah but intellisense didnt pop up with an option for Class2, so i guess this wasnt the way to do it.

how can I make these classes accessable by eachother (by the way, is there a more complex book that I can get on this subject I think im growing beyond the scope of my visual c# 2005 step by step book!)

thanks



Answer this question

still not getting communication between classes

  • MAyotte

    Can you post small piece of code from both the classes in form of:

    public class Class1

    {

    string someProperty;

    public string SomeProprty

    {

    get{ return someProperty;}

    }

    //////////////////////////

    }

    What actually you are not getting from other class Make sure what you are trying to reference is not market with static and/or private keywords.

    Best Regards,



  • MurrayLang

    Depends of what you need from Class2 to be used in Class1. If that is Class2 instance properties or non static methods, then you should create a property in Class1 of type Class2 and set the property after instatiating Class2 instance. If you need to use only some static methods of Class2, then you can use without property of type Class2, which means directly Class2.MethodName(). If intellisense doesn't show Class2 then this two classes are in different namespace. You should solve this either by 'using' statement in Class1 or by specifying full path for Class2.

  • Ben Vanik

    alright I tried that, and it worked. Thats called adding a reference right

    Thanks very much everyone


  • keptin

    hehe I guess my attempt at simplifying the problem was way to vague. Heres some code snippets:

    public class main
    {
    private galaxSetup gcGalaxSetup = null;
    private ai gcShipAI = null;

    //blah blah blah

    gcGalaxSetup=new galaxSetup();
    gcShipAI = new ai();

    public galaxSetup Ggalaxsetup //apparently didnt do what i wanted it to
    {
    get { return gcGalaxSetup; }
    set { gcGalaxSetup = value; }
    }
    }

    public class ai
    {
    public void dispatch()
    {
    //do things with gInSystem by calling Ggalaxsetup.gInSystem but this didnt work!
    }
    }

    public class galaxSetup
    {
    List<int> inSystem = new List<int>();
    //some numbers are added to inSystem

    public List<int> gInSystem
    {
    get { return inSystem; }
    set { inSystem=value; }
    }
    }


    alright so the idea is that the main class creates "gcGalaxSetup and "gcShipAI". The main class then goes to a method (which i didnt include) in gcGalaxSetup which stores some information to the "inSystem" variable. After this is done, the main class then goes to gcShipAI.dispatch(); This method is supposed to read some numbers from the inSystem variable, but I can't figure out how to do that. I attempted to make gcGalaxSetup public through Ggalaxsetup in the main class, but i cant access this in gcShipAI like i thought i could.

    hope that clears it up a bit

  • Joshua Baker

    Can you start by explaining why class 1 can't access class 2

  • tody4

    If the main class creates class 2, then it has a reference to it. eg:

    class2 c2 = new class2();

    Now, main can create class 1 and pass this in eg.

    class1 c1 = new class1(c2);

    Now class 1 has a reference to class 2.



  • ELADDDDDDDA

    I haven't tried it myself, but i guess this should work:

    public class main

    {

    private galaxSetup gcGalaxSetup = null;

    private ai gcShipAI = null;

    //blah blah blah

    gcGalaxSetup=new galaxSetup();

    gcShipAI = new ai(gcGalaxSetup);

    }

    public class ai

    {

    private galaxSetup _galaxSetup;

    public ai(galaxSetup objGalaxSetup)

    {

    _galaxSetup = objGalaxSetup;

    }

    public void dispatch()

    {

    _galaxSetup.gInSystem.items.add(5);

    }

    }

    public class galaxSetup

    {

    List<int> inSystem = new List<int>();

    //some numbers are added to inSystem

    public List<int> gInSystem

    {

    get { return inSystem; }

    set { inSystem=value; }

    }

    }


  • still not getting communication between classes