0

It was taught to me that any alterations to an object on a form (change the text, make visible/invisible or change the color and so on) should be done in the corresponding form class.

But due to the large amount of such alterations done inside the project, the file has became large and hard to search through. I've read online that a Partial Class could help, but there was no explanation on how to implement this. As an easy example, I have the following 2 files: Form_Main.cs

namespace Test
{
    partial class Form_Main : Form
    {
        public Form_Main()
        {
             InitializeComponent();    
        }
    }
}

AND Form_Main.Dataloader.cs

 namespace Test
    {
        partial class Form_Main : DataLoader
        {
            public void SetText()
            {
                TextBox_StudentSurname.Text = "1";        
            }
        }
    }

How can I make this work? Because if I do this I get several errors in the designer. enter image description here

2
  • 1
    Read the first line of that list of errors. -- You cannot have this: Form_Main : DataLoader. See Partial Classes and Methods (C# Programming Guide) Commented Feb 27, 2022 at 18:51
  • Watch out though; the forms designer cannot always handle well things that you put in other files than YourFormName.cs Commented Feb 27, 2022 at 18:57

1 Answer 1

1

Your main problem is the first error printed: You cannot declare different base classes in different partial implementations. I don't know which of the two base classes is the right one (the one you previously used), but as always, a class cannot have two base classes. It is legal to specify the base class only in one of the parts, but if it is specified multiple times, it must be the same.

Sign up to request clarification or add additional context in comments.

5 Comments

So how can i correct this? The original file is Form_Main.cs, the other one i added. What do i need to change?
From the second file, remove : DataLoader in the class header. Partial classes is really just a syntactic way of splitting up a class, it is semantically still the very same class.
Thanks, this worked, while searching every "awnser" told me to mess around with the designer.cs which i have had some really bad experience with.
@A.bakker Yea, you shouldn't do that, unless you know what you're doing. The designer file is actually implemented as a part of the same class as well.
Messing with the designer is fine, as long as you have a backup. i f you haven't got into it already, source control is a good idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.