The MP3car.com Store  

Welcome to the MP3Car.com forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. Registering will also remove advertisements. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   MP3Car.com > Mp3Car Technical > Software & Software Development > Coders Corner

Reply
 
Thread Tools Search this Thread Display Modes
Old 05-06-2006, 06:12 PM   #1
Variable Bitrate
 
Join Date: Apr 2004
Location: The land of polar bears and nekkid blonds
Posts: 446
My Photos: (0)
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); } }

Maximus is offline   Reply With Quote
Sponsored Links
Old 05-12-2006, 10:51 PM   #2
Newbie
 
Join Date: May 2006
Vehicle: 2004 / Volkswagen / R32
Posts: 5
My Photos: (0)
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

Last edited by skrewdlude; 05-13-2006 at 01:24 AM.
skrewdlude is offline   Reply With Quote
Old 05-13-2006, 08:33 AM   #3
Variable Bitrate
 
Splash-X's Avatar
 
Join Date: Mar 2006
Location: Palm Harbor, FL
Vehicle: 2007 Mazda 3
Posts: 241
My Photos: (0)
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();
}

}
Splash-X is offline   Reply With Quote
Old 05-13-2006, 09:23 AM   #4
Variable Bitrate
 
Join Date: Apr 2004
Location: The land of polar bears and nekkid blonds
Posts: 446
My Photos: (0)
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;

}
Maximus is offline   Reply With Quote
Old 05-13-2006, 10:50 AM   #5
Newbie
 
Join Date: May 2006
Vehicle: 2004 / Volkswagen / R32
Posts: 5
My Photos: (0)
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 } }


Last edited by skrewdlude; 05-13-2006 at 11:00 AM.
skrewdlude is offline   Reply With Quote
Old 05-14-2006, 07:20 PM   #6
Variable Bitrate
 
Splash-X's Avatar
 
Join Date: Mar 2006
Location: Palm Harbor, FL
Vehicle: 2007 Mazda 3
Posts: 241
My Photos: (0)
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.
Splash-X is offline   Reply With Quote
Old 05-14-2006, 07:46 PM   #7
Newbie
 
Join Date: May 2006
Vehicle: 2004 / Volkswagen / R32
Posts: 5
My Photos: (0)
I was just assuming that he needed instances. Your way is obviously the correct way if this is not the case.
skrewdlude is offline   Reply With Quote
Old 06-10-2006, 02:17 AM   #8
Constant Bitrate
Ineffigy's CarPC Specs
 
Join Date: Apr 2006
Location: Dallas, Texas
Vehicle: 2005 Toyota Celica GTS 6
Posts: 222
My Photos: (0)
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.
Ineffigy is offline   Reply With Quote
Sponsored Links
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird stuff you do during boring classes Rafster Off Topic 50 06-19-2006 04:42 PM
classes? craniumdesigns Newbie 8 07-20-2005 08:31 PM
Where are all these visitors linking from? Vacheron General MP3Car Discussion 19 01-28-2005 05:35 PM
Linking headunit to car for radio Scouse Monkey Car Audio 1 11-11-2004 05:18 AM
NOPI NATIONALS Motorsport Supershow - Atlanta - Audio Classes Announced: NOPIMAN MP3Car Gatherings 0 06-16-2004 11:49 PM


All times are GMT -5. The time now is 08:56 AM.


Sponsored Links
The MP3car.com Store

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 1999 - 2008 Mp3Car.com Inc.
Ad Management by RedTyger
Message Board Statistics