|
||
|
|
#1 (permalink) |
|
Forum Expert
|
OK, this one is hard even to describe, so I will post some pseudo code to illustrate.
Code:
using System;
namespace MyWorld
{
public class BaseShape : Item
{
//code here
}
}
Code:
using System;
namespace MyWorld
{
public class BaseSquare : BaseShape
{
//code here
}
}
Code:
using System;
namespace MyWorld
{
public class BaseCircle : BaseShape
{
//code here
}
}
Code:
using System;
namespace MyWorld
{
public class BaseTriangle : BaseShape
{
//code here
}
}
Am I missing something easy? [/code] Code:
using System;
namespace MyWorld
{
public class DerivedSquare : BaseSquare
{
public Type TriangleType;
public Type CircleType;
// HERE IS WHERE I WANT TO CREATE SOME ITEMS
Item i = new Item; //not what I want.... I want somehow to call typeof(TriangleType) or something...??
Item j = new Item;
}
}
|
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Posts: 1,754
|
The .NET Framework provides a method for doing this: Activator.CreateInstance( Type type )
You could implement as such: Code:
using System;
namespace MyWorld
{
public class DerivedSquare : BaseSquare
{
public Type TriangleType = typeof( Triangle );
public Type CircleType = typeof( Circle );
TriangleType i = null;
try{ i = Activator.CreateInstance( TriangleType ) as TriangleType; }
catch{}
if( i != null )
//creation was successful
}
}
__________________
"Misfortune shows those who are not really friends." -Aristotle "A multitude of words is no proof of a prudent mind." -Thales "Conceal a flaw, and the world will imagine the worst." -Marcus Martialis
Last edited by Khaz; 05-02-2007 at 02:30 PM. Reason: syntax fix |
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
|
One problem.....
TriangeType is not a class, its a variable.... TriangleType is a Type variable. That Type could be DerivedTriangle1, DerivedTriangle2, etc. I can't use: Code:
TriangeType i = null; And I don't want to use: Code:
DerivedTriange1 i = null;
try{ i = Activator.CreateInstance( typeof( DerivedTriange1 ) ) as DerivedTriange1; }
Help! |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Posts: 1,754
|
Use object instead, then:
Code:
object i = null;
try{ i = Activator.CreateInstance( TriangleType ); }
catch{}
if( i != null ) {
//creation successful, start checking via reflection...
if( i is DerivedTriangle1 ) {
//yay
} else if( i is DerivedTriangle2 ) {
//yay #2
}
}
|
|
|
|
|
|
#6 (permalink) |
|
Forum Expert
|
Thank you again.
Now, lets move this into RunUO terms, if you don't mind. An example a little closer to home...(this is not my real code, but simulates the concept.) Tell me if this code would work for what I am trying to do: PHP Code:
|
|
|
|
|
|
#7 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Posts: 1,754
|
Close. Highlighted recommended changes:
Code:
public class CreateMobile
{
public Type MobileType { get{ return m_MobileType; } set{ m_MobileType = value; } }
public void DoCreate( Mobile from )
{
Mobile m = null;
try { m = Activator.CreateInstance( MobileType ) as Mobile; }
catch{}
if( m != null ) //creation was successful
{
if ( m.Combatant != null && m is from.Combatant.GetType() ) //If we are fighting the same type of Monster...
from.SendGump( new SuccessGump( from, m ) );
}
}
}
|
|
|
|
|
|
#8 (permalink) |
|
ConnectUO Creator
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,757
|
You are looking at this all wrong, or I am not understanding, But it seems to me you want to do something like this instead.
Code:
public class DerivedSquare : BaseSquare
{
public BaseShape TriangleType;
public BaseShape CircleType;
public DerivedSquare()
{
//You can make TriangleType anything you want since all shapes derive from the same BaseShape
TiangleType = new BaseTriangle();
//Or even this if you REALLY wanted to
TiangleType = new BaseCircle();
}
__________________
Jeff Boulanger ConnectUO - Core Developer Want to help make ConnectUO better? Click here to submit your ideas/requests Use your talent to compete against other community members in RunUO hosted coding competitions If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team. Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|