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 8734983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:57:11+00:00 2026-06-13T09:57:11+00:00

This may just be me showing my lack of knowledge / bad programming practice,

  • 0

This may just be me showing my lack of knowledge / bad programming practice, but i’m curious to know if:
a) This already exists
b) If it doesn’t exist, if it’s bad programming practice to do so

But here’s my question:

Suppose I have a class, let’s call it “Computer” and it holds data of all the computers in a company. Now, it just so happens that this company has thousands of Dell computers and thousands of HPs and nothing else. (Again please stick with me here, this is just an example to illustrate my point)

Now, I could define my class as follows:

Public Class Computer

Dim Type as string
Dim SerialNumber as string
Dim User as String
...

End Class

Now, in my code I create two lists:

Dim DellComps as new list(of computer)
Dim HPComps as new list(of computer)

Obviously, for the DellComps, all them will have .Type = "Dell" and for the HPComps, all will have .Type = "HP"

Now, I know I could set this variable in the constructor very easily, but I’m wondering if there is a smarter way to declare the variable inside the class – Similar to the VB Shared / C# Static statement where all the instances of the class share the same variable.

My thoughts are:

  1. Inherit the class and create a shared variable in the child class
  2. Just leave it as is and declare the Type var in the constructor
  3. Maybe this is something that could be done via interfaces somehow
  4. MOST PROBABLE – something i just don’t know about

Thanks and I hope what I’m asking makes sense!!!

  • 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-13T09:57:12+00:00Added an answer on June 13, 2026 at 9:57 am

    If you are always setting a flag in the constructor indicating the type of computer (which is NOT a typical business object scenario, where the type can be edited), chances are that you can really solve your problem using subclasses.

    Subclass Computer to create DellComputer and HpComputer classes.

    When creating lists of each type of computer, one approach is to have a master list of all computers and use Linq’s Enumerable.OfType(TResult) to select instances that match the type you are interested in.

    If indeed you want the type of class to be editable after the class is created, instead provide a property to modify the type of computer. You may for convenience provide a constructor overload that also sets the property (though I would shy away from that personally). If you do, have that constructor overload use the property to set the type.

    UPDATE

    Example of what the factory pattern might look like.

    public abstract class Computer
    {
        public virtual string Type { get; }
    }
    
    public class DellComputer : Computer 
    {
        public override string Type 
        {
            get { return "Dell"; }
        }
    }
    
    public class HpComputer : Computer 
    {
        public override string Type 
        {
            get { return "HP"; }
        }
    }
    
    // Here I'm using an enum to indicate the type desired.  You might use a string
    // or anything else that makes sense in your problem domain.
    
    public enum ComputerType
    {
        Dell = 1,
        Hp = 2
    }
    
    public class ComputerFactory
    {
        public Computer Create(ComputerType type)
        {
            switch (type)
            {
                case ComputerType.Dell:
                    return new DellComputer();
                case ComputerType.Hp:
                    return new HpComputer();
                default:
                    throw new InvalidArgumentException();
            }
        }
    }
    
    // Usage would be something like:
    
    
    List<Computer> computers = new List<Computer>();
    computers.Add(ComputerFactory.Create(ComputerTypes.Dell);
    computers.Add(ComputerFactory.Create(ComputerTypes.Dell);
    computers.Add(ComputerFactory.Create(ComputerTypes.Hp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this may seem like a math question but i just saw this
I fear this just may not be possible, but I'm trying to create a
This may be a stupid question but im just starting to learn Rail thats
This may sound like a stupid question but I'm a beginner not just to
This may be a really dumb question but I'm just starting to learn the
This may be a really silly question, but it just crossed my mind and
I'm just starting to get into jquery/javascript programming so this may be a beginner
This may be a basic question but I need to know how can I
This may be a stupid question but I have a code with the following
This may be have a better name than custom tab completion, but here's the

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.