Click here to Skip to main content
15,905,785 members
Home / Discussions / C#
   

C#

 
GeneralRe: Any alternative Pin
Ennis Ray Lynch, Jr.21-Jan-09 8:31
Ennis Ray Lynch, Jr.21-Jan-09 8:31 
GeneralRe: Any alternative Pin
CodingYoshi21-Jan-09 8:42
CodingYoshi21-Jan-09 8:42 
GeneralRe: Any alternative Pin
Ennis Ray Lynch, Jr.21-Jan-09 9:03
Ennis Ray Lynch, Jr.21-Jan-09 9:03 
GeneralRe: Any alternative Pin
Jon Rista21-Jan-09 10:13
Jon Rista21-Jan-09 10:13 
GeneralRe: Any alternative Pin
Ennis Ray Lynch, Jr.21-Jan-09 10:14
Ennis Ray Lynch, Jr.21-Jan-09 10:14 
AnswerRe: Any alternative Pin
DaveyM6921-Jan-09 7:34
professionalDaveyM6921-Jan-09 7:34 
AnswerRe: Any alternative Pin
PIEBALDconsult21-Jan-09 7:39
mvePIEBALDconsult21-Jan-09 7:39 
AnswerRe: Any alternative Pin
Jon Rista21-Jan-09 10:27
Jon Rista21-Jan-09 10:27 
That is indeed hard coding...and its best to avoid it whenever possible. An alternative to a hard coded string, enum, etc., you can use inheritance to solve the problem. Assuming that the TransportationType table is just a lookup of entries that classify records in other tables, you could do something like the following:

abstract class TransportationMedium
{
    // Common transportation medium functionality and data here
    abstract string Name { get; }
    abstract void Transport();
}

class Vessel: TransportationMedium
{    
    // Extend common stuff from base
    override string Name { get { return "Vessel"; } }

    override void Transport()
    {
        // Vessel-specific transport implementation
    }
}

class Air: TransportationMedium
{
    // Extend common stuff from base
    override string Name { get { return "Air"; } }

    override void Transport()
    {
        // Air-specific transport implementation
    }
}

class Land: TransportationMedium
{
    // Extend common stuff from base
    override string Name { get { return "Land"; } }

    override void Transport()
    {
        // Land-specific transport implementation
    }
}

static class TransportationMediumFactory
{
    static TransportationMedium Construct(int typeID)
    {
        switch (typeID)
        {
            case 1: return new VesselMedium();
            case 2: return new AirMedium();
            case 3: return new LandMedium();
        }
    }

    static TransportationMedium Construct(string type)
    {
        switch (type)
        {
            case "Vessel": return new VesselMedium();
            case "Air": return new AirMedium();
            case "Land": return new LandMedium();
        }
    }
}

class Program
{
    void Main()
    {
        int mediumType = 0;
        string intput;

        Console.Write("Select medium (1=Vessel, 2=Air, 3=Land): ");
        input = Console.ReadLine();

        mediumType = Convert.ToInt32(input);

        TransportationMedium medium = TransportationMediumFactory.Construct(mediumType);

        Console.WriteLine("You chose " + medium.Name + " as your transportation medium.");
        
        medium.Transport();
    }
}


The factory in the implementation above still "hard codes" strings and ID's. This hard coding is centralized, and may be sufficient and maintainable if your application is small. If you need things to be more dynamic, you could easily pull out the mapping between type codes and ID's and the class implementations out into a configuration file, and change the factory implementation to dynamically, rather than statically, create the appropriate type via configuration. Then all hard coding is eliminated.
Questionparent child communication Pin
bfis10813721-Jan-09 6:35
bfis10813721-Jan-09 6:35 
AnswerRe: parent child communication Pin
PIEBALDconsult21-Jan-09 6:50
mvePIEBALDconsult21-Jan-09 6:50 
GeneralRe: parent child communication Pin
bfis10813721-Jan-09 7:09
bfis10813721-Jan-09 7:09 
GeneralRe: parent child communication Pin
PIEBALDconsult21-Jan-09 7:13
mvePIEBALDconsult21-Jan-09 7:13 
AnswerRe: parent child communication Pin
DaveyM6921-Jan-09 7:21
professionalDaveyM6921-Jan-09 7:21 
GeneralRe: parent child communication Pin
bfis10813722-Jan-09 3:32
bfis10813722-Jan-09 3:32 
GeneralRe: parent child communication Pin
DaveyM6922-Jan-09 4:53
professionalDaveyM6922-Jan-09 4:53 
GeneralRe: parent child communication Pin
bfis10813722-Jan-09 4:56
bfis10813722-Jan-09 4:56 
GeneralRe: parent child communication Pin
DaveyM6922-Jan-09 5:09
professionalDaveyM6922-Jan-09 5:09 
GeneralRe: parent child communication Pin
bfis10813722-Jan-09 12:55
bfis10813722-Jan-09 12:55 
GeneralRe: parent child communication Pin
DaveyM6923-Jan-09 1:07
professionalDaveyM6923-Jan-09 1:07 
AnswerRe: parent child communication Pin
Giorgi Dalakishvili21-Jan-09 8:48
mentorGiorgi Dalakishvili21-Jan-09 8:48 
QuestiontoolStrip Quickie Pin
musefan21-Jan-09 6:09
musefan21-Jan-09 6:09 
AnswerRe: toolStrip Quickie Pin
#realJSOP21-Jan-09 8:00
professional#realJSOP21-Jan-09 8:00 
AnswerRe: toolStrip Quickie Pin
musefan21-Jan-09 8:29
musefan21-Jan-09 8:29 
Questionhow to get file present on another web site Pin
sushilbondre21-Jan-09 6:05
sushilbondre21-Jan-09 6:05 
AnswerRe: how to get file present on another web site Pin
musefan21-Jan-09 6:13
musefan21-Jan-09 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.