Results 1 to 8 of 8

Thread: C# Linking classes to eachother.

  1. #1
    Variable Bitrate
    Join Date
    Apr 2004
    Location
    The land of polar bears and nekkid blonds
    Posts
    446

    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. #2
    Newbie
    Join Date
    May 2006
    Posts
    6
    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

  3. #3
    Variable Bitrate Splash-X's Avatar
    Join Date
    Mar 2006
    Location
    Palm Harbor, FL
    Posts
    241
    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();
    }

    }

  4. #4
    Variable Bitrate
    Join Date
    Apr 2004
    Location
    The land of polar bears and nekkid blonds
    Posts
    446
    Quote 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;
    }

  5. #5
    Newbie
    Join Date
    May 2006
    Posts
    6
    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			
          }
    }

  6. #6
    Variable Bitrate Splash-X's Avatar
    Join Date
    Mar 2006
    Location
    Palm Harbor, FL
    Posts
    241
    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.

  7. #7
    Newbie
    Join Date
    May 2006
    Posts
    6
    I was just assuming that he needed instances. Your way is obviously the correct way if this is not the case.

  8. #8
    Variable Bitrate
    Join Date
    Apr 2006
    Location
    Dallas, Texas
    Posts
    264
    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.

Similar Threads

  1. Weird stuff you do during boring classes
    By Rafster in forum Off Topic
    Replies: 50
    Last Post: 06-19-2006, 05:42 PM
  2. classes?
    By craniumdesigns in forum Newbie
    Replies: 8
    Last Post: 07-20-2005, 09:31 PM
  3. Where are all these visitors linking from?
    By Vacheron in forum General MP3Car Discussion
    Replies: 19
    Last Post: 01-28-2005, 06:35 PM
  4. Linking headunit to car for radio
    By Scouse Monkey in forum Car Audio
    Replies: 1
    Last Post: 11-11-2004, 06:18 AM
  5. Replies: 0
    Last Post: 06-17-2004, 12:49 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •