How to edit a Partial Class?

1

I took a project from a client and it has a class that is partial. When I click to go in the reference opens the file, but in the project I do not find the file, and has the following text in the class file:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

I've never used partial class , how does it work, and how do I edit?

    
asked by anonymous 30.07.2015 / 22:46

2 answers

4

Definition

Partial class is just one way to separate two parts of the code of a class in distinct files. It has no special secrets or semantics. It is essentially a way to facilitate the organization of code and to indicate to the compiler that the two codes available in the two files with a class of the same name is not a duplication error, but one code is complementary to the other and must be compiled as if it were one thing only.

Use

In general this type of artifice should only be used by code generators, so a part is generated in a file and no one should tinker with this file. In another file have the parts of the class code that the programmer can move without causing problems.

Of course, nothing prevents the programmer from tampering with the generated part, but it should not. It's an organizational protection just to make it clear what parts you can move and parts you can not, but it does not really protect.

If you are going to create some class that needs to be partial, just use the partial modifier before class to indicate that there will be another part. Do this in both files that have both parts. You do not need anything else. But remember that you should only use this if you create a generator and want a part not to be modified.

Partial Method

In the above documentation we talk about partial method which is a method that only has the declaration without a body. In this case the body will be in the other file. It is a way of ensuring that the signature of the method is not changed in the other file. If it is changed, the compiler will report problems.

Do not tamper with generated code

So the file you want to move should not be tampered with.

Where to find the files

The files usually stay together in the project, but in the same folder, in some other related. Try to scan through Windows Explorer for all files available in the project. The file name will probably make some mention of partial or designer or at least have a name similar to the file you want to move and can not find. If you find it in Windows Explorer, it may be easier to find it in VS.

See in VS if the files are not grouped in the project. Click the plus [+] sign to open the related files. Depending on the summer it may be a triangle or other symbol, but that's what opens the tree of files.

But I will strengthen, do not edit this part unless you know a lot what you are doing and know all the implications, which should not be the case.

    
30.07.2015 / 23:23
3

@bigown explained very well - that a partial class is just a class divided into multiple files.

Example:

auto / Persona.cs

namepsace App.Gente {
  public partial class Pessoa {
      public Pessoa() {
      }
  }
}

manual / Persona.cs

namespace App.Gente {
  public partial class Pessoa {
     //não pode ter construtor aqui, pois já está definido no outro arquivo
     //porém pode ter tantas quantas funções/métodos que queira

     public void FazAlgo() {

     }
  }
}

Main.cs

public static class Main {
   Pessoa p = new Pessoa(); //chama o consturtor em auto/Pessoa.cs
   p.FazAlgo(); //chama o método definido em manual/Pessoa.cs
}

Obs: The partial class should be in the same namespace , regardless of physical file location. If it is in another namespace, is another class .

    
31.07.2015 / 05:49