Avatar of pinnanagesh
pinnanagesh
Flag for India asked on

CONSTANTS problem in Internationalization

We are implementing Internationalization for our existing application. Getting all stringf from Resource files and assigning to normal string variables. When we tried to do same thing for CONST variables it's throwing an excpetion.

Our Problem: We could not asscoaite Resource file strings to CONST variables. Please let me know alternatives ASAP.

Error Description:
Error      1              The property or indexer 'WindowsApplication1.Properties.Resources.msgText' cannot be used in this context because it lacks the get accessor              
Error      2              'WindowsApplication1.Form1.var' is of type 'string.' A const of reference type other than string can only be initialized with null          
C#

Avatar of undefined
Last Comment
pinnanagesh

8/22/2022 - Mon
JasonRawlins

Pinnanagesh, try to use readonly variables instead. Readonly variables can be assigned to once and then become constant.
pinnanagesh

ASKER
Thanks Jason for your suggestion but Readonly won't work in our case.

Again we have to use those constants in Case statement and Case is not accepting ReadOnly variables.
JasonRawlins

Here is another possibility. You can store the name of the values as strings. When you retrieve them from the Resource file, you can assign them to read-only variables. The Enum class has some methods to make sure you can cast a string value to the enum value safely.

Then you can use the test variable in a case statement.
private enum SomeEnum
{
	One,
	Two,
	Three
}
 
private void SomeMethod()
{
	Const1 = "One";
	Const2 = "Two";
	Const3 = "Three";
 
	SomeEnum testValue = SomeEnum.One;
 
	if (Enum.IsDefined(typeof(SomeEnum), testValue))
	{
		switch (testValue)
		{
			case SomeEnum.One:
				break;
			case SomeEnum.Two:
				break;
			case SomeEnum.Three:
				break;
		}
	}
	else
	{
		throw new ArgumentOutOfRangeException("testValue is not a member of SomeEnum");
	}
}

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
JasonRawlins

oops, add these declarations before SomeEnum declaration

private readonly string Const1;
private readonly string Const2;
private readonly string Const3;

Open in new window

pinnanagesh

ASKER
I am not sure whether you understood my problem or not but I am not 100% clear with above solution.

My requirement is, I should get some thing thing from Resource file and associate to CONST and use that COSTANTS in case statement.

In above example, I didn't understood where you used constants...tweak my below example and give me the final solutions.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using I18N.Properties;

namespace I18N
{
    public partial class Form1 : Form
    {
        private readonly string Const1 = Resources.Value1;
        private readonly string Const2 = Resources.Value2;
        private readonly string Const3 = Resources.Value3;

        private enum SomeEnum
        {
            One,
            Two,
            Three
        }
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SomeMethod();
        }

        private void SomeMethod()
        {

            SomeEnum testValue = SomeEnum.Two;

            if (Enum.IsDefined(typeof(SomeEnum), testValue))
            {
                switch (testValue)
                {
                    case SomeEnum.One:
                        txt1.Text = "one";
                        break;
                    case SomeEnum.Two:
                        txt1.Text = "Two";
                        break;
                    case SomeEnum.Three:
                        txt1.Text = "Three";
                        break;
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("testValue is not a member of SomeEnum");
            }
        }
    }
}
ASKER CERTIFIED SOLUTION
pinnanagesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.