Something broke my C# application

Maylar

Member
Joined
Jan 22, 2021
Messages
16
Programming Experience
10+
Sorry about the cryptic subject line, but I have a bizzare situation...

Upgraded to the latest version of VS 2019 this morning and something stopped working -

I'm passing some argunments from one form to another, by making them public on the recieving form. I've done this many times before, and prior to the upgrade this was working.
Code is very similar to this:


Passing code variables:
public partial class Form2 : Form
    {
        public int varA;
        public int varB;
        public string txt;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            int varC = varA;
        }
    }
    
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.varA = 2;
            frm.varB = 3;
            frm.txt = "Hello world";

            frm.ShowDialog();

        }
    }

The variables in my application are instantiated class objects and int. In the receiving form (Form_2 here} the int variables show as zeros, and the class objects show as nulls.
It used to work, now it doesn't.

Is there a project file that might have become corrupted?

TIA
Dave
 
I doubt that this has anything to do with your issue but those public fields should be properties. The standard is to pretty much ALWAYS use properties to expose data publicly.

As for the issue, there's certainly nothing in the code that would indicate why that's happening. I would try adding another form and do something similar and see if the same thing happens, both in that project and another. That will at least give you an indication of how widespread the problem is.
 
I doubt that this has anything to do with your issue but those public fields should be properties. The standard is to pretty much ALWAYS use properties to expose data publicly.
-----------------------------------

I've never done that, and I'm willing to give it a go - but I can't find a tutorial for doing that, and what I've found via Google doesn't seem to work. "Use Class View and right click on the interface and add-> new property". I'm not sure what they mean by interface, and anything I right click on only gives Add -> new EditorConfig. Where do I get the add properties from?

-----------------------------------
As for the issue, there's certainly nothing in the code that would indicate why that's happening. I would try adding another form and do something similar and see if the same thing happens, both in that project and another. That will at least give you an indication of how widespread the problem is.
-----------------------------------

Tried that, good idea. The problem seems to be limited to one form. My method works perfectly with a new form. Hmm...

Dave
 
Just write the code for the property, just as you would for a field or a method. Generally speaking, a property is almost the same as a field:
C#:
Public Property SomeProperty As SomeType
If you need to add some additional functionality, e.g. validation or raising a change event, you can write out the full getter and setter:
C#:
Private _someProperty As SomeType

Public Property SomeProperty As SomeType
    Get
        Return _someProperty
    End Get
    Set
        _someProperty = value
    End Set
End Property
When you write the first code, the system automatically expands it to the second behind the scenes.
 
If there's not much to your form then you can just create a new one to replace it. If it's complex and you don't want to discard it, there's something that can fix corrupt forms sometimes. You can try dragging the form to a different project in the same solution, then see if the form works as expected in that new project. If it does, delete the original and drag it back again. You may need to fiddle with the second project a bit to get it to compile, but that may be less work than recreating the form.
 
Back
Top Bottom