5 Replies - 1121 Views - Last Post: 08 July 2012 - 06:46 AM Rate Topic: -----

#1 eddychamoun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 22-November 10

How To Read A Resource File In VB .Net ?

Posted 08 July 2012 - 01:40 AM

hello ,
In my form I have 1 textbox , 1 labels and 1 button + I've added a resource file "sites.txt"
Now in the textbox i should enter the URL of the site and when I click the button , the application should search in the resource file "sites.txt" for the URL of the site , if the site was found then in the label the word "INFECTED" must appear and if not the word "NOT INFECTED" must appear in the label .
My code is :
Label1.Text = ""

        Dim FileContents() As String = IO.File.ReadAllLines("sites.txt")
        
        For I = 0 To FileContents.Length - 1

            Dim LineParts As String = FileContents(I)

            If TextBox1.Text = LineParts Then

                Label1.Text = "Infected"
                
            Else : Label1.Text = "Not Infected"

            End If

        Next


But my problem is when the URL of a site is found in "sites.txt" , the word is always "Not Infected" :hmmm:
Any suggestions would be appreciated :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: How To Read A Resource File In VB .Net ?

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1948
  • View blog
  • Posts: 8,662
  • Joined: 29-May 08

Re: How To Read A Resource File In VB .Net ?

Posted 08 July 2012 - 01:59 AM

Of course it is, the label will be what the last condition in the the for-loop results in choosing.

Accessing Resource in vb.net is simple as;-
My,Resources.
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: How To Read A Resource File In VB .Net ?

Posted 08 July 2012 - 02:06 AM

Here I strongly suggest you to put a breakpoint and debug your code. My guess is that at the end of every LineParts value is a CR_LF(Carriage return and Line feed = Enter). A breakpoint will help you to see the value of that variable at each step.
Also, you may find Contains method more useful
if LineParts.Contains(TextBox1.Text) then 
   'set infected
else 
   'set not infected
end if 



Furthermore, you can shorten your code by using IIf
Label1.Text = IIf(LineParts.Contains(TextBox1.Text), "Infected", "Not infected")


Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1948
  • View blog
  • Posts: 8,662
  • Joined: 29-May 08

Re: How To Read A Resource File In VB .Net ?

Posted 08 July 2012 - 02:10 AM

IFF is a throwback to vb6, and has to preserve vb6 semantics. It has an issue that it evaluates both potential return result before returning one.

Use: If( {Boolean_Condition}, {IsTrue}, {IsFalseResult})
Was This Post Helpful? 1
  • +
  • -

#5 eddychamoun  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 22-November 10

Re: How To Read A Resource File In VB .Net ?

Posted 08 July 2012 - 02:13 AM

View PostAdamSpeight2008, on 08 July 2012 - 01:59 AM, said:

Of course it is, the label will be what the last condition in the the for-loop results in choosing.

Accessing Resource in vb.net is simple as;-
My,Resources.

So in my case what should I do in order to solve my problem - If the site was included in the "sites.txt" the label should be Infected and if the site wasn't included in the "sites.txt" the label should be Not Infected -

View PostIonut, on 08 July 2012 - 02:06 AM, said:

Here I strongly suggest you to put a breakpoint and debug your code. My guess is that at the end of every LineParts value is a CR_LF(Carriage return and Line feed = Enter). A breakpoint will help you to see the value of that variable at each step.
Also, you may find Contains method more useful
if LineParts.Contains(TextBox1.Text) then 
   'set infected
else 
   'set not infected
end if 



Furthermore, you can shorten your code by using IIf
Label1.Text = IIf(LineParts.Contains(TextBox1.Text), "Infected", "Not infected")


thanks Ionut you solved my problem :clap:
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1948
  • View blog
  • Posts: 8,662
  • Joined: 29-May 08

Re: How To Read A Resource File In VB .Net ?

Posted 08 July 2012 - 06:46 AM

Ionut may have provide a solution to your problem, but I reckon you still don't comprehend the reason why your original don't act as you thought.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1