Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 05-02-2007, 01:41 PM   #1 (permalink)
Forum Expert
 
Lokai's Avatar
 
Join Date: Aug 2003
Location: Bergen, NY (Rochester)
Age: 41
Posts: 1,411
Send a message via ICQ to Lokai Send a message via MSN to Lokai Send a message via Yahoo to Lokai
Default How to create Items based on a Type

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
   }
}
Now, what I want to do is based on this next bit of code, so its the most important part. Lets pretend that I have many classes that are all Shapes. Some of them are Derived from Square, some from Circle, and some from Triangle. The one below is Derived from Square. I have named it DerivedSquare1 for simplicity. It has 2 variables, both Types, because they can be any type of Shape, one based on Circle, and the other based on Triangle. But I do not know which during Scripting, because there are many, like DerivedTriangle1, DerivedTriangle2, etc. SO HERE IS THE QUESTION: How do I create the items based on a Type? I don't just want to create BaseTriangle and BaseCircle, because they don't have all the code I need, it has to be specific Types which are derived from the base types.

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;
   }
}
Lokai is offline   Reply With Quote
Old 05-02-2007, 01:49 PM   #2 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default

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
	}
}
You have to cast the value returned from CreateInstance because it is returned as an Object. It is also a good idea to use the try/catch as this method is not always "safe."
__________________
"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
Khaz is offline   Reply With Quote
Old 05-02-2007, 01:52 PM   #3 (permalink)
Forum Expert
 
Lokai's Avatar
 
Join Date: Aug 2003
Location: Bergen, NY (Rochester)
Age: 41
Posts: 1,411
Send a message via ICQ to Lokai Send a message via MSN to Lokai Send a message via Yahoo to Lokai
Default

You rock!

Karma++ (or should that be Karma# ?)
Lokai is offline   Reply With Quote
Old 05-02-2007, 02:09 PM   #4 (permalink)
Forum Expert
 
Lokai's Avatar
 
Join Date: Aug 2003
Location: Bergen, NY (Rochester)
Age: 41
Posts: 1,411
Send a message via ICQ to Lokai Send a message via MSN to Lokai Send a message via Yahoo to Lokai
Unhappy

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;
because TriangleType is not a class or Type, its a variable.

And I don't want to use:

Code:
 
DerivedTriange1 i = null;
try{ i = Activator.CreateInstance( typeof( DerivedTriange1 ) ) as DerivedTriange1; }
because I don't know if it's DerivedTriange1 or not.

Help!
Lokai is offline   Reply With Quote
Old 05-02-2007, 02:29 PM   #5 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default

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
	}
}
__________________
"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


Khaz is offline   Reply With Quote
Old 05-02-2007, 04:27 PM   #6 (permalink)
Forum Expert
 
Lokai's Avatar
 
Join Date: Aug 2003
Location: Bergen, NY (Rochester)
Age: 41
Posts: 1,411
Send a message via ICQ to Lokai Send a message via MSN to Lokai Send a message via Yahoo to Lokai
Default

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:
public class CreateMobile
{
            public 
Type MobileType get{ return m_MobileType; } setm_MobileType value; } }
 
            public 
void DoCreateMobile from )
            {
                        
Mobile m null;
                        try { 
Activator.CreateInstanceMobileType ); }
                        catch{}
                        if( 
!= null //creation was successful
                        
{
                                    if ( 
m is from.Combatant //If we are fighting the same type of Monster...
                                                
from.SendGump( new SuccessGumpfrom) );
                        }
            }

Lokai is offline   Reply With Quote
Old 05-02-2007, 04:36 PM   #7 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Posts: 1,754
Default

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 ) );
                        }
            }
}
__________________
"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


Khaz is offline   Reply With Quote
Old 05-03-2007, 12:39 AM   #8 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,757
Default

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();
		}
Make sense?
__________________
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
Jeff is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5