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

Started by
10 comments, last by Spoonbender 16 years, 8 months ago
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?
Advertisement
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()    {    }}
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..
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.
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.
Look up on DoFactory.com "Abstract Factory Pattern" for more information on this design pattern. it is sweet for doing such things like this.
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);
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 :-)
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.
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

This topic is closed to new replies.

Advertisement