Export (0) Print
Expand All

INameCreationService Interface

Provides a service that can generate unique names for objects.

Namespace: System.ComponentModel.Design.Serialization
Assembly: System (in system.dll)

public interface INameCreationService
public interface INameCreationService
public interface INameCreationService
Not applicable.

A DesignerLoader can implement this service to provide a way for a designer to create new, unique names for objects. If this service is not available, the designer uses a default implementation.

The following example code provides an example INameCreationService implementation. The service can create a unique name based on a type that does not match any names in the specified container. It can also validate a specified name string.

using System;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Globalization;

namespace NameCreationServiceExample
{
    public class NameCreationService : System.ComponentModel.Design.Serialization.INameCreationService
    {
        public NameCreationService()
        {
        }

        // Creates an identifier for a particular data type that does not conflict 
        // with the identifiers of any components in the specified collection.
        public string CreateName(System.ComponentModel.IContainer container, System.Type dataType)
        {
            // Create a basic type name string.
            string baseName = dataType.Name;
            int uniqueID = 1;

            bool unique = false;            
            // Continue to increment uniqueID numeral until a 
            // unique ID is located.
            while( !unique )
            {
                unique = true;
                // Check each component in the container for a matching 
                // base type name and unique ID.
                for(int i=0; i<container.Components.Count; i++)
                {
                    // Check component name for match with unique ID string.
                    if( container.Components[i].Site.Name.StartsWith(baseName+uniqueID.ToString()) )
                    {
                        // If a match is encountered, set flag to recycle 
                        // collection, increment ID numeral, and restart.
                        unique = false;
                        uniqueID++;
                        break;
                    }
                }
            }
            
            return baseName+uniqueID.ToString();
        }

        // Returns whether the specified name contains 
        // all valid character types.
        public bool IsValidName(string name)
        {            
            for(int i = 0; i < name.Length; i++)
            {
                char ch = name[i];
                UnicodeCategory uc = Char.GetUnicodeCategory(ch);
                switch (uc) 
                {
                    case UnicodeCategory.UppercaseLetter:       
                    case UnicodeCategory.LowercaseLetter:     
                    case UnicodeCategory.TitlecaseLetter:                                                  
                    case UnicodeCategory.DecimalDigitNumber:                         
                        break;
                    default:
                        return false;                
                }
            }
            return true;        
         }

        // Throws an exception if the specified name does not contain 
        // all valid character types.
        public void ValidateName(string name)
        {
            for(int i = 0; i < name.Length; i++)
            {
                char ch = name[i];
                UnicodeCategory uc = Char.GetUnicodeCategory(ch);
                switch (uc) 
                {
                    case UnicodeCategory.UppercaseLetter:       
                    case UnicodeCategory.LowercaseLetter:     
                    case UnicodeCategory.TitlecaseLetter:                                                  
                    case UnicodeCategory.DecimalDigitNumber:                         
                        break;
                    default:
                        throw new Exception("The name '"+name+"' is not a valid identifier.");                
                }
            }
        }
     }
}

package NameCreationServiceExample; 

import System.*;
import System.ComponentModel.Design.*;
import System.ComponentModel.Design.Serialization.*;
import System.Globalization.*;

public class NameCreationService 
    implements System.ComponentModel.Design.Serialization.INameCreationService
{
    public NameCreationService()
    {
    } //NameCreationService

    // Creates an identifier for a particular data type that does not conflict 
    // with the identifiers of any components in the specified collection.
    public String CreateName(System.ComponentModel.IContainer container, 
        System.Type dataType)
    {
        // Create a basic type name string.
        String baseName = dataType.get_Name();
        int uniqueID = 1;
        boolean unique = false;

        // Continue to increment uniqueID numeral until a 
        // unique ID is located.
        while (!(unique)) {
            unique = true;

            // Check each component in the container for a matching 
            // base type name and unique ID.
            for (int i = 0; i < container.get_Components().get_Count(); i++) {
                // Check component name for match with unique ID string.
                if (container.get_Components().get_Item(i).get_Site().
                    get_Name().StartsWith((baseName + ((Int32)uniqueID).
                    ToString()))) {
                    // If a match is encountered, set flag to recycle 
                    // collection, increment ID numeral, and restart.
                    unique = false;
                    uniqueID++;
                    break;
                }
            }
        }
        return (baseName + ((Int32)uniqueID).ToString());
    } //CreateName

    // Returns whether the specified name contains 
    // all valid character types.
    public boolean IsValidName(String name)
    {
        for (int i = 0; i < name.length(); i++) {
            char ch = name.get_Chars(i);
            UnicodeCategory uc = Char.GetUnicodeCategory(ch);
            switch (uc) {
                case UnicodeCategory.UppercaseLetter:
                case UnicodeCategory.LowercaseLetter:
                case UnicodeCategory.TitlecaseLetter:
                case UnicodeCategory.DecimalDigitNumber:
                    break;

                default:
                    return false;
            }
        }
        return true;
    } //IsValidName

    // Throws an exception if the specified name does not contain 
    // all valid character types.
    public void ValidateName(String name)throws java.lang.Exception
    {
        for (int i = 0; i < name.length(); i++) {
            char ch = name.get_Chars(i);
            UnicodeCategory uc = Char.GetUnicodeCategory(ch);
            switch (uc) {
                case UnicodeCategory.UppercaseLetter:
                case UnicodeCategory.LowercaseLetter:
                case UnicodeCategory.TitlecaseLetter:
                case UnicodeCategory.DecimalDigitNumber:
                    break;

                default:
                    throw new Exception("The name '" + name 
                        + "' is not a valid identifier.");
            }
        }
    } //ValidateName
} //NameCreationService 

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft