-1

I'm new to StackOverflow.

I'm working on a C# WinForms project including a self-made DLL. I have several variables in my DLL that are used in my project. However, I would like these variables accessible to my project but readonly. They could only be modified by the DLL internally.

Naturally, I made custom getters/setters on these variables. My DLL correctly generates, my project correctly compiles, but on execution it freezes when trying to access a DLL variable (no error messages).

Here's some of my code:

DLL:

namespace ProductSelection
{
    public class Products{

        // First called by ReadFile
        public static int Count
        {
            get
            {
                return Count;
            }
            set
            {
                Count = value;
            }
        }

        // Called by ReadSystemConfiguration
        internal static bool ReadFile(string File)
        {
            try
            {
                // (..)
                //JSON Array read from file
                JArray array = (JArray)jsondata["products"]; 
                Count = array.Count;
                // (..)
            }
            catch(){}
        }

        // Called by project
        public static bool ReadSystemConfiguration(string File)
        {
            try
            {
                // (..)
                ReadFile(string File);
            }
            catch (){}

        }
    }
}

Project main Form class:

using ProductSelection;

namespace MyProject
{
    public partial class F_Main : Form
    {
        // Variables 
        public static Products F_Products = new Products();

        // Constructor
        public F_Main()
        {

            InitializeComponent();

            // (..)
            Products.ReadSystemConfiguration("File.txt");


        }
    }
}

Without the getter/setter (just public static int Count;)my code runs perfectly, but Count can be modified in my project. I tried to put private set or internal set but nothing worked.

How can I be able to this:

int value = Products.Count;

But not this:

Products.Count = 5;

Thank you very much for your help !

Nicola.

Nicola
  • 91
  • 8

3 Answers3

1

You're in an infinite loop with your Count property accessing itself. Create a backing field.

private static int _count;
public static int Count
{
    get
    {
        return _count;
    }
    set
    {
        _count = value;
    }
}  

If you want it to be readonly, simply remove the setter (your class can still manipulate _count internally).

private static int _count;
public static int Count
{
    get
    {
        return _count;
    }
}  

internal/private set should work just fine as well.

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • This did the trick ! Thank you very much ! However, I have a small subsidary question, this trick works for primitive values, but not for arrays. For example, Products.Count = 5 is not allowed, but for instance if it was an array of int, Products.Count[0] = 5 is allowed. – Nicola Dec 29 '17 at 14:37
  • @Nicola - `Products.Count = 5` is not allowed because then that would be assigning an int to an array, which simply isn't possible. `Products.Count[0] = 5` is allowed because you are assigning to one of the elements, not the array (i.e. `Products.Count = new int[] { ... }` would not be allowed) – Broots Waymb Dec 29 '17 at 14:57
  • How could one not allow `Products.Count[0] = 5` with get/set ? I've researched a bit and found Indexers, but they seem very complicated to use espacially when you have multiple arrays. – Nicola Dec 29 '17 at 15:28
  • @Nicola - Not 100% sure, since I've never actually needed to do that, but this post looks promising: https://stackoverflow.com/questions/2133616/whats-the-best-way-of-creating-a-readonly-array-in-c – Broots Waymb Dec 29 '17 at 16:47
  • It seems to complicated for the benefits it provides me. I'll just keep it this way. However, thank you for your research ! – Nicola Jan 03 '18 at 15:46
0

Your property Products.Count references itself, it is basicly an endless recursive call.

You either have to implement the property normally, or use an auto property.

private static int count;
public static int Count
{
    get
    {
        return count;
    }
    internal set
    {
        count = value;
    }
}

Or:

public static int Count { get; internal set; }
rokkerboci
  • 1,167
  • 1
  • 7
  • 14
0

I tried to put private set or internal set but nothing worked.

Why not? You didn't show us your code. Something like

public static int Count { get; internal set; }

or

public static int Count { get; private set; }

should work.

adjan
  • 13,371
  • 2
  • 31
  • 48
  • No I didn't show the code because it was of little interest. As DangerZone mentioned, my problem was not the accessibility, but the infinite loop :) – Nicola Dec 29 '17 at 14:36