Sign in to follow this  
djnevs

[.net] [c#] Some class void thing

Recommended Posts

djnevs    100
I wonder. How can i create a class like: cObject.Create.box(bla) cObject.Create.sphere(bla) cObject.Create.cilinder(bla) I have this atm:
public class cObject
{
    private Mesh obj;

    public void Create(Device device)
    {
        
        obj = Mesh.Box(device, 50.0f, 50.0f, 50.0f);

    }

    public void OnPaint()
    {

    }
}
But this is only a box. I want to split up the Create, to box sphere silinder file etc. Possable? If yes, how?

Share this post


Link to post
Share on other sites
Gage64    1235
Not sure I understand your question, but what about this:


public class cObject
{
private Mesh obj;

public void CreateBox(Device device, ...)
{
obj = Mesh.Box(device, ...);
}

public void CreateSphere(Device device, ...)
{
obj = Mesh.Sphere(device, ...);
}

public void CreateCylinder(Device device, ...)
{
obj = Mesh.Cylinder(device, ...);
}


public void OnPaint()
{

}
}

Share this post


Link to post
Share on other sites
djnevs    100
Well, I had that bevore. but it isnt really what I want..

I kinda want something with
cObject.Create.box(bla)
with the dot between Create and Box.

i thought something like:

public class cObject
{
private Mesh obj;

public void Create(Device device)
{
public void Box(){
obj = Mesh.Box(device, 50.0f, 50.0f, 50.0f);
}

}

public void OnPaint()
{

}
}



but that doesnt work..
hmm, im gonna try this:
public void Create.Box(Device device)

edit: hmm, doesnt work eigher..

Share this post


Link to post
Share on other sites
Krisc    494
Are you asking if you can just be able to call a method at random without it existing in the class? Your best bet to get this sort of functionality is to use a factory class with generics. This allows you to create new objects without having to do any work on the Factory class. Note that the Factory class probably shouldn't be a static class, but for simplicity's sake, it is in my example.


public static class Factory
{
public static object Create<T>(Device device) where T is IBuildable, new()
{
T _obj = new T();
T.Initialize(device);
}
}

public interface IBuildable
{
void Initialize(Device device);
Mesh Mesh { get; set; }
}

public class Box : IBuildable
{
private Mesh m_mesh = null;

public void Initialize(Device device)
{
Mesh = Mesh.Box(device, 50.0f, 50.0f, 50.0f);
}

public Mesh Mesh
{
get { return m_mesh; }
set { m_mesh = value; }
}
}



This will allow you to make a call like...

Box box = Factory.Create<Box>(device);


This design pattern works really well for more advanced classes where creating them by yourself with the constructor, etc. produces a lot of repeated lines of code.

Share this post


Link to post
Share on other sites
jpetrie    13106
System.Activator.CreateInstance(typeof(T),any,paramters,to,the,constructor).

This reduces the amount of work you need to do to storying a Dictionary<T,Type> where T is the key type, along with any other abstraction you'd like to make for usability, et cetera.

Share this post


Link to post
Share on other sites
k00k    272
Look up on DoFactory.com "Abstract Factory Pattern" for more information on this design pattern. it is sweet for doing such things like this.

Share this post


Link to post
Share on other sites
Krisc    494
Quote:
Original post by jpetrie
System.Activator.CreateInstance(typeof(T),any,paramters,to,the,constructor).

This reduces the amount of work you need to do to storying a Dictionary<T,Type> where T is the key type, along with any other abstraction you'd like to make for usability, et cetera.


Ok, how about this. This should let you just pass in the parameters.


public static class Factory
{
public static object Create<T>(params object[] parameters)
{
return System.Activator.CreateInstance(typeof(T), parameters);
}
}

public class Box
{
public Mesh mesh;
public Box(Device device, float length, float width, float height)
{
mesh = Mesh.Box(device, length, width, height);
}
}

public class Sphere
{
public Mesh mesh;
public Box(Device device, float radius)
{
mesh = Mesh.Sphere(device, radius);
}
}




This should allow you to make a call like...

Box box = Factory.Create<Box>(device, 50.0f, 50.0f, 50.0f);
Sphere sphere = Factory.Create<Sphere>(device, 50.0f);

Share this post


Link to post
Share on other sites
djnevs    100
Still not what i want actually, but i can live with this too :)
if someone else knows how do to it the way i want, please let me know..
untill then i'll be using Krisc's solution..

Thanks to you all :-)

Share this post


Link to post
Share on other sites
djnevs    100
Hmm, one little problem i think..

I need to use 2 of these Instances.

One for Create (init)
and one for OnPaint (draw)
but i cant use the "mesh" in the onpaint stuff, because its declared in Create.

Share this post


Link to post
Share on other sites
TheTroll    883
Going to make a suggestion;

Create your cOjbect base class.

Then create derived objects based on it.


public class cObject
{
protected Mesh obj;

public virtual void Init(Device device)
{

// Init code

}

public virtual void OnPaint()
{
//Drawing code.
}
}

public class Box : cObject
{
public override void Init(Device device)
{
// Create your box mess
}

public override void OnPaint()
{
// your box drawing code.
}
}





This will allow you to create any object and add objects without messing with the code you already have working.

theTroll

Share this post


Link to post
Share on other sites
BorgPidgeon    129
You said you wanted:

cObject.Create.box(bla)
cObject.Create.sphere(bla)
cObject.Create.cilinder(bla)

Here is the pseudo code:

public class cObject
{
public Create Create = new Create();
class Create
{
public void box(object obj)
{
// your drawing code here
}
public void sphere(object obj)
{
// your drawing code here
}
public void cylinder(object obj)
{
// your drawing code here
}
}
}



You would then access it through the following code:


cObject temp = new cObject();
temp.Create.box(blah);
temp.Create.sphere(blah);
temp.Create.cylinder(blah);


Share this post


Link to post
Share on other sites
Spoonbender    1258
If you want the exact syntax you described, make Create a class.


class cObject {
public static class Create {
public static Box box(...){}
public static Cylinder cylinder(...){}
public static Sphere sphere(...){}
}
}

Then you can call it the way you described.

However, the sane way of doing it would probably be with generics, as someone said above.
Then the syntax becomes cObject.Create<Box>() (where Create is a method again)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this