Author Topic: Visual Basic problem  (Read 4027 times)

0 Members and 1 Guest are viewing this topic.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Visual Basic problem
« on: January 12, 2012, 03:49:29 pm »
i encountered a problem in my code.

i make a treenode with childs, the problem is that i only want to add nodes that are not yet in the treenode.
sometimes the same name for a node appears twice or more, and i want those to only be added once...

this is how i thought it should work, but it doesn't, it works the other way around though: when i delete the "not", no nodes are added to the treenode, which means that the function is correct ???
Code: [Select]
If Not varsnode.Nodes.ContainsKey(linetext.Substring(0, linetext.LastIndexOf("="))) Then
     node = varsnode.Nodes.Add(linetext.Substring(0, linetext.LastIndexOf("=")))
End If

all nodes are initialized, so that's not the problem, varsnode is the main node, and node is the child node
linetext is a string, containing the string i want to add
« Last Edit: January 13, 2012, 08:53:36 am by Nick »

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Visual Basic regex
« Reply #1 on: January 12, 2012, 06:09:26 pm »
Hmm... Well, as far as I know, this isn't regular expressions (regex). ContainsKey only checks the name of the node, not the text. That's more than likely your problem because new TreeNode("text") just sets the text, not the name.
EDIT: Check this MSDN page to see what I mean.

Question, though: is linetext always guaranteed to have an equals sign in it?
« Last Edit: January 12, 2012, 06:10:39 pm by BlakPilar »

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Visual Basic problem
« Reply #2 on: January 13, 2012, 01:24:06 am »
why the hell did i write regex ??? wow xp

thanks, that might be the solution, i'll try it tonight :)

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Visual Basic regex
« Reply #3 on: January 13, 2012, 07:09:53 am »
No problem! Let me know if it works :)

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Visual Basic regex
« Reply #4 on: January 13, 2012, 08:50:04 am »
yeah, i'm sure it contains the "=", and i added a name, and it still does not work xs

Code: [Select]
If Regex.IsMatch(linetext, "=") Then
     If Not varsnode.Nodes.ContainsKey(Regex.Replace(linetext.Substring(0, linetext.LastIndexOf("=")), " ", "")) Then
          node = varsnode.Nodes.Add(Regex.Replace(linetext.Substring(0, linetext.LastIndexOf("=")), " ", ""), Regex.Replace(linetext.Substring(0, linetext.LastIndexOf("=")), " ", ""))
     End If
End If

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Visual Basic problem
« Reply #5 on: January 13, 2012, 04:17:47 pm »
Wait... Why are you making it so complicated? What is your overall goal? To make sure there are no nodes with the same text?

Private Sub DoesNodeExist(ByVal parent As TreeNode, ByVal text As String) As Boolean
    For Each node As TreeNode In parent.Nodes
        If node.Text = text Then Return True
    Next node
    Return False
End Sub

That method will go through every single node in the provided parent node and check to see if a node contains the specified text. That is, if that's what you're trying to do. (Also, I'm a bit rusty on VB, but I'm 99% sure that's all correct.)

Then, to use it, you would do this:

String var = linetext.Substring(0, linetext.LastIndexOf("="))
If Not DoesNodeExist(varsnode, var) Then
    node = New TreeNode(var)   'This is assuming you want to store the recently added treenode
    varsnode.Add(node)
End If
« Last Edit: January 13, 2012, 04:41:54 pm by BlakPilar »

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Visual Basic problem
« Reply #6 on: January 13, 2012, 04:42:29 pm »
i tried it, but i had to make it a function to be able to return true or false..

it doesn't work, it still writes everything to the tree xs

Code: [Select]
If Not DoesNodeExist(varsnode, Regex.Replace(linetext.Substring(0, linetext.LastIndexOf("=")), " ", "")) Then
     node = varsnode.Nodes.Add(Regex.Replace(linetext.Substring(0, linetext.LastIndexOf("=")), " ", ""), Regex.Replace(linetext.Substring(0, linetext.LastIndexOf("=")), " ", ""))
End If

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Visual Basic problem
« Reply #7 on: January 13, 2012, 05:07:16 pm »
Ok, so, this will only work if there is an equals sign in the text line, but here's what I came up with that worked:
Spoiler For code:

Public Class frmMain
    Dim parseNum As Integer = 1

    Private Sub btnParse_Click(sender As System.Object, e As System.EventArgs) Handles btnParse.Click
        Dim varsNode As TreeNode = New TreeNode("Parse #" & parseNum)                   'Setup our variable node
        parseNum = parseNum + 1                                                         'Increment our parse number
        For lineNum As Integer = 0 To txtEdit.Lines.Count - 1                           'For each line
            Dim lineText As String = txtEdit.Lines(lineNum).Trim()                      'Get the text at line lineNum while trimming whitespace characters
            If Not lineText.StartsWith("//") Then                                       'If the line is not a comment
                Dim var As String = lineText.Substring(0, lineText.LastIndexOf("="))    'Get the variable name
                If Not DoesNodeExist(varsNode, var) Then                                'If we have not already added a certain variable
                    varsNode.Nodes.Add(New TreeNode(var))                               'Add a new node with the variable
                End If
            End If
        Next lineNum
        tvwVars.Nodes.Add(varsNode)                                                     'Finally we add our vars node
    End Sub

    Private Function DoesNodeExist(ByVal parent As TreeNode, ByVal text As String) As Boolean
        For Each node As TreeNode In parent.Nodes                                       'For each child node in the provided parent
            If node.Text = text Then
                Return True                                                             'If a node already exists, return true
            End If
        Next node
        Return False                                                                    'If we have not already returned true, return false
    End Function
End Class

tvwVars is my TreeView.
EDIT: This adds "var NAME" to the vars node. I'm sure you can parse the string from there ;)
« Last Edit: January 13, 2012, 05:08:25 pm by BlakPilar »