简体   繁体   中英

Access class fields from partial class

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?

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)

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 partial class file.

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";
        }

    }

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

您只需要在两个地方将类标记为partial ,而不仅仅是其中一个。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM