-
[RESOLVED] Var being used - help
Hi,
Can someone throw me a bone, I don't know a fix for this:
Code:
Warning 1 Variable 'progName' is used before it has been assigned a value. A null reference exception could result at runtime. H:\X17-59183\PostInstall\PostInstall\SelectPrograms.vb 29 42 PostInstall
Here's me code:
Code:
Public Class SelectPrograms
Dim Ver As String = " - v1.0.1 Beta 2"
Private Sub SelectPrograms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Text = "Post Install By Chris2k" + Ver
Dim iniFile As String = Application.StartupPath + "\Start\InstallCFG.ini"
Dim sR As New IO.StreamReader(iniFile)
Dim line, progName As String
Dim ReadResult() As String
If Not (System.IO.File.Exists(iniFile)) Then
MessageBox.Show("The programs opted for this OS can't be installed as the InstallCFG.ini has not been included...")
End If
line = sR.ReadLine()
Do While Not (line Is Nothing)
ReadResult = line.Split(":") 'Split line at :
If (ReadResult(0).ToLower() = "prog name") Then
progName = TextBox1.Text + ReadResult(1) + Environment.NewLine
End If
line = sR.ReadLine() 'Move on to the next line.
Dim Item As New ListViewItem(progName)
Item.SubItems.Add("progDesc")
Item.SubItems.Add("Size")
ListView1.Items.Add(Item)
Loop
sR.Close()
End Sub
End Class
-
Re: Var being used - help
its because progName is outside the do while...
just do this
Dim progName As String = ""
or remove the Dim progName from up top and do this
Dim progName as String = TextBox1.Text + ReadResult(1) + Environment.NewLine
-
Re: Var being used - help
I did the 1st solution u said, now all 30 progName's show as blaank/empty in the Textbox1 and listview1......
-
Re: Var being used - help
Er ... how did you manage that? All you had to do was replace ....
Dim line, progName As String
... with ...
Dim line As String = ""
Dim progname As String = ""
Oh, and there doesn't appear to be anything assigned to TextBox1.Text so it's not terribly surprising if it doesn't show anything.
-
Re: Var being used - help
Yep I did that m8, try it n see..
-
Re: Var being used - help
Code:
Public Class SelectPrograms
Dim Ver As String = " - v1.0.1 Beta 2"
Private Sub SelectPrograms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Text = "Post Install By Chris2k" + Ver
Dim iniFile As String = "Start\InstallCFG.ini"
using sR As New IO.StreamReader(iniFile)
Dim progName As String=string.empty
Dim ReadResult() As String
If Not (System.IO.File.Exists(iniFile)) Then
MessageBox.Show("The programs opted for this OS can't be installed as the InstallCFG.ini has not been included...")
End If
Dim line As String = sR.ReadLine()
Do While line IsNot Nothing
ReadResult = line.Split(":") 'Split line at :
If (ReadResult(0).ToLower() = "prog name") Then
progName = TextBox1.Text & ReadResult(1)' & Environment.NewLine
End If
line = sR.ReadLine() 'Move on to the next line.
Dim Item As New ListViewItem(progName)
Item.SubItems.Add("progDesc")
Item.SubItems.Add("Size")
ListView1.Items.Add(Item)
Loop
End using
End Sub
End Class
-
Re: Var being used - help
Just showing as empty/blank in the listview1....
Code:
Public Class SelectPrograms
Dim Ver As String = " - v1.0.1 Beta 2"
Private Sub SelectPrograms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Text = "Post Install By Chris2k" + Ver
Dim iniFile As String = Application.StartupPath + "\Start\InstallCFG.ini"
Dim sR As New IO.StreamReader(iniFile)
Dim line As String = ""
Dim progName As String = ""
Dim ReadResult() As String
If Not (System.IO.File.Exists(iniFile)) Then
MessageBox.Show("The programs opted for this OS can't be installed as the InstallCFG.ini has not been included...")
End If
line = sR.ReadLine()
Do While Not (line Is Nothing)
ReadResult = line.Split(":") 'Split line at :
If (ReadResult(0).ToLower() = "prog name") Then
progName = TextBox1.Text + ReadResult(1)
TextBox1.Text = progName
End If
line = sR.ReadLine() 'Move on to the next line.
Dim Item As New ListViewItem(progName)
Item.SubItems.Add("progDesc")
Item.SubItems.Add("Size")
ListView1.Items.Add(Item)
Loop
sR.Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
End ClassPublic Class SelectPrograms
Dim Ver As String = " - v1.0.1 Beta 2"
Private Sub SelectPrograms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Text = "Post Install By Chris2k" + Ver
Dim iniFile As String = Application.StartupPath + "\Start\InstallCFG.ini"
Dim sR As New IO.StreamReader(iniFile)
Dim line As String = ""
Dim progName As String = ""
Dim ReadResult() As String
If Not (System.IO.File.Exists(iniFile)) Then
MessageBox.Show("The programs opted for this OS can't be installed as the InstallCFG.ini has not been included...")
End If
line = sR.ReadLine()
Do While Not (line Is Nothing)
ReadResult = line.Split(":") 'Split line at :
If (ReadResult(0).ToLower() = "prog name") Then
progName = TextBox1.Text + ReadResult(1)
TextBox1.Text = progName
End If
line = sR.ReadLine() 'Move on to the next line.
Dim Item As New ListViewItem(progName)
Item.SubItems.Add("progDesc")
Item.SubItems.Add("Size")
ListView1.Items.Add(Item)
Loop
sR.Close()
End Sub
End Class
-
Re: Var being used - help
So, let's see a sample CFG.ini file. We can't know what you're attempting to read otherwise.
-
Re: Var being used - help
Sample .ini:
[code]##############################################################################
Force Install:
Install Order:1
Prog Name:Adobe Reader X 11.0.02
Description:
Installer File:Install\Apps\AcroRdrX-11.0.02.exe
##############################################################################
Force Install:
Install Order:2
Prog Name:Adobe Photoshop CS5 Extended Edition
Description:
Installer File:Install\Apps\Adobe_Photoshop_CS5_Extended_Edition_Silent_Chris2k.exe
##############################################################################
[code]
-
Re: Var being used - help
is the INI file in the debug BIN folder? (When you run the app, its going to look in the debug bin folder and not in the projects folder where your SLN is)
Dim iniFile As String = Application.StartupPath + "\Start\InstallCFG.ini"
better to put it in a place like "C:\start\" for now so your app always looks in the same place
-
Re: Var being used - help
Yeh my ini is in "blah\Debug\Start\InstallCFG.ini".. However that's no the issue, TextBox1 gets the info but listview1 isblank...
-
Re: Var being used - help
Right, first problem. Delete Me.Show() Your results are going to another instance of the form and therefore can't be seen!
Second problem, now you can see the results, they're probably not what you want. Have a think about why.
-
Re: Var being used - help
Well that didn't help @dun... still blank lisstview
-
1 Attachment(s)
Re: Var being used - help
Quote:
Originally Posted by
chris-2k
Well that didn't help @dun... still blank lisstview
Your code, with Me.Show deleted. Your sample, post #9, saved to file C:\InstallCFG.ini and file address amended to absolute.
Attachment 99793
Rubbish results but clearly visible ones!
-
Re: Var being used - help
OooooO Thanks guys, now can u help remove dupes
-
Re: Var being used - help
The problem is that you are adding a listviewitem on every line of the file, whether or not it is relevant. You need to test the content of the line and create an item when creating an item is what the content merits.
-
Re: Var being used - help
If I move the listview outside the loop, all the progName's show as 1 item.......
-
Re: Var being used - help
Don't move it out of the loop, move it into the If!
-
Re: Var being used - help
I tried that at 1st, no change...
-
Re: Var being used - help
vb.net Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For Each ln In IO.File.ReadLines("C:\InstallCFG.ini")
If ln.ToLower.StartsWith("prog name") Then
ListView1.Items.Add(ln.Substring(ln.IndexOf(":"c) + 1)) ' a faster and more efficient option than splitting
TextBox1.AppendText(ln.Substring(ln.IndexOf(":"c) + 1) & vbCrLf)
ElseIf ln.ToLower.StartsWith("description") Then
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(ln.Substring(ln.IndexOf(":"c) + 1))
End If
Next
End Sub
End Class
-
Re: Var being used - help
That don't work, it don't like
Code:
For Each ln In IO.File.ReadLines("C:\InstallCFG.ini")
-
Re: Var being used - help
You pasted ion my file address and didn't change it for your own, didn't you? :rolleyes:
-
Re: Var being used - help
Yep m8, working now, tyvm for help.......
Hmm b4 I mark thread, can I copy the if for the description and add to listview as subitem......
-
Re: Var being used - help
Er ... did that already in Line 7.
-
Re: Var being used - help
Sorry I meant file size....
-
Re: Var being used - help
Yup. The only reason I didn't put size in is cos it didn't appear in the file sample. But if it's the same format then just add another copy of the ElseIf with "size" instead of "description".