I’m very new to C# programming and I’m struggling a bit with OOP.
I have created a class with a method that analyzes a text file and creates an object based on its content. I want this function to return an object that contains some of the information in the text file only if the text file is in the correct format.
I call the function to analyze the text file like this:
pokerHand newHand;
newHand = new pokerHand();
AnalyzePokerHand.importHand("c:\\text.txt");
newHand = AnalyzePokerHand.getAnalyzedHand;
If text.txt isn’t a poker hand history file, or is in the wrong format etc., I don’t want newHand to be filled with garbage information about the hand.
What is the correct approach if I would like to pass a path to a text file in and return an object with information about the hand only when the function was successful?
Thanks for having a look!
Returning null is the best way if you want to indicate that no data is available.
The other advantage of this approach is it will throw null pointer exception if you try to access the member variable.