Reputation: 24562
I have this code in one file:
namespace Japanese
{
public partial class PhrasesPage : ContentPage
{
PhrasesFrame phrasesFrame = new PhrasesFrame();
this code in another file:
namespace Japanese.Views.Phrases
{
public partial class PhrasesPage
{
public void setTimeInterval()
{
phrasesFrame // can't access
I would like to be able to access phrasesFrame in the second file but it seems not possible. Can anyone give me suggestions on what I could do so I could access this in the other file which is also a partial class. One more question. Should I inherit from ContentPage in all of the files with my partial class or is just one file enough?
Upvotes: 1
Views: 2547
Reputation: 34189
You can easily access all members of partial classes inside it, even private.
public partial class Test
{
private int x = 5;
}
public partial class Test
{
public void Run()
{
Console.WriteLine(x); // outputs 5
}
}
However, you use it incorrectly. Since your classes are in two different namespaces, compiler treats them as two different classes:
Japanese.PhrasesPage
Japanese.Views.Phrases.PhrasesPage
If you want to make it work like a partial class, you need to place them into a single namespace:
// 1
namespace Japanese.Views.Phrases
{
public partial class PhrasesPage : ContentPage
{
PhrasesFrame phrasesFrame = new PhrasesFrame();
// 2
namespace Japanese.Views.Phrases
{
public partial class PhrasesPage
{
public void setTimeInterval()
{
phrasesFrame // CAN access
However, make some analysis on these classes' responsbilities.
Since you placed them into different namespaces and inherit from different classes, then may be they have different meaning and need to be separate classes by design?
You can always make classes relate to each other with composition or aggregation instead of making it partial. Partial classes are not very convenient and are usually used for code generation scenarios.
For me, setTimeInterval
sounds like it has nothing to do with "phrases" and "pages".
Upvotes: 4
Reputation: 2603
Those are treated as two different classes because of the namespaces being different. You should use a common namespace for both of them as follows:
namespace Japanese
{
public partial class PhrasesPage : ContentPage
{
PhrasesFrame phrasesFrame = new PhrasesFrame();
//...
}
}
namespace Japanese
{
public partial class PhrasesPage
{
public void setTimeInterval()
{
phrasesFrame // accessible! :)
}
}
}
Upvotes: 3