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?
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.