const, string and other variables - InfoPath Dev

InfoPath Dev

Use our Google Custom Search for best site search results.

const, string and other variables

Last post 02-02-2012 01:51 PM by jaxkookie. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 02-02-2012 11:50 AM

    const, string and other variables

    I have been using a bunch of c#, in order to keep the code clean and neat I have started pulling out my xpaths and declaring as const string. One addition item I am using over and over again is converting this

    string RegionName = MainDataSource.CreateNavigator().SelectSingleNode(xpathRegName, NamespaceManager).Value;

    to this

    XPathNavigator root = MainDataSource.CreateNavigator();
    string RegionName = root.SelectSingleNode(xpathRegName, NamespaceManager).Value;

    the problem is I have to write the XPathNavigator in every function. is there a way to write this once and use it many times?

    XPathNavigator root = MainDataSource.CreateNavigator();

    Kind regards,
    Filed under:
  • 02-02-2012 12:35 PM In reply to

    Re: const, string and other variables

    Well, if you're tired of typing XPathNavigator all the time, you can create an alias for it.  Just put this under the other using statements at the top of the file:

    using XPN = System.Xml.XPath.XPathNavigator;

    Now you can use

    XPN root = MainDataSource.CreateNavigator();

    Also, it's not actually necessary to type MainDataSource here.  All you need is CreateNavigator(), so now you're down to this:

    XPN root = CreateNavigator();


    Also, you can add a function like this:

    XPathNavigator GetNode(XPathNavigator start, string path)
        return start.SelectSingleNode(path, NamespaceManager);

    And thanks to that, you can replace that second line you have there with:

    string RegionName = GetNode(root,  xpathRegName).Value;

    Is that any better?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-02-2012 01:51 PM In reply to

    Re: const, string and other variables

    You are a rock star! I am not worthy.

    thank you

    Kind regards,
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.