Announcement

Collapse
No announcement yet.

C# Linking classes to eachother.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • C# Linking classes to eachother.

    Bit of a noob when it comes to C#.
    I'm working on a plugin for Centrafuse and I'd like to have two different classes, "A_Class" beeing the main plugin class and B_Class is my "interface" class that handles the interface to the car.

    So, A_Class needs to access instructions in B_Class and vice versa.

    The code below shows what I'm thinking about, but unfortunatley it's not that easy.
    Code:
    class A_Class:CF_Plugin
       B_Class myB = new B_Class;    
    
    {
        public void Do_Something_In_CF()
        {
         myB.Do_Something_With_The_Car();
        }
    
    }
    class B_Class
    {
       A_Class myA = new A_Class;    
        public void Do_Something_With_The_Car()
        {
        }
    
        public void Control_CF()
        {
         myA.CF_Actions(Volume UP);
        }
    }

  • #2
    Code:
    public class A_Class : CF_Plugin
    {
        B_Class myB = new B_Class();   
    
        public A_Class()
        {
              //Intialization logic goes here
        }
    
        public void Do_Something_In_CF()
        {
              myB.Do_Something_With_The_Car();
        }
    }
    
    public class B_Class
    {
       A_Class myA = new A_Class();
    
        public B_Class()
        {
              //Intialization logic goes here
        }
    
        public void Do_Something_With_The_Car()
        {
                //Code to do something with the car.
        }
    
        public void Control_CF()
        {
               myA.CF_Actions(Volume UP); 
        }
    }
    I assume CF_Actions() is an inherited method from the CF_Plugin?
    I am not familiar with the CF plugin, but I can help you with the C# part. That code should run now. You had forgotten the () after declaring the classes, and a few other minor things. Also, you should give your classes modifers (public, private, etc ).
    -Josh

    Comment


    • #3
      You can also mark the voids as static,

      public class ClassB
      {
      public static string ReturnString()
      {
      return "";
      }
      public static void MyVoid()
      {
      }
      }
      public class ClassA:CF_Plugin
      {
      public void Do_Something_In_CF()
      {
      ClassB.MyVoid();
      }

      }

      Comment


      • #4
        Originally posted by skrewdlude
        That code should run now. You had forgotten the () after declaring the classes, and a few other minor things. Also, you should give your classes modifers (public, private, etc ).
        The problem wasn't that it wouldn't compile, I wrote that _example_ from my memory.

        The problem with having two "new" declarations of the other class
        Code:
        Class A....
        
        myb = new Class B
        
        Class B...
        
        myA = new Class A
        Is that you create an endless spiral that eventually (long before the app launched) will make .net puke.



        I ended up solving it this way:

        Code:
        	public class ClassA: CFPlugin
        	{
                      ...............
                      private ClassB m_ClassB
        
        	public ClassA()
        	{
                    m_ClassB= new ClassB();
                    m_ClassB.init(this);
                    }
                    ..............
                    }
        
        
        public class ClassB
        	{
                     ..................
                     private ClassA m_ClassA
        
        		public  ClassB()
        		{
        			//
        			// TODO: Add constructor logic here
        			//
        			
        		}
        
        		public void init(ClassA pc)
        		{
        		m_ClassA=pc;
        }

        Comment


        • #5
          haha, yea I noticed that this morning after I read what I had posted, sorry it was late.

          It could be done a bit simpler than that too:
          Code:
          public class ClassA : CFPlugin
          {
                private ClassB m_ClassB
                
                public ClassA()
                {
                      m_ClassB= new ClassB( this );
                }
          }
          
          public class ClassB
          {
                private ClassA m_ClassA;
                
                public  ClassB()
                {
                     //
                     // TODO: Add constructor logic here
                     //			
                }
                
                public  ClassB( ClassA classA ) //Overloaded constructor
                {
                     m_ClassA = classA			
                }
          }

          Comment


          • #6
            Is there a reason you need to create an instance of class B and not just static? The only time you need individual classes is if you create more than one of them. If you intentions are to use a sigle instance I would recommend using static methods/properties.

            Comment


            • #7
              I was just assuming that he needed instances. Your way is obviously the correct way if this is not the case.

              Comment


              • #8
                The correct way to code this is to use 2 seperate classes that contain the methods required for each class, then develop a third class that creates the needed instances of the other two classes. Then the third class can access both methods and properties that are made public on the two other classes.

                Of course, you still have not told anyone what you are trying to accomplish.
                --------------------------
                Software Developed:
                DS-ATX Controller Interface
                Laptop Shutdown Controller

                Considered Projects:
                TPMS Interface
                Bluetooth Handsfree Interface (WIDCOMM/BROADCOMM)

                Comment

                Working...
                X