Resolved Just Upraded to .Net 5, my static int Count variable is no longer being initialised

SilverShaded

Well-known member
Joined
Mar 7, 2020
Messages
93
Programming Experience
10+
So when i try and use it in the constructor it gives an error (its null), this didn't happen in .Net 4 so im wondering if its a Core feature or .Net 5, any ideas?
 
Last edited:
I think you need to show us your code. An int can never be null.
 
Last edited:
Hi heres the code;

The error occurs at Count++, it works perfectly in .Net 4

C#:
    class DrawBaseStream : DrawLine, IDrawStream, ISerializable
    {
        internal Guid startDrawObjectID, endDrawObjectID;
        internal DrawObject startObject, endObject;

        internal Guid startNodeGuid, endNodeGuid;
        internal Node startNode, endNode;
        static private int Count = 0;
    }
   
    public DrawBaseStream(Node hsstart, Node hsend) : base()
     {
         StreamColor = Color.Blue;
         Count++;
         Name = Count.ToString();
      }
 
Can you also post the exact exception along with stack trace?
 
Hi heres the code;

The error occurs at Count++, it works perfectly in .Net 4

C#:
    class DrawBaseStream : DrawLine, IDrawStream, ISerializable
    {
        internal Guid startDrawObjectID, endDrawObjectID;
        internal DrawObject startObject, endObject;

        internal Guid startNodeGuid, endNodeGuid;
        internal Node startNode, endNode;
        static private int Count = 0;
    }
  
    public DrawBaseStream(Node hsstart, Node hsend) : base()
     {
         StreamColor = Color.Blue;
         Count++;
         Name = Count.ToString();
      }
Your constructor appears to be outside the class it constructs. Is that just the way you posted the code?
 
I just tested this code in Console apps targeting .NET Framework 4.8 and .NET 5.0:
C#:
class Program
{
    static void Main()
    {
        var t1 = new Thing();
        var t2 = new Thing();

        Console.WriteLine(t1.Name);
        Console.WriteLine(t2.Name);

        Console.ReadLine();
    }
}

class Thing
{
    private static int Count = 0;
    public readonly string Name;

    public Thing()
    {
        Count++;
        Name = Count.ToString();
    }
}
In both cases, it worked exactly the same way and exactly as you'd expect. I suspect that there's something else going on in your project and you just haven't debugged properly. As suggested, an int can't be null so, at the very least, your description is inaccurate.
 
Hi heres the code;

The error occurs at Count++, it works perfectly in .Net 4

C#:
    class DrawBaseStream : DrawLine, IDrawStream, ISerializable
    {
        internal Guid startDrawObjectID, endDrawObjectID;
        internal DrawObject startObject, endObject;

        internal Guid startNodeGuid, endNodeGuid;
        internal Node startNode, endNode;
        static private int Count = 0;
    }

    public DrawBaseStream(Node hsstart, Node hsend) : base()
     {
         StreamColor = Color.Blue;
         Count++;
         Name = Count.ToString();
      }

I just tested this code in Console apps targeting .NET Framework 4.8 and .NET 5.0:
C#:
class Program
{
    static void Main()
    {
        var t1 = new Thing();
        var t2 = new Thing();

        Console.WriteLine(t1.Name);
        Console.WriteLine(t2.Name);

        Console.ReadLine();
    }
}

class Thing
{
    private static int Count = 0;
    public readonly string Name;

    public Thing()
    {
        Count++;
        Name = Count.ToString();
    }
}
In both cases, it worked exactly the same way and exactly as you'd expect. I suspect that there's something else going on in your project and you just haven't debugged properly. As suggested, an int can't be null so, at the very least, your description is inaccurate.
Yes you are of course correct. When stepping through the code last night that was the line which seemed to throw the error, look at it this morning and realised its actually a line where its trying to get a cursor.

this line below was failing not entirely sure why as it was ok previously

C#:
private static Cursor handleCursor = new Cursor(typeof(DrawPolygon), "PolyHandle.cur");

replaced it with the next line after adding the cursor to resources and all is working again.

C#:
private static Cursor handleCursor = new(new System.IO.MemoryStream(Refbits.Properties.Resources.PolyHandle));
 
Back
Top Bottom