Access class fields from partial class

人盡茶涼 提交于 2019-12-01 15:33:08

问题


I'm currently in a scenario in which I have to make use of partial classes. In this partial class I have a few methods that need to address fields in the other class.

for example

Edit: I'm sorry: the first class is already declared partial!

public partial class myClass
{        
    private string _myString;

    public string myString
    {
        get { return _myString; }
        set { _myString = value; }
    }
}

and

public partial class myClass
{
    public void doSomething()
    {
        myString = "newString";
    }
}

The compiler says myString doesn't exist in the partial class!

How can I overcome this problem?


回答1:


There are a few things you need to fix with the code you posted:

When using partial classes in C# all parts of the class must be declared as partial classes

You have

 public class myClass {}
 public partial class myClass {}

Which needs to become

public partial class myClass {}
public partial class myClass {}

Secondly, you're trying to set

myString="newString";

but myString is a public property without a setter.

So either you add a setter when declaring myString

public string myString
{
    get{ return _myString; }
    set { _myString = value; }
}

or just use

_myString="newString";

in your second partical class file.




回答2:


A common problem is having the partial classes in different namespaces. Namespaces are part of the Class name, namespace1.myClass and namespace.a.myClass are handled as two completely seperate classes.

According to MSDN, each part of a partial class should:

  • have the partial modifier
  • have the same class name
  • be in the same namespace
  • be in the same assembly or DLL
  • have the same visibility (like public, private, etc)



回答3:


When I put your code into Visual Studio I got a different error:

myString = "newString";
Error   Property or indexer 'ConsoleApplication1.Program.myClass.myString' cannot be assigned to -- it is read only

If I change it to this, it works fine:

_myString = "newString";

Edit:

You don't need to mark a class as partial in both palaces, this compiles fine:

    public class myClass
    {

        private string _myString;

        public string myString
        {
            get { return _myString; }
        }
    }


    public partial class myClass
    {

        public void doSomething()
        {
            _myString = "newString";
        }

    }



回答4:


Assuming that both declaration refer to the same namespace, your first declaration does not contain the partial keyword:

Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace. All the parts must use the partial keyword. All of the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.public, private, and so on.

http://msdn.microsoft.com/en-en/library/wa80x488(v=VS.80).aspx




回答5:


You simply have to mark the class as partial in both places, not just one of them.



来源:https://stackoverflow.com/questions/4132984/access-class-fields-from-partial-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!