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:
partial
modifierThere 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With