0

I'm working on a personal finance application in WPF. I have my MainWindow which obviously handles the rendering. My question is, how do I elegantly access an object in the App class? My structure is set up like this:

class Application { }

class App : Application {
  private AccountManager accountManager;  // HOW DO I ELEGANTLY ACCESS THIS IN MAINWINDOW?  
                                          // USE SINGLETON PATTERN?
}

class MainWindow { }

class AccountManager {
  List<Account> accounts = new List<Account>();
}

static class AccountFactory {
  static Account CreateFactory(string Account);
}

class Account { }
class Asset : Account { }
class Equity : Account { }
class Expense : Account { }
class Income : Account { }
class Liability : Account { }
5
  • 1
    delete all that "factory" stuff. C# is not java. you don't need millions of useless abstractions. Keep it simple. Commented Nov 7, 2014 at 4:55
  • Can you elaborate how the Factory design pattern is strictly Java and not C#? Commented Nov 7, 2014 at 5:02
  • 1
    @keelerjr12 Is the Factory design pattern nesessary? The provided snippet does not show the nesesserity of this pattern here. Commented Nov 7, 2014 at 5:05
  • @keelerjr12 basically, java sucks and is a retarded worthless dinosaur while C# is a properly designed, modern language. Commented Nov 7, 2014 at 5:12
  • That said, simply put the string parameter in the constructor of the account class, if anything. Or use object initializers and leave constructors alone. Commented Nov 7, 2014 at 5:12

1 Answer 1

1

If you want to access a member of one class from another class you make this member public. Keep in mind that public fields are generally bad idea, so use public property:

public AccountManager accountManager { get; private set; }

The setter is private, so the property will be read-only for MainWindow class. Thanks @ HighCore for the readability tip.

Sign up to request clarification or add additional context in comments.

6 Comments

Is the best way to do this? I usually fall into the "getters & setters" are bad design category.
@keelerjr12 Nobody can say 'this is the best way to...'. It's always depends. I believe in this case nothing is wrong with using of property with private setter. The good thing is the simplicity of resulting code.
please. for the love of all that is good, put the getter first. Your code makes me want to kill little kittens. { get; private set; }
@HighCore Just curious what is the difference
@MadSorcerer the difference is that all C# code written in the entire history of humanity has getters first, and so people's brain EXPECT it to be that way, which means that quickly reading your code I interpret { private get; set; } instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.