Home
Options

Setting Static field in one Query file and having another Query file access the same value.

I'm fairly new to LINQPad. I've got some runtime settings in static fields of various classes that are common to many scripts I need to create. To avoid replicating these values in each file, I was trying to move those settings (which won't change often) into a standalone file and then run that file from other .linq files as their first line. No matter what I try, the values set in the settings file are not retained when accessing that same static field in the calling script. I've tried many combinations of caching, compiling, running etc, as well as toggling different LINQPad settings like Always Use Fresh Process Per Execution or Run Each Query In it's Own Process etc but always the same result. I'm stuck using LINQPad version 5 with .NET Framework for the time being, so I'm not able to use the new #load feature, but that's the way used to do this kind of thing in the Visual Studio Interactive window - so it sounds like I might need version 6. Can someone confirm or let me know if there is a recipe I can follow to achieve the same thing in version 5?

Thanks

Comments

  • Options

    Have you tried adding a class to the 'My Extensions' query? That class will then be available to all queries.

    void Main()
    {
    } 
    
    public static class MyExtensions
    {
    }
    
    public static class Settings
    {
        public static int Foo = 123;
    }
    

    If you want settings that persist permanently (written to disk), use Util.LoadString / Util.SaveString:

    public static class Settings
    {
        public static string PersistedValue
        {
            get => Util.LoadString ("PersistedValue");
            set => Util.SaveString ("PersistedValue", value);
        }
    }
    
  • Options

    Thanks @JoeAlbahari , that works great. I hadn't explored the extensions much yet and didn't realize you could use it for that.

Sign In or Register to comment.