CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Switch Problem

  1. #1
    Join Date
    Jul 2009
    Posts
    4

    Exclamation Switch Problem

    I have the following code:

    int a = 0;
    switch (A)
    {
    case 0:
    {
    string lang001 = "Welkom";
    string lang002 = "Tot ziens";
    }
    break;

    case 1:
    {
    string lang001 = "Welcome";
    string lang002 = "Goodbye";
    }
    break;
    default:
    {
    string lang001 = "Welcome";
    }
    break;

    It is the intention that I somewhere else in my programming code lang001 place and then depending on whether a 0 or 1 is a specific language is displayed.
    Only when I'm somewhere in the code lang001 drop I get the message that this is never defined

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Switch Problem

    Welcome to the forum
    You should not that C# is a case-sensitive language which means a variable named "a" is not same as "A". In your code you are defining int a = 0 however in the switch statement you are checking for "A". If you replace switch (A) with switch(a), it should work properly.

    You should also remember to use proper names for the variables, that way it will be easier to maintain your code in the long run. Make it a habit now and you will reap number of benefits in your career later.

    Also when you post code here in the forums, you should use code tags.

  3. #3
    Join Date
    Mar 2007
    Posts
    90

    Re: Switch Problem

    Quote Originally Posted by Maarten-tutoworld View Post
    Only when I'm somewhere in the code lang001 drop I get the message that this is never defined
    That is because you define lang001 inside the switch scope, you should move the defenition outside the switch block and set its value inside the switch block:
    Code:
    int a = 0;
    string lang001;
    string lang002;
    switch (a)
    {
        case 0:
        {
    	lang001 = "Welkom";
            lang002 = "Tot ziens";
        }
        break;
    
        case 1:
        {
            lang001 = "Welcome";
            lang002 = "Goodbye";
        }
        break;
        
        default:
        {
            lang001 = "Welcome";
        }
        break;
    }

  4. #4
    Join Date
    Jul 2009
    Posts
    4

    Re: Switch Problem

    thank you for your help, now I have what you see below but still I don't get the switch in the same scope.. can't understand what I am doing wrong.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Switch Problem

    Why don't you use a standard method of translating your application instead of a home-brew method which is less likely to work and a hell of a lot harder to maintain?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Jul 2009
    Posts
    4

    Re: Switch Problem

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Win32;
    
    namespace Gpedit_alternatvie_2._0
    {
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Resize(object sender, EventArgs e)
            {
                if (FormWindowState.Minimized == WindowState)
                {
                    this.Hide();
                }
            }
    
            private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
            {
                this.Show();
                WindowState = FormWindowState.Normal;
            }
    
    
    
            private void button15_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoControlPanel", 0);
                RegKeyWrite.Close();
                MessageBox.Show(lang001,
    "ingeschakeld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button14_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoControlPanel", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol uitgeschakeld",
    "Uitgeschakeld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button16_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoControlPanel", 0);
                RegKeyWrite.DeleteValue("NoControlPanel");
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol hersteld",
    "hersteld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button19_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoAddPrinter", 0);
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol ingeschakeld",
    "ingeschakeld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button18_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoAddPrinter", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol uitgeschakeld",
    "Uitgeschakeld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button17_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoAddPrinter", 0);
                RegKeyWrite.DeleteValue("NoAddPrinter");
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol hersteld",
    "Hersteld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button22_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoDeletePrinter", 0);
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol ingeschakeld",
    "Ingeschakeld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button21_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoDeletePrinter", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol uitgeschakeld",
    "Uitgeschakeld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button20_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoDeletePrinter", 0);
                RegKeyWrite.DeleteValue("NoDeletePrinter");
                RegKeyWrite.Close();
                MessageBox.Show("Sucsesvol hersteld",
    "Hersteld",
    System.Windows.Forms.MessageBoxButtons.OK,
    System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button44_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoRun", 0);
                RegKeyWrite.Close();
                MessageBox.Show("Uitvoeren is ingeschakeld" +
                "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
                "ingeschakeld",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
            }
    
            private void button43_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoRun", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Uitvoeren is uitgeschakeld" +
                "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
                "uitgeschakeld",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
    
            }
    
            private void button42_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoRun", 0);
                RegKeyWrite.DeleteValue("NoRun");
                RegKeyWrite.Close();
                MessageBox.Show("uitvoeren is herseld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "Hersteld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button28_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("LockTaskbar", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Taakbalkvergrendelen is ingeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "ingeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button27_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("LockTaskbar", 0);
                RegKeyWrite.Close();
                MessageBox.Show("Taakbalkvergrendelen is uitgeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "uitgeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button26_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("LockTaskbar", 0);
                RegKeyWrite.DeleteValue("LockTaskbar");
                RegKeyWrite.Close();
                MessageBox.Show("Taakbalkvergrendelen is hersteld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "hersteld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button31_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoSimpleStartMenu", 1);
                RegKeyWrite.Close();
                MessageBox.Show("klassiek startmenu forceren is ingeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "ingeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button30_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoSimpleStartMenu", 0);
                RegKeyWrite.Close();
                MessageBox.Show("klassiek startmenu forceren is uitgeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "uitgeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button29_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                RegKeyWrite.SetValue("NoSimpleStartMenu", 0);
                RegKeyWrite.DeleteValue("NoSimpleStartMenu");
                RegKeyWrite.Close();
                MessageBox.Show("klassiek startmenu forceren is hersteld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "hersteld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.LocalMachine;
                RegKeyWrite = RegKeyWrite.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
                RegKeyWrite.SetValue("ConsentPromptBehaviorAdmin", 2);
                RegKeyWrite.Close();
                MessageBox.Show("UAC voor admins is ingeschakeld Reboot je PC",
    "UAC",
               System.Windows.Forms.MessageBoxButtons.OK,
               System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.LocalMachine;
                RegKeyWrite = RegKeyWrite.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
                RegKeyWrite.SetValue("ConsentPromptBehaviorAdmin", 0);
                RegKeyWrite.Close();
                MessageBox.Show("UAC voor admins is uitgeschakeld Reboot je PC",
    "UAC",
                           System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.LocalMachine;
                RegKeyWrite = RegKeyWrite.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
                RegKeyWrite.SetValue("ConsentPromptBehaviorUser", 1);
                RegKeyWrite.Close();
                MessageBox.Show("UCA voor gebruikers is ingeschakeld Reboot je PC",
                "UCA",
                           System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.LocalMachine;
                RegKeyWrite = RegKeyWrite.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
                RegKeyWrite.SetValue("ConsentPromptBehaviorUser", 0);
                RegKeyWrite.Close();
                MessageBox.Show("UCA voor gebruikers is uitgeschakeld Reboot je PC",
                "UCA",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Information);
            }
    
            private void groupBox4_Enter(object sender, EventArgs e)
            {
    
            }
    
            private void updatesToolStripMenuItem_Click(object sender, EventArgs e)
            {
                update update = new update();
                update.Show();
            }
    
            private void button6_Click_1(object sender, EventArgs e)
            {
    
            }
    
            private void fAQToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void bugsToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button11_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\NonEnum");
                RegKeyWrite.SetValue("{450D8FBA-AD25-11D0-98A8-0800361B1103}", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Het pictogram Mijn documenten verwijderen van bureaublad is ingeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "ingeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button10_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\NonEnum");
                RegKeyWrite.SetValue("{450D8FBA-AD25-11D0-98A8-0800361B1103}", 0);
                RegKeyWrite.DeleteValue("{450D8FBA-AD25-11D0-98A8-0800361B1103}");
                RegKeyWrite.Close();
                MessageBox.Show("Het pictogram Mijn documenten verwijderen van bureaublad is uitgeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "uitgeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\NonEnum");
                RegKeyWrite.SetValue("{645FF040-5081-101B-9F08-00AA002F954E}", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Het Prullenbakpictogram verwijderen van bureaublad is ingeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "ingeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\NonEnum");
                RegKeyWrite.SetValue("{645FF040-5081-101B-9F08-00AA002F954E}", 0);
                RegKeyWrite.DeleteValue("{645FF040-5081-101B-9F08-00AA002F954E}");
                RegKeyWrite.Close();
                MessageBox.Show("Het Prullenbakpictogram verwijderen van bureaublad is uitgeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "uitgeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button23_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\NonEnum");
                RegKeyWrite.SetValue("{20D04FE0-3AEA-1069-A2D8-08002B30309D}", 1);
                RegKeyWrite.Close();
                MessageBox.Show("Het pictogram Deze computer verwijderen van bureaublad is ingeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "ingeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
            private void button13_Click(object sender, EventArgs e)
            {
                RegistryKey RegKeyWrite = Registry.CurrentUser;
                RegKeyWrite = RegKeyWrite.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\NonEnum");
                RegKeyWrite.SetValue("{20D04FE0-3AEA-1069-A2D8-08002B30309D}", 0);
                RegKeyWrite.DeleteValue("{20D04FE0-3AEA-1069-A2D8-08002B30309D}");
                RegKeyWrite.Close();
                MessageBox.Show("Het pictogram Deze computer verwijderen van bureaublad is uitgeschakeld" +
    "\nStart uw PC opniew op of start explorer opnieuw op, Hoe? kijk in de FAQ bij help",
    "uitgeschakeld",
    MessageBoxButtons.OK,
    MessageBoxIcon.Information);
            }
    
    
    
    [...]
    
            public void langselect(object sender, EventArgs e)
            {
                int a = 0;
                string lang001;
                string lang002;
                switch (a)
                {
                    case 0:
                        {
                            lang001 = "Welkom";
                            lang002 = "Tot ziens";
                        }
                        break;
    
                    case 1:
                        {
                            lang001 = "Welcome";
                            lang002 = "Goodbye";
                        }
                        break;
    
                    default:
                        {
                            lang001 = "Welcome";
                        }
                        break;
                }
    
    
    
    
            }
        }
    }
    accidently forgot to actually post my code with the reply, I apologize for that.
    (message was to long, i've cut out some code, where you see [...] there is code.
    Found a solution to show all the code, you can find it here: http://cl1p.net/gpeditalternative/

    Again, thanks in advance

  7. #7
    Join Date
    Jul 2009
    Posts
    4

    Re: Switch Problem

    @Mutant_Fruit

    Well, I did search for ways to translate my application with a somewhat standard method but the methods I found were beyond my knowledge, I didn't understand the code that they used. And it doesn't seem smart to use certain code if I don't even know how it works and if the code fits my needs. If you have a page for me which shows how to use the standard code for localization of my application I would be very glad.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured