0

So I wrote this question yesterday. I am still using the solution under UPDATE, but for some reason I am now receiving a FormatException was Unhandled error. Under the error, the compiler window says that the Input string was not in a correct format. Why would this be happening?

When I was looking into the error I thought that maybe I'd have better luck using Int32.TryParse, like in this link. But it was pretty much the same deal.

This is what I currently have...

//Initializing a parent TreeView Item
TreeViewItem parentItem = (TreeViewItem)SelectedItem.Parent;

//This is the call to getNumber that I am having trouble with.
//It is located in an if statement, but I didn't bother to write out the
//whole statement because I didn't want to add surplus code
int curNumber = getNumber(parentItem.Header.ToString());

//Gets the number contained in a Node's header
public static int getNumber(string parentNodeHeader)
{
      int curNumber = 0;
      curNumber = Convert.ToInt32(parentNodeHeader); //**FormatException!!
      return curNumber;
}

Note: The nodes that I click on to make this error appear do not have number values in them. However, their parents do (Which is what I don't understand because I am passing the parent's header to the function).

Thanks for your help!

Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

1 Answers1

0

Well Int32.TryParse should not raise an exception...

//Gets the number contained in a Node's header
public static int getNumber(string parentNodeHeader)
{
      int curNumber;
      //if parse to Int32 fails, curNumber will still be 0
      Int32.TryParse(parentNodeHeader, out curNumber);
      return curNumber;
}

EDIT :

Seems that you should do something like that (somme null check would be better, of course)

//Initializing a parent TreeView Item
var parentItem = (TreeViewItem)SelectedItem.Parent;
var header = (TextBlock)parentItem.Header;
int curNumber = getNumber(header.Text);
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Alright, that took care of the exception. But `Int32.TryParse` is failing because `curNumber` is returning 0. Is there anything I could show you that would help you to explain why? – Eric after dark Aug 22 '13 at 14:54
  • @Ericafterdark well, you should try to debug slowly, see what's in parentItem, then in parentItem.Header... – Raphaël Althaus Aug 22 '13 at 14:55
  • Well my parent node's header is set to a `TextBlock`, is that a problem? Inside the `if` statement I get the correct value for `parentItem`, but then when it passes to `getNumber`, `parentNodeHeader` displays `"System.Windows.Controls.TextBlock"` as it's Value. – Eric after dark Aug 22 '13 at 15:01
  • @Ericafterdark I edited my answer, can you test the EDIT part ? – Raphaël Althaus Aug 22 '13 at 15:06
  • I get an `InvalidCastException` on `var header = ...` – Eric after dark Aug 22 '13 at 15:10
  • I ended up adding the `var header = ...` line to the `if` statement and now it is working as expected. Thanks for your help! – Eric after dark Aug 22 '13 at 15:16