Passing Current Device to other Classes

Jelly Donut Man

Newcomer
Joined
Feb 1, 2003
Messages
22
Location
South Flordia
I'm trying to set up separate classes to handle different parts of the engine, but I'm having a problem trying to initialize the things with out the Device that I'm using with the rest of Direct3D. I don't want to set the device as public so that it shows up in the application using the engine. I've seen other engines achieve this not sure of how they did but they have multiple classes and everything runs smooth.
 
Last edited:
I think you should do the main initialization method only in one class (the CMain class, for example).
Passing the Device to other classes can be achieved by using "creation" method inside this CMain class:
Let's suppose you have these 4 classes -
1. CMain
2. COne
3. CTwo
4. CThree

2,3,4 all have a creator method that requires passing of the Device by reference.

So, in CMain, you define
createOne() as COne
createTwo() as CTwo
createThree() as CThree
and in these methods you create an object of desided class by passing the Device by ref.
 
Last edited:
In the constructor of your secondary classes you'll want to add the device as an argument. Each class can then keep a reference to the main class's device.

For example, this would be your main class that defines the device. It assumes there's a class called Class2 that needs access to the device.
C#:
public class mainClass
{
    private Device dev;
    
    public mainClass()
    {
        // This class's constructor, create the device
        dev = new Device(...);

        // Create class 2, passing in the device
        Class2 c = new Class2(dev);
    }
}

Here's what Class2 might look like:
C#:
public class Class2
{
    private Device myDev;

    public Class2(Device dev)
    {
        this.myDev = dev;
    }
}

Now Class2 can use the device wherever it needs it.

-nerseus
 
Ah, I think you have to use the New() function in VB.NET. The default constructor takes no params, so you end up with New(). You can change that to New(ByRef dev As Device) I think - not 100% about VB.NET :)

-nerseus
 
Umm.. but wouldn't I call something like that in the Class 2? How am I getting it to the class 2 so I can ref it? Just give me a simple code again in your best VB syntax for each part :) .. Not sure if I get it in the C# way :-\
 
Last edited:
Ok, here goes (may not work exactly right):
Visual Basic:
Public Class mainClass
   Private device As Device
    
    public Sub New()
        ' This class's constructor, create the device
        device = New Device(...)

        ' Create class 2, passing in the device
        Dim c As Class2 = New Class2(device)
    End Sub
End Class

And the second class...
Visual Basic:
Public Class Class2
    Private myDev As Device
    Public Sub New(ByRef dev As Device)
        Me.myDev = dev
    End Sub

    Private Sub UseDevice()
        'You can use myDev here...
    End Sub
End Class


-nerseus
 
As the syntax of the code is correct. The usage of it within the application that I use the DLL is not working as expected. Say if I were to declare my Class 2 in the application as follows.
Visual Basic:
Dim Class2 as New Class2
It is telling me that I need to pass it the device right there when I declare it as New. This is what it is asking me for.
Visual Basic:
Dim Class2 as New Class2(Device)
I thought that making the MainClass do
Visual Basic:
Dim c As Class2 = New Class2(device)
Would pass it the device ..... :-\ I'm not sure whats going on here.
 
Sorry, I can't understand your problem.
The correct syntax is:
Visual Basic:
Dim c2 as New Class2(mDev)
or
Visual Basic:
Dim c2 as Class2 = New Class2(mDev)

So, what's wrong?
 
Well, I'm trying to pass my device and create it with in the .dll, and when I try to make the engine, and something else using the device from the engine, it wants me to pass it the device from the application that is using the engine. At least the way Nerseus is showing me.
 
Back
Top