Documentation

    Table of Content
    • CodePorting.Translator Product Family
      • CodePorting.Translator Cs2Cpp
        • Getting Started
          • Product Overview
          • Installation
          • Licensing
          • How to use CodePorting.Translator Cs2Cpp
            • How to Use Command line to translate and build Projects
            • How to Use GUI to translate and build Projects
        • Developer Guide
          • Translating Simple C# Projects
            • Translating Simple Console Application
            • Translating Simple Library
            • Translating Simple NUnit Test
          • Translating Dependent C# Projects
            • Translating Dependent Console Application
            • Translating Dependent Library
            • Translating Dependent NUnit Test
          • Qt support
          • Translating Complex C# Projects
            • How to Convert Complex C# Console Application to C++
            • How to Convert Complex C# Library to C++
            • How to Convert Complex C# NUnit Test to C++
          • What Converts to What
            • AbstractClasses
            • BreakStatements
            • ClassAutoProperties
            • ClassConstructors
            • ClassEvents
            • ClassFinalizers
            • ClassGenericMethods
            • ClassIndexers
            • ClassMethods
            • ClassProperties
            • ClassStaticConstructor
            • ClassStaticMethods
            • ClassStaticProperties
            • ClassVirtualMethods
            • ContinueStatements
            • Delegates
            • DoWhileStatements
            • Enums
            • EnumTypeCast
            • Exceptions
            • ExpectedException
            • ForeachStatements
            • ForeachStatementsStd
            • ForStatements
            • GenericClasses
            • GenericDelegates
            • GenericInterfaces
            • GenericStructs
            • IfStatements
            • LambdaExpressions
            • NestedClasses
            • ReturnStatements
            • SimpleClass
            • SimpleInterface
            • SimpleStruct
            • SimpleTest
            • StandardTypeCast
            • StaticClass
            • SwitchStatements
            • TestWithSetupMethods
            • ThrowStatements
            • TryCatchFinallyStatements
            • TryCatchStatements
            • TryFinallyStatements
            • VarExpressions
            • WhileStatements
          • CodePorting Translator Cs2Cpp Attributes
          • CodePorting Translator Cs2Cpp Configuration File
            • CodePorting.Translator Cs2Cpp Configuration File Structure
            • Attributes in Configuration file
            • Configuration file Nodes
            • Configuration file Options
          • Memory Management Model
            • Memory Management Model Description
            • Using aliasing constructor to create a smart pointer
          • Cmake Support
          • C++ code injection
          • C++ user-defined exception classes
          • Limitations and Bugs
            • Translator Limitations and Bugs
            • Library Limitations and Bugs
            • Cpp Code Injection
        • Release Notes
          • 2022
            • CodePorting.Translator Cs2Cpp 22.6
            • CodePorting.Native Cs2Cpp 22.5
            • CodePorting.Native Cs2Cpp 22.4
            • CodePorting.Native Cs2Cpp 22.3
            • CodePorting.Native Cs2Cpp 22.2
            • CodePorting.Native Cs2Cpp 22.1
          • 2021
            • CodePorting.Native Cs2Cpp 21.12
            • CodePorting.Native Cs2Cpp 21.11
            • CodePorting.Native Cs2Cpp 21.10.1
            • CodePorting.Native Cs2Cpp 21.10
            • CodePorting.Native Cs2Cpp 21.9
            • CodePorting.Native Cs2Cpp 21.8
            • CodePorting.Native Cs2Cpp 21.7
            • CodePorting.Native Cs2Cpp 21.6
            • CodePorting.Native Cs2Cpp 21.5
            • CodePorting.Native Cs2Cpp 21.4
            • CodePorting.Native Cs2Cpp 21.3
            • CodePorting.Native Cs2Cpp 21.2
            • CodePorting.Native Cs2Cpp 21.1
          • 2020
            • CodePorting.Native Cs2Cpp 20.12
            • CodePorting.Native Cs2Cpp 20.11
            • CodePorting.Native Cs2Cpp 20.10
            • CodePorting.Native Cs2Cpp 20.9
            • CodePorting.Native Cs2Cpp 20.8
            • CodePorting.Native Cs2Cpp 20.7
            • CodePorting.Native Cs2Cpp 20.6
            • CodePorting.Native Cs2Cpp 20.5
            • CodePorting.Native Cs2Cpp 20.4
            • CodePorting.Native Cs2Cpp 20.3
            • CodePorting.Native Cs2Cpp 20.2
            • CodePorting.Native Cs2Cpp 20.1
          • 2019
            • CodePorting.Native Cs2Cpp 19.1
            • CodePorting.Native Cs2Cpp 19.2
            • CodePorting.Native Cs2Cpp 19.3
            • CodePorting.Native Cs2Cpp 19.4
            • CodePorting.Native Cs2Cpp 19.4.1
            • CodePorting.Native Cs2Cpp 19.5
            • CodePorting.Native Cs2Cpp 19.6
            • CodePorting.Native Cs2Cpp 19.7
            • CodePorting.Native Cs2Cpp 19.8
            • CodePorting.Native Cs2Cpp 19.9
            • CodePorting.Native Cs2Cpp 19.10
            • CodePorting.Native Cs2Cpp 19.11
            • CodePorting.Native Cs2Cpp 19.12
          • 2018
            • CodePorting.Native Cs2Cpp 18.9
            • CodePorting.Native Cs2Cpp 18.9.1
            • CodePorting.Native Cs2Cpp 18.10
            • CodePorting.Native Cs2Cpp 18.11
            • CodePorting.Native Cs2Cpp 18.12
    1. Home
    2. CodePorting.Translator Product Family
    3. CodePorting.Translator Cs2Cpp
    4. Developer Guide
    5. What Converts to What
    6. SwitchStatements

    SwitchStatements

    What's on this Page

      • Source C# Code
      • Translated Code
        • C++ Header
        • C++ Source Code

    This example demonstrates how switch statement is translated to C++. Switch statement with stings is translated to C++ if statement.

    Additional command-line options passed to CodePorting.Translator.Cs2Cpp: none.

    Source C# Code

    using System;
    using System.Collections.Generic;
    
    namespace StatementsPorting
    {
        public enum SomeEnum
        {
            InitValue = 0,
            Value = 1,
            SomeValue =2,
            OtherValue = 3,
            YetOneValue = 4,
            AnotherValue = 5
        }
    
        public class SwitchStatements
        {
            public void IntSwitch(int value)
            {
                switch (value)
                {
                    case 3:
                        Console.WriteLine("3 branch");
                        break;
                    case 4:
                    case 5:
                        Console.WriteLine("4-5 branch");
                        break;
                    case 6:
                        Console.WriteLine("6 branch with return");
                        return;
                    case 7:
                        Console.WriteLine("7 branch");
                        break;
                    default:
                        Console.WriteLine("default branch");
                        break;
                }
            }
    
            public void EnumSwitch(SomeEnum value)
            {
                switch (value)
                {
                    case SomeEnum.InitValue:
                        Console.WriteLine("SomeEnum.InitValue branch");
                        break;
                    case SomeEnum.Value:
                    case SomeEnum.SomeValue:
                        Console.WriteLine("SomeEnum.Value-SomeEnum.SomeValue branch");
                        break;
                    case SomeEnum.OtherValue:
                        Console.WriteLine("SomeEnum.OtherValue branch with return");
                        return;
                    case SomeEnum.YetOneValue:
                        Console.WriteLine("SomeEnum.YetOneValue branch");
                        break;
                    default:
                        Console.WriteLine("default branch");
                        break;
                }
            }
    
            public void StringSwitch(string value)
            {
                switch (value)
                {
                    default:
                        Console.WriteLine("default branch");
                        break;
                    case "iddqd":
                        Console.WriteLine("iddqd branch");
                        break;
                    case "idkfa":
                    case "idkfa2":
                        Console.WriteLine("idkfa-idkfa2 branch");
                        break;
                    case "idclip":
                        Console.WriteLine("idclip branch with return");
                        return;
                    case "quicken":
                        Console.WriteLine("quicken branch");
                        break;
    
                }
            }
    
            public void StringSwitchAdvanced()
            {
                string value = "";
                switch (value + "d")
                {
                    default:
                        Console.WriteLine("default branch");
                        break;
                    case "iddqd":
                        Console.WriteLine("iddqd branch");
                        break;
                    case "idkfa":
                    case "idkfa2":
                        Console.WriteLine("idkfa-idkfa2 branch");
                        break;
                    case "idclip":
                        Console.WriteLine("idclip branch with return");
                        return;
                    case "quicken":
                        {
                            Console.WriteLine("quicken branch");
                            break;
                        }
                    case "choice":
                        while (true)
                        {
                            if (value == "iddqd")
                            {
                                break;
                            }
                            break;
                        }
                        if (value == "iddqd")
                        {
                            break;
                        }
                        break;
                    case "choice2":
                        Console.WriteLine("choice2 branch");
                        if (value == "iddqd")
                        {
                            break;
                            if (value == "iddqd")
                            {
                                break;
                            }
                        }
                        break;
                    case "choice3":
                        if (value == "aaa")
                            break;
    
                        Console.WriteLine("choice3 branch");
                        break;
                }
            }
    
            public string Property1 { get { return "circle"; } }
            
            public string Field1 = "a";
    
            public SwitchStatements NewInstance() { return new SwitchStatements(); }
    
            public void StringSwitchOnProperties()
            {
                switch (Property1)
                {
                    case "abc":
                        break;
                    case "def":
                    case "a, b, c":
                        break;
                    default:
                        break;
                }
            }
            
            public void StringSwitchOnFields()
            {
                switch (Field1)
                {
                    case "a":
                        break;
                    case "b":
                    case "c":
                        break;
                    default:
                        break;
                }
    
                switch (NewInstance().Field1)
                {
                    case "a":
                        break;
                    case "b":
                    case "c":
                        break;
                    default:
                        break;
                }
            }
        }
    }

    Translated Code

    C++ Header

    #pragma once
    
    #include <system/string.h>
    #include <cstdint>
    
    namespace StatementsPorting {
    
    enum class SomeEnum
    {
        InitValue = 0,
        Value = 1,
        SomeValue = 2,
        OtherValue = 3,
        YetOneValue = 4,
        AnotherValue = 5
    };
    
    class SwitchStatements : public System::Object
    {
        typedef SwitchStatements ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_DECL();
        
    public:
    
        System::String get_Property1();
        
        System::String Field1;
        
        void IntSwitch(int32_t value);
        void EnumSwitch(SomeEnum value);
        void StringSwitch(System::String value);
        void StringSwitchAdvanced();
        System::SharedPtr<SwitchStatements> NewInstance();
        void StringSwitchOnProperties();
        void StringSwitchOnFields();
        
        SwitchStatements();
        
    };
    
    } // namespace StatementsPorting
    

    C++ Source Code

    #include "SwitchStatements.h"
    
    #include <system/console.h>
    
    namespace StatementsPorting {
    
    RTTI_INFO_IMPL_HASH(3987979893u, ::StatementsPorting::SwitchStatements, ThisTypeBaseTypesInfo);
    
    System::String SwitchStatements::get_Property1()
    {
        return u"circle";
    }
    
    void SwitchStatements::IntSwitch(int32_t value)
    {
        switch (value)
        {
            case 3:
                System::Console::WriteLine(u"3 branch");
                break;
            
            case 4:
            case 5:
                System::Console::WriteLine(u"4-5 branch");
                break;
            
            case 6:
                System::Console::WriteLine(u"6 branch with return");
                return;
            
            case 7:
                System::Console::WriteLine(u"7 branch");
                break;
            
            default: 
                System::Console::WriteLine(u"default branch");
                break;
            
        }
    }
    
    void SwitchStatements::EnumSwitch(SomeEnum value)
    {
        switch (value)
        {
            case StatementsPorting::SomeEnum::InitValue:
                System::Console::WriteLine(u"SomeEnum.InitValue branch");
                break;
            
            case StatementsPorting::SomeEnum::Value:
            case StatementsPorting::SomeEnum::SomeValue:
                System::Console::WriteLine(u"SomeEnum.Value-SomeEnum.SomeValue branch");
                break;
            
            case StatementsPorting::SomeEnum::OtherValue:
                System::Console::WriteLine(u"SomeEnum.OtherValue branch with return");
                return;
            
            case StatementsPorting::SomeEnum::YetOneValue:
                System::Console::WriteLine(u"SomeEnum.YetOneValue branch");
                break;
            
            default: 
                System::Console::WriteLine(u"default branch");
                break;
            
        }
    }
    
    void SwitchStatements::StringSwitch(System::String value)
    {
        if (value == u"iddqd")
        {
            System::Console::WriteLine(u"iddqd branch");
        }
        else if (value == u"idkfa" || value == u"idkfa2")
        {
            System::Console::WriteLine(u"idkfa-idkfa2 branch");
        }
        else if (value == u"idclip")
        {
            System::Console::WriteLine(u"idclip branch with return");
            return;
        }
        else if (value == u"quicken")
        {
            System::Console::WriteLine(u"quicken branch");
        }
        else 
        {
            System::Console::WriteLine(u"default branch");
        }
    }
    
    void SwitchStatements::StringSwitchAdvanced()
    {
        System::String value = u"";
        const System::String& switch_value_0 = value + u"d";
        if (switch_value_0 == u"iddqd")
        {
            System::Console::WriteLine(u"iddqd branch");
        }
        else if (switch_value_0 == u"idkfa" || switch_value_0 == u"idkfa2")
        {
            System::Console::WriteLine(u"idkfa-idkfa2 branch");
        }
        else if (switch_value_0 == u"idclip")
        {
            System::Console::WriteLine(u"idclip branch with return");
            return;
        }
        else if (switch_value_0 == u"quicken")
        {
            System::Console::WriteLine(u"quicken branch");
        }
        else if (switch_value_0 == u"choice")
        {
            while (true)
            {
                if (value == u"iddqd")
                {
                    break;
                }
                break;
            }
            if (value == u"iddqd")
            {
                
            }
        }
        else if (switch_value_0 == u"choice2")
        {
            System::Console::WriteLine(u"choice2 branch");
            if (value == u"iddqd")
            {
                goto switch_break_0;
                if (value == u"iddqd")
                {
                    goto switch_break_0;
                }
            }
        }
        else if (switch_value_0 == u"choice3")
        {
            if (value == u"aaa")
            {
                goto switch_break_0;
            }
            System::Console::WriteLine(u"choice3 branch");
        }
        else 
        {
            System::Console::WriteLine(u"default branch");
        }
        switch_break_0:;
    }
    
    System::SharedPtr<SwitchStatements> SwitchStatements::NewInstance()
    {
        return System::MakeObject<SwitchStatements>();
    }
    
    void SwitchStatements::StringSwitchOnProperties()
    {
        const System::String& switch_value_1 = get_Property1();
        if (switch_value_1 == u"abc")
        {
        }
        else if (switch_value_1 == u"def" || switch_value_1 == u"a, b, c")
        {
        }
        else 
        {
        }
    }
    
    void SwitchStatements::StringSwitchOnFields()
    {
        if (Field1 == u"a")
        {
        }
        else if (Field1 == u"b" || Field1 == u"c")
        {
        }
        else 
        {
        }
        
        const System::String& switch_value_2 = NewInstance()->Field1;
        if (switch_value_2 == u"a")
        {
        }
        else if (switch_value_2 == u"b" || switch_value_2 == u"c")
        {
        }
        else 
        {
        }
    }
    
    SwitchStatements::SwitchStatements() : Field1(u"a")
    {
    }
    
    } // namespace StatementsPorting