static/const/readonly variable...

I must be missing something simple...

I want to have a small struct... I want 3 of the 5 variables to be constant for all instances and the other two to be variable.

However, the 3 that are constant come from an xml file.. which must be read in...not an actual string in code...

so I've tried things like

struct samplestruct
{
public string constVar1 = xmlval1; //won't compile because you can't instantiate in struct

so i tried

struct samplestruct
{
public const string constVar1 = xmlval1; //won't compile because expression must be constant

so i tried

struct samplestruct
{
public static string constVar1 = xmlval1; //won't compile because because object reference is required

so i tried

struct samplestruct
{
public readonly string constVar1 = xmlval1; //won'tt compile because you can't instantiate in struct

any ideas....




Answer this question

static/const/readonly variable...

  • Plumpton

    Structs can not have instance initializers (field per 'instance'), however, they can have static initializers (field for all 'instances'):



        public struct Foo
        {
            private static readonly string Value = CalculateValue();
     
            public static string CalculateValue()
            {
                // Calculate value;
            }
        }

     



  • fanis

    "public string constVar1 = xmlval1;" etc...

    Are you trying to assign some variable to the field You must do that kind of actions in the constructor, you know...

  • Jean-Marie

    Hi This code is a sample that may help to solve your problem

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Configuration;

     

    namespace staticinitializers

    {

    public struct fooo

    {

    public static readonly int x;

    public static readonly int y;

    static fooo()

    {

    try

    {

    x = int.Parse(ConfigurationSettings.AppSettings["First"]);

    y = int.Parse(ConfigurationSettings.AppSettings["Second"]);

    }

    catch (Exception e)

    {

    Console.WriteLine(e.Message);

    }

    }

    }

    class Program

    {

    static void Main(string[] args)

    {

    Console.WriteLine (fooo.x.ToString()+ " "+ fooo.y.ToString());

    Console.ReadLine();

    }

    }

    }

     

    Application Configuration file that i am refereing[XML file]

     

    < xml version="1.0" encoding="utf-8" >

    <configuration>

    <appSettings>

    <add key ="First" value ="20" />

    <add key ="Second" value ="30"/>

    </appSettings>

    </configuration>

     

    Regards



  • GAJMAN

    I would use a class instead, create read-only property procedures for properties that need to be initialized, and initialize them in the class constructor.

    public class SampleClass

    {

        private string _var1;

        public string Property1

        {

            get { return _var1; }

            //set property procedure is not implemented, so this value

            //can be set only from this class and not from outside

        }

     

        private string _var2;

        public string Property2

        {

            get { return _var2; }

            set { _var2 = value; }

        }

     

        public SampleClass()

        {

            //here you can read from your file, for demo initializing it to "Hi"

            _var1 = "Hi ";

     

            //Property2 can be changed from outside if needed. Intializing it to "There"

            _var2 = "There";

        }

     

    }

    In this code, Property1 is read-only and its value cannot be changed, outside of SampleClass.


  • static/const/readonly variable...