Jump to content
  • Advertisement
Sign in to follow this  
djnevs

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

This topic is 3999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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()
{

}
}

Share this post


Link to post
Share on other sites
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
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
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!