Lookup / search value from a file

dotdotdot

Member
Joined
Oct 22, 2008
Messages
7
Programming Experience
Beginner
Dear all,

I’m very new to VB and without much experience in programming. Any advice or direction would be helpful to me.

I’m writing a programme that, when I get a certain integer value from a message, I have to return a string of characters. Sounds simple if there is only a handful of integers, I could get away with few IF statements. BUT, such is life, there is a very large number of possible values.

My guess is that, if I have a separate file to store the corresponding characters, I could change the characters easily, and the programme itself could be smaller.

My questions are:
*What would be the best type of file to store the ‘lookup table’?
*How could I access the file quickly and search for corresponding characters?
*How do I read just one line of the file and not more?

I would greatly appreciate if anyone could give me any advice or example links.

Thanks in advance
Thomson
 
I suggest looking at the database section of the forums on how to use parameterized queries. This way you can keep 1 centralized lookup table for multiple clients.

-----

Another option would be to use an XML file. Here's a sample layout and function to pull the string value using an XPath expression.

VB.NET:
<?xml version="1.0" encoding="utf-8" ?>
<TopLevel>
  <Conversion>
    <IntegerCode>1</IntegerCode>
    <ActualString>1st value</ActualString>
  </Conversion>
  <Conversion>
    <IntegerCode>2</IntegerCode>
    <ActualString>2nd value</ActualString>
  </Conversion>
</TopLevel>

VB.NET:
Private Function StringLookup(ByVal MyInteger As Integer) As String

    Dim xdoc As New XPathDocument("Components\IntLookup.xml")
    Dim nav As XPathNavigator = xdoc.CreateNavigator()
    Dim iterator As XPathNodeIterator = _
        nav.Select(String.Format("//IntegerCode[.'{0}']/parent::node()/ActualString", MyInteger))
    iterator.MoveNext()

    Return iterator.Current.Value
End Function
 
Matt,

Thanks for the code, but one more followup question. How could I have multiple sections on the xml?

Because the messages I receive comes in few types, they may contain the same integer, but would raise a different response message. If I want to keep all the response messages in one XML file, how could I alter the code to do so?

Cheers,
T
 
Matt,

Thanks for the code, but one more followup question. How could I have multiple sections on the xml?

Because the messages I receive comes in few types, they may contain the same integer, but would raise a different response message. If I want to keep all the response messages in one XML file, how could I alter the code to do so?

Cheers,
T

So your're going to receive 1 and could have multiple responses you could give back? I would seriously consider revising your program while it's still in the planning phase. If it's out of your hands I'd just add another element to your conversion XML.

VB.NET:
  ....
  <Conversion>
    <IntegerCode>1</IntegerCode>
    <SourceA>return value</SourceA>
    <SourceB>another value</SourceB>
  </Conversion>
  ....

From here it's easy to modify the XPath expression in the StringLookup method.

VB.NET:
Private Function StringLookup(ByVal MyInteger As Integer, ByVal ElementName As String) As String

    Dim xdoc As New XPathDocument("Components\IntLookup.xml")
    Dim nav As XPathNavigator = xdoc.CreateNavigator()
    Dim iterator As XPathNodeIterator = _
        nav.Select(String.Format("//IntegerCode[.'{0}']/parent::node()/{1}", MyInteger, ElementName))
    iterator.MoveNext()

    Return iterator.Current.Value
End Function
 
Back
Top