Partial Class extension of Winforms application doesn't work. Intellisense says nothing is in context, solution explorer sees it as part of the class

McKeon, Brian 1 Reputation point
2021-02-24T22:02:05.263+00:00

![71751-image.png][1] ![71726-image.png][2] [1]: /api/attachments/71751-image.png?platform=QnA [2]: /api/attachments/71726-image.png?platform=QnA

So not sure if this is a bug, or just something I am doing wrong. I have used partials in the past with other code to split things off that I strip off in production, things like test functions. But never tried with a windows form application. All I would like to do is split the logic into several source files, but it seems visual studio will not allow this, and not sure why. The solution explorer finds this partial as being in the Slice class, but intellisense says everything is not in the current context. It doesn't matter how the properties are set, they are already set to compile. Intellisense cannot see these items as being in scope even though the solution explorer seems to see them fine. Is this supposed to even be possible? I just want to clean up some code between various tabs of my application form to aid in maintaining things. Visual Studio 2017 15.9.33 .NET 4.8.03752 building against .NET 4.7.2 targets.
Smith.ProdReads.cs

using System;   
using System.Data;   
using System.Windows.Forms;   
using System.Data.SqlClient;  
using System.Collections.Generic;   
namespace Interrogator  
 {   
public partial class Smith   
{   
AMRFilterString=   
}   
}  

is not able to reference anything in Smith class, doesn't matter if Form inheritance is set or not. Solution Explorer happily sees them as one happy family.

Smith.cs excerpt

namespace Interrogator {   
public partial class Smith : Form   
{   
string AMRFilterString;   
//Establish binding source for gridview   
private BindingSource AMRbSource;   
private SqlDataAdapter ReadData;   
private DataTable AMRTable;   
public Smith() {}   
}  
 }  

Smith.cs has been trimmed, I am not going to provide the full application logic, it is irrelevant and private. But its not clear to me why one part of Visual Studio sees things in scope while another part does not. For now I'll just keep the madness in one source file, but sure would be convenient if this worked. Only think I can think of is tagging everything static, but that seems wrong considering they should alll be seen as part of the same class. Visual Studio seems to treat this as a new form, even though I don't want a new form, just a new source file.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,888 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,141 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,797 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-02-25T08:29:58.71+00:00

    Hi McKeonBrian-2613,
    Base on your code, the AMRFilterString is a field not property.
    You need to use Smith class instance to access the field.
    The Properties can be read-write (they have both a get and a set accessor).
    Code as below:

    public partial class Smith : Form  
     {  
            private string amrFilterString; // field  
            public string AMRFilterString   // property  
            {  
                get { return AmrFilterString; }  
                set { amrFilterString = value; }  
            }  
     }  
    

    In other form.cs(Smith.ProdReads.cs), you can use the AMRFilterString property to access and update the private field of the Smith class

     Smith S = new Smith();  
     S.AMRFilterString = "HELLO";  
    

    If I misunderstand what you mean, please explain in detail.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.