Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Going more agnostic
Message
From
10/09/2009 20:39:52
 
 
To
10/09/2009 11:33:21
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01422695
Message ID:
01423568
Views:
46
Hi Jim,

I think I need more info as to what exactly you're trying to do before I can make any additional suggestions. Sorry, I don't yet understand what it is that you want to accomplish.

~~Bonnie




>Hi Bonnie,
>
>Well, I gave it a good shot, but it is not the right solution. The problem is that interfaces cannot contain fields. It is these very fields that I need to be consistent across all instantiations.
>
>I tried to simply create a base class out of a user control, but when I inherit it the designer surface is lost.
>
>Do you have any ideas on how to inherit a user control such that each sub class has its own design surface?
>
>>Thanks Bonnie,
>>
>>I forgot how powerful interface programming can be. I think I can make it work. Will let you know.
>>
>>Thanks again,
>>
>>Jim
>>
>>
>>>Jim,
>>>
>>>You can probably handle this by using Reflection and Interfaces, since you say you have the classname.
>>>
>>>So, using an Interface (call it IElement, which have all the common properties/methods your elements need to use) and a Reflection class (a simplified version of a class I use is shown below), you should be able to use code something like this:
>>>
>>>
>>>public void AddElement(string ElementName)
>>>{
>>>    int elementNo = 0;
>>>
>>>    // Assuming you already know the assembly name, or it can be another parameter
>>>    MyReflectionClass oReflection = new MyReflectionClass(assembly, ElementName);
>>>    string message = "";
>>>    IElement o = oReflection.InstantiateClass(ref message) as IElement
>>>    if (o != null)
>>>    {
>>>       // where ElementMethods is part of your IElement interface
>>>        o.ElementMethods += new ElementMethodsDelegate(element_ElementMethods);
>>>        // etc.etc.
>>>    }
>>>}
>>>
>>>
>>>And here's a simplified version of a Reflection class I've used:
>>>
>>>
>>>public class MyReflectionClass
>>>{
>>>	#region Declarations
>>>	private string m_AssemblyName;
>>>	private string m_ClassName;
>>>	#endregion
>>>
>>>	#region Constructors
>>>	public MyReflectionClass()
>>>	{
>>>	}
>>>	/// Make sure that you have either included the full path to your Assembly file
>>>	/// or that you modify this class to include code for obtaining the full path
>>>	public MyReflectionClass(string assemblyName, string className)
>>>	{
>>>		this.AssemblyName = assemblyName;
>>>		this.ClassName = className;
>>>	}
>>>	#endregion
>>>
>>>	#region Methods
>>>
>>>	public Assembly LoadAssembly(ref string Message)
>>>	{
>>>		Assembly oAssembly = null;
>>>
>>>		try
>>>		{
>>>			oAssembly = Assembly.LoadFrom(this.m_AssemblyName + ".DLL");
>>>		}
>>>		catch (System.IO.FileNotFoundException)
>>>		{
>>>			Message = this.m_AssemblyName + " could not be found at the specified URL!" + (char)13 + (char)13 +
>>>				"Check that you have correctly entered the component URL and that your network or Internet " +
>>>				"connection is functioning correctly.";
>>>			return oAssembly;
>>>		}
>>>		catch (System.BadImageFormatException)
>>>		{
>>>			Message = this.m_AssemblyName + " is invalid or damaged!" + (char)13 + (char)13 +
>>>				"Contact your system administrator.";
>>>			return oAssembly;
>>>		}
>>>		catch (System.Exception ex)
>>>		{
>>>			Message = ex.Message;
>>>			return oAssembly;
>>>		}
>>>
>>>		return oAssembly;
>>>	}
>>>
>>>	public object InstantiateClass(ref string Message)
>>>	{
>>>		Assembly oAssembly = null;
>>>		return this.InstantiateClass(ref oAssembly, ref Message);
>>>	}
>>>	public object InstantiateClass(ref Assembly oAssembly, ref string Message)
>>>	{
>>>		object oClass = null;
>>>		if (oAssembly == null || oAssembly.FullName.Contains(this.m_AssemblyName) == false)
>>>			oAssembly = this.LoadAssembly(ref Message);
>>>
>>>		try
>>>		{
>>>			// Create an instance of the desired type from the assembly
>>>			if (oAssembly != null)
>>>				oClass = oAssembly.CreateInstance(this.m_ClassName);
>>>		}
>>>		catch (Exception ex)
>>>		{
>>>			Message = ex.Message;
>>>			return oClass;
>>>		}
>>>
>>>		return oClass;
>>>	}
>>>
>>>	#endregion
>>>
>>>	#region Properties
>>>	public string AssemblyName
>>>	{
>>>		get { return this.m_AssemblyName; }
>>>		set
>>>		{
>>>			this.m_AssemblyName = value.Trim();
>>>			if (this.m_AssemblyName.ToUpper().EndsWith(".DLL", true, null))
>>>				this.m_AssemblyName = this.m_AssemblyName.Remove(this.m_AssemblyName.Length - 4);
>>>		}
>>>	}
>>>	public string ClassName
>>>	{
>>>		get { return this.m_ClassName; }
>>>		set 
>>>		{ 
>>>			this.m_ClassName = value.Trim(); 
>>>			if (this.m_ClassName.Contains(this.m_AssemblyName) == false)
>>>				this.m_ClassName = this.m_AssemblyName + "." + this.m_ClassName;
>>>		}
>>>	}
>>>	#endregion
>>>}
>>>
>>>
>>>Does this help at all?
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>
>>>
>>>>Hi All,
>>>>
>>>>In the following code segment I pass an object and an enum. The object is a class and the enum is the user defined type
>>>>or simply the class name. The switch statement then determines the type and creates an object of type newElement. The only reason for the switch statement is to determine which class to instantiate.
>>>>
>>>>        public void AddElement(object newElement, Enums.ElementType t)
>>>>        {
>>>>            int elementNo = 0;
>>>>            switch (t)
>>>>            {
>>>>                case Enums.ElementType.Bit:                    
>>>>                    Bit element = new Bit();
>>>>                    element.Location = this._ConnectionPoint;
>>>>                    element.ElementMethods += new ElementMethodsDelegate(element_ElementMethods);
>>>>                    foreach (Control c in this.Controls)
>>>>                    {
>>>>                        if (c.GetType().Equals(typeof(Bit)))
>>>>                        {
>>>>                            element.Left = c.Right;
>>>>                            elementNo++;
>>>>                        }
>>>>                    }
>>>>                    element.ElementNumber = elementNo;
>>>>                    this.Controls.Add(element);
>>>>                    Elements.Insert(Elements.Count - 1, element);
>>>>                    break;
>>>>
>>>>            }
>>>>
>>>>            this.Controls.Clear();
>>>>            int numElement = 0;
>>>>            int right = 0;
>>>>            foreach (Control c in Elements)
>>>>            {
>>>>                Bit b = (Bit)c;
>>>>                b.ElementNumber = numElement;
>>>>                b.Left = right;                    
>>>>                this.Controls.Add(b);
>>>>                numElement++;
>>>>                right = b.Right;
>>>>            }
>>>>        }
>>>>
>>>>
>>>>
>>>>The statement I want to change to be more agnostic is
>>>>
>>>>Bit element = new Bit();
>>>>
>>>>
>>>>I want to do something like
>>>>
>>>>newElement.Name element = new newElement.Name();
>>>>
>>>>
>>>>If thereis a way to do this it would eliminate the need for a switch statement with up to 30 or more case statements.
>>>>
>>>>Thanks,
>>>>
>>>>Jim
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform