Ishan
Ishan

Reputation: 4028

Set value of a static variable using a function

I have declared some static variables in my solution as below,

using System;
using System.Collections.Generic;
using System.Configuration;

namespace SSPWAS.Utilities
{
    public class Constants
    {
        public static string ApproverlevelL1 = "M23";
        public static string ApproverlevelL2 = "Cre";
        public static string ApproverlevelL3 = "Free34";
        public static string ApproverlevelL4 = "CDF";
        public static string ApproverlevelL5 = "FM";
    }
}

My question is, If i try to set it the like below then i get the error as :

An object reference is required for the non-static field, method, or property

using System;
using System.Collections.Generic;
using System.Configuration;

namespace SSPWAS.Utilities
{
    public class Constants
    {
        public static string ApproverlevelL1 = getLevel("1");
        public static string ApproverlevelL2 = getLevel("2");
        public static string ApproverlevelL3 = getLevel("3");
        public static string ApproverlevelL4 = getLevel("4");
        public static string ApproverlevelL5 = getLevel("5");
    }
}

public string getLevel(string levelID)
{
  string levelName;
  //logic here
  return levelName; 
}

So how can i achieve this?

Upvotes: 0

Views: 3833

Answers (4)

Lee
Lee

Reputation: 3969

Your "constants" are not constant. Make them readonly if you want them to behave like constants while making use of initialising static members with values. You could also make them properties and use just the get accessor to call the getLevel method.

As others have pointed out you cannot call a non-static member from within a static member, without instantiating an instance of the non-static class.

Your getLevel method also needs to be in its own class. As it stands it doesn't belong to a class or namespace. If you want it in its own separate class then just set the method to static.

.NET naming conventions recommend you use Pascal Casing for methods. So rename your getLevel method.

using System;
using System.Collections.Generic;
using System.Configuration;

namespace SSPWAS.Utilities
{
    public static class Constants
    {
        // Use readonly static members
        public static readonly string
            ApproverlevelL1 = GetLevel("1"),
            ApproverlevelL2 = GetLevel("2"),
            ApproverlevelL3 = GetLevel("3"),
            ApproverlevelL4 = GetLevel("4"),
            ApproverlevelL5 = GetLevel("5");

        // Or you could use the latest convenient syntax
        public static string ApproverLevelL6 => GetLevel("6");

        // Or you could use readonly properties
        public static string ApproverLevelL7 { get { return GetLevel("7"); } }

        private static string GetLevel(string levelId)
        {
            //... do logic
            return "";
        }
    }
}

Or if you want the method in its own class:

public static class Constants
{
    // Use readonly static members
    public static readonly string
        ApproverlevelL1 = Level.Get("1"),
        ApproverlevelL2 = Level.Get("2"),
        ApproverlevelL3 = Level.Get("3"),
        ApproverlevelL4 = Level.Get("4"),
        ApproverlevelL5 = Level.Get("5");

    // Or you could use the latest convenient syntax
    public static string ApproverLevelL6 => Level.Get("6");

    // Or you could use readonly properties
    public static string ApproverLevelL7 { get { return Level.Get("7"); } }
}

public class Level
{
    public static string Get(string levelId)
    {
        //... do logic
        return "";
    }
}

Upvotes: 1

Steve Harris
Steve Harris

Reputation: 5119

If your issue is that you want the getLevel method to be in a different class, you could add a Function into your static class and override it with the method.

using System;
using System.Collections.Generic;
using System.Configuration;

namespace SSPWAS.Utilities
{
    public class Constants
    {
        public static Func<string, string> getLevel = x => string.Empty;
        // added get accessor to make these read only
        public static string ApproverlevelL1 { get; } = getLevel("1");
        public static string ApproverlevelL2 { get; } = getLevel("2");
        public static string ApproverlevelL3 { get; } = getLevel("3");
        public static string ApproverlevelL4 { get; } = getLevel("4");
        public static string ApproverlevelL5 { get; } = getLevel("5");
    }

    public class WhateverClass
    {
        public string getLevel(string levelID)
        {
            string levelName;
            //logic here
            return levelName; 
        }

        // call this before accessing the fields in your Constants class
        public void Init()
        {
            Constants.getLevel = x => getLevel(x);
        }
    }
}

The only reason I can think of to do this is maybe you have two applications using this static class and they need to get the level differently. Maybe one uses actual constant values and another reads a database, etc.

If you don't require this then the simplest answer is to actually put the method into the class as a static method:

namespace SSPWAS.Utilities
{
    public class Constants
    {
        public static string getLevel(string levelID)
        {
            string levelName;
            //logic here
            return levelName; 
        }
        // added get accessor to make these read only
        public static string ApproverlevelL1 { get; } = getLevel("1");
        public static string ApproverlevelL2 { get; } = getLevel("2");
        public static string ApproverlevelL3 { get; } = getLevel("3");
        public static string ApproverlevelL4 { get; } = getLevel("4");
        public static string ApproverlevelL5 { get; } = getLevel("5");
    }
}

Upvotes: 0

Leng Weh Seng
Leng Weh Seng

Reputation: 755

Do you mean this:

using System;
using System.Collections.Generic;
using System.Configuration;

namespace SSPWAS.Utilities
{
    public static class Constants
    {
        public static string ApproverlevelL1 = getLevel("1");
        public static string ApproverlevelL2 = getLevel("2");
        public static string ApproverlevelL3 = getLevel("3");
        public static string ApproverlevelL4 = getLevel("4");
        public static string ApproverlevelL5 = getLevel("5");
        private static string getLevel(string levelID)
        {
            string levelName;
            logic here
            return levelName; 
        }
    }
}

Upvotes: 0

openshac
openshac

Reputation: 5165

Looks like you are trying to call an instance method (non-static) from a static property.

Try making your method static:

public string getLevel(string levelID)
{
  string levelName;

  //logic here
  return levelName; 
}

Upvotes: 1

Related Questions