views:

547

answers:

3

I am going to aplogize in advance because I am really at the limits of my understanding on this so if I do not explain this well....well sorry...

Anyway I am trying to create an asp.net server control that has complex properties which can be set using intellisense. So as an example I will use cars, so the server control might be called car and when I actually implement the control on a webform I want to set complex, hierarchical properties so for example:

<Control:Car Color="Paint.Metalic.CandyRed" 
             Wheels="Forged.Aluminun.FiveSpoke.GunMetal" />

or

<Control:Car Color="Paint.Matte.Yellow"
             Wheels="Forged.Steel.SevenSpoke.BareMetal" />

I have tried creating public properties in the server control that are just types/classes that point to the base class but intellisense doesn't come up with anything. I can use a straight forward enum and that will show up but I can't do anything hierarchical that way. I've been looking for examples but I can't seem to find anything. Any help would be greatly appreciated!!!!

Thank you!

How about a different example as it seems the relationship between items or their intended value, although completely unimportant, seems to be an issue.

Let's take the relationships between continent/ country / state / city / etc.... By this example, if my custom server control is called "Location" then I would like to be able to ...

<Control:Location CurrentLocation="UnitedStates.Nebraska.Lincoln" />

or

<Control:Location CurrentLocation="Europe.Italy.Napoli" />
A: 

These items would need to be enums as that is the only way that it is supported at least in everything I have seen. now, you can accomplish what you want with a few enums.

namespace Paint
{
    public enum Metalic
    {
        CandyRed
    }

    public enum Matte
    {
        Yellow
    }
}

Granted, not perfect, but easy to document and understand!

Mitchel Sellers
I think that would work but how do I expose a property of type paint if paint is a namespace?
Blaxer
A: 

As Mitchel Sellers posted, they will need to be either Enum or Constants/Statics.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Paint
{
    public class Metallic
    {
        public static Color CandyRed
        {
            get { return Color.Red; }
        }

        public static Color CandyGreen
        {
            get { return Color.Green; }
        }
    }

    public class Matte
    {
        public static Color Red
        {
            get { return Color.Red; }
        }

        public static Color Green
        {
            get { return Color.Green; }
        }
    }
}
Tom Anderson
Can you give me an example please?
Blaxer
added code sample
Tom Anderson
This is what I need but how do I expose this as a property at the namespace level, meaning by this example, when using the control on a webform I want to by able to have intellisense show me paint="Metalic.CandyRed" and show all the available "choices"
Blaxer
@Blaxer - You need to then put it in a namspace and import that namespace for your .aspx
Mitchel Sellers
+7  A: 

Your problem is worse than the previous two answers suggest: you don't know what you're doing.

Sorry to sound so harsh, but where did you ever see something like "Paint.Metalic.CandyRed", and what did it mean there? Or "Forged.Aluminun.FiveSpoke.GunMetal"? What do you even want that to mean?

First, figure out what you want to represent. Then, create a class that can represent it. Then, add a property of that class to the server control. You may have to add a TypeConverter or other designer support in order for ASP.NET to convert your preferred textual representation into an instance of the class. Otherwise, you'll be able to get something like the properties of a Font.


I'm going to make a guess about what some of these values represent, and try to show you how to deal with them in a control. My guess could be far off, though.

I'll work with "Paint.Metalic.CandyRed". I'll assume this applies to the domain of automobile customization, and that the Color property is meant to represent the finish given to the car as a whole. In that case, "Paint" would probably be an enum referring to the type of finish (though I don't know what other sorts of finish apply to a car!). I know from building model cars when I was a kid that paints may be metallic, or gloss, or flat, so those three would be enum values of one enum. "CandyRed" would be one of many colors. This would give something like this:

public enum FinishType
{
    Paint,
    NotPaint // _I_ don't know!
}

public enum PaintFinish
{
    Metallic,
    Gloss,
    Flat
}

public enum CarColor
{
    CandyRed,
    SilverMist,
    DesertSandMica,
    MagneticGray,
    // etc.
}


public class CarFinish
{
    public FinishType FinishType {get;set;}
    public PaintFinish PaintFinish {get;set;}
    public CarColor CarColor {get;set;}
}

public class Car : WebControl
{
    public CarFinish Color {get;set;}
}

This would allow for something like this:

<Control:Car Color-FinishType="Paint"
             Color-PaintFinish="Metallic"
             Color-CarColor="CandyRed" .../>

or this:

<Control:Car ...>
    <Color FinishType="Paint" PaintFinish="Metallic" CarColor="CandyRed"/>
</Control:Car>
John Saunders
Thanks for the help.....
Blaxer
I hope it helps. Sorry, not in the best mood today. Likely to be blunt for the next few hours or so. ;-)
John Saunders
Do you think Mitchel and Tom were incorrect? Their answers made sense but I need more detail to understand it fully.
Blaxer
It looks to me like you haven't really thought through what something like "Forged.Steel.SevenSpoke.BareMetal" means. Is that material+appearance+finish? They thought you had a syntax problem. I think you have a conceptual problem to work on.
John Saunders
It was just an example, Thank you for the code that makes perfect sense but there is no way to combine the "properties", by your example paint.metallic.candyred so that you understand the relationships within intellisense?
Blaxer
Please see updated example at the top
Blaxer
.NET has no built-in syntax for describing sets of objects that exist in a hierarchy. Even if the Paint.Metallic.CandyRed hierarchy did exist, there is no standard way to retrieve that information from a page or control.
John Saunders
Is there some way to do it, as you suggested, using some kind of design-time support?
Blaxer
Yes. But as I said. Before you can use designer support, you first have to decide on the classes to represent the data, then decide what they mean and how you want them to look. You can then create a TypeConverter to convert them to and from string.
John Saunders