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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:22:39+00:00 2026-06-12T00:22:39+00:00

I am working on a project where I need to switch for example between

  • 0

I am working on a project where I need to switch for example between device types. I want to define them like in C/C++. I found out that this is not possible in C# the same way. I am making some experimental code to learn how it works, but I am stuck on one thing. I’ve pointed that out in the code below. Please take a look :). I hope the idea of my code is clear, how should I change it to make it work?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        productnames pn = new productnames(); 
        string str = textBox1.Text;
        switch (pn) // I am getting an red line under pn; which says: A switch expression or case label must be a bool, char, string, integral, enum or corresponding nullable type
        { 
            case DEVICE1:
                label1.Text = str+"CASE1";
                break;
            case DEVICE2:
                label1.Text = str+"CASE2";
                break;
        }
    }
}
public class productnames
{
    public const string DEVICE1 = "name1";
    public const string DEVICE2 = "name2";
    public const string DEVICE3 = "name3";
    public const string DEVICE4 = "name4";
}

The code above does not work, because you can not switch between an entire class. A better approach would be something like this:

private const string DEVICE1 = "dev1";
private const string DEVICE2 = "dev2";

        private void button1_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            getCommand(str)
        }

private void getCommand(string Dev)
{
            switch (Dev) 
            { 
                case DEVICE1:
                    label1.Text = str+"CASE1";
                    break;
                case DEVICE2:
                    label1.Text = str+"CASE2";
                    break;
            }
    }

This example could be useful:

#define CMD_VERSION 0
#define CMD_TYPE 1

private void getCommandName(CMD)
{
   switch(CMD)
   {
      case CMD_VERSION:
      return ("ver");
      case CMD_TYPE:
      return ("serienummer");
   }
}
  • 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-12T00:22:41+00:00Added an answer on June 12, 2026 at 12:22 am

    Let’s be creative and add an example that would be used more quickly in real life.

    using System;
    
    namespace Draft
    {
        class Program
        {
            static void Main()
            {
                const string str = "Device2";
                var strParsed = (Devices)Enum.Parse(typeof(Devices), str);
    
                switch (strParsed) {
                    case Devices.Device1:
                        Console.WriteLine("Device 1");
                        break;
                    case Devices.Device2:
                        Console.WriteLine("Device 2");
                        break;
                    case Devices.Device3:
                        Console.WriteLine("Device 3");
                        break;
                    case Devices.Device4:
                        Console.WriteLine("Device 4");
                        break;
                }
    
                Console.ReadKey();
            }
    
            public enum Devices {
                Device1,
                Device2,
                Device3,
                Device4
            }
        }
    }
    

    [Edit]

    Seems i was going in the right direction, only your “define” example give them integer’s (which they have anyway on default in the order you have declared them in).

    But to follow your example it would be something like:

    public enum Devices {
        Device1 = 0,
        Device2 = 1,
        Device3 = 2,
        Device4 = 3
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How Guys I'm working on a project that need display same thing like this,
I'm working in a project that need to get SVD (Single Values Decomposition) for
I'm working on a project where I need to create a link that when
I'm working on a project that I need to eliminate all references to the
I'm working on a project at the moment where I need to pick out
I'm working in a project that need to recognize the physical keys' positions, Control,
I'm working on project that need do call the same method several times, but
I'm working on project that need to control sending queue by code. So I
I want to overload the assignment operator for types like int, long etc. That
I was working on a project and I need to read a row in

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.