Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Everyone,

I've a small problem with partial class

Suppose that a class A is already defined as follow in some namespace n as -

C#
namespace n
{
  public class A
  {
    private int i;
    ///Other methods and variables goes here
  }
}



Now, I need to add more methods to the class A and also to access the variable i.

so, i did something like this -

C#
namespace n
{
  public partial class A
  {
     public void SomeMethod()
     {
        this.i = i++;
     }
     ///Other methods and variables goes here
  }
}



But it is not working in ASP.NET 4.0.

Can anyone has solution,
Thanx in advance :)
-Satya182

[Modified: code tags are meant for inserting snippets within paragraphs like if I wanted to say to use a bool variable. Pre tags are meant for bigger chunks of code]
Posted
Updated 22-Dec-10 12:42pm
v2
Comments
AspDotNetDev 22-Dec-10 18:43pm    
What do you mean by "it isn't working"? And remember that both partial classes must exist in the same project... so you can't, for example, create a partial class to manipulate an exist .Net Framework class.

from MSDN: "All the parts must use the partial keyword" (Partial Class Definitions (C# Programming Guide)[^])

The first class has to have the partial keyword or the compiler won't know that it should look for other parts of the class before closing it.

If you don't have the ability to change the first part, then you need to have a new class inherit from the first class. Then you can add as much as you want and still have access to the original parts.
 
Share this answer
 
v2
Comments
AspDotNetDev 22-Dec-10 19:16pm    
And that is why error messages are useful (why don't people post the error messages they get?). Take a 5.
namespace n
{
public partial class A
{
private int i;
///Other methods and variables goes here
}
}
 
Share this answer
 
Comments
Matt T Heffron 23-Oct-15 19:43pm    
Why are you answering a FIVE YEAR OLD question?
Your answer doesn't even really address the actual issue.
CHill60 23-Oct-15 20:34pm    
Meaningless

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900