Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:39:48+00:00 2026-06-11T14:39:48+00:00

I am trying to implement a factory. As I understand it, the following simple

  • 0

I am trying to implement a factory. As I understand it, the following simple code is a factory (C#)

private void methodName()
{
    var result = GetTypeByName("nameOfType")
}

private AbstractClassName GetTypeByName(string nameOfType)
{
    switch(nameOfType)
    {
       case "type1":
           return new inheritedFromAbstractClass();
       case "type2":
           return new alsoInheritedFromAbstractClass();
       default:
           throw new Exception();
    }
}

I am sadly stuck on a .NET 2.0 project with VS2005 and as such can’t use the ‘var’ keyword. Can any one give me any advice on how something like this was done or how it can be done in .NET 2.0.

So, a more real life example:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Starting order");
        Console.WriteLine("Press 1 or 2");
        string s = Console.ReadLine();
        var order = GetOrderByIdFromFactory(s);
        order.ShippingAddress = "My House, England";
        Console.WriteLine(order.Ship());
        Console.ReadKey();
    }

    private static OrderShipment GetOrderByIdFromFactory(string s)
    {
        OrderShipment os = default(OrderShipment);
        switch (s)
        {
            case "1":
                return new FedExShipping();
                break;
            case"2":
                return  new UpsShipping();
                break;
            default:
                throw new NotImplementedException("Not implemented");
        }
    }
}

My Abstract base class

public abstract class OrderShipment
{

    #region Properties
    public string ShippingAddress { get; set; }
    internal string Label { get; set; }
    private readonly TextWriter _text = new StringWriter();
    #endregion

    #region Public Methods

    internal string Ship ()
    {
        VerifyShippingData();
        GetShippingLabelFromCarrier();
        PrintLabel();
        return _text.ToString();
    }

    internal virtual void VerifyShippingData()
    {
        if (string.IsNullOrEmpty(ShippingAddress))
        {
            throw new ApplicationException("Invalid Address");
        }
    }

    internal abstract void GetShippingLabelFromCarrier();

    internal virtual void PrintLabel()
    {
        _text.Write(Label);
    }

    #endregion
}

And my 2 inheriting classes

public class FedExShipping : OrderShipment
{
    internal override void GetShippingLabelFromCarrier()
    {
        //ToDo perform logic
        Label = "FedEx Label Details";
    }
}

public class UpsShipping : OrderShipment
{
    internal override void GetShippingLabelFromCarrier()
    {
        //ToDo logic
        Label = "Ups Label Details";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:39:49+00:00Added an answer on June 11, 2026 at 2:39 pm

    Why not somethign like this:

    AbstractClassName result = GetTypeByName("nameOfType")
    

    var keyword is not an innovation, it’s just let’s you do not type AbstractClassName (in this concrete example). So within this concrete question scenario you don’t loose much using 2.0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a cache using Zend Cache. I use the following code
I am trying to implement a factory for two classes Circle, Square both of
I am trying to implement a logging factory and I have used an interface
I am trying to implement a factory class that generates objects and intercept all
I'm currently trying to implement a factory as a singleton. I practically used the
I am trying to implement prepared staments in my code as a way of
Following this question , I am trying to implement an async method using the
I am trying to implement factory pattern by registering the function pointers of the
I'm trying to implement what I believe is a factory class. I have an
I'm trying to implement the sample code to this article from 2002 (I know..),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.