vb.net How to reference a local variable - vb.net

Hi I have the below code:
Dim ColMap As Integer = Val(Microsoft.VisualBasic.Left(dr("MappedTo").ToString(), 3))
Dim ValorLeido As String
ValorLeido = temp(dr("NoColumn").ToString())
Select Case ColMap
Case 101
_101 = ValorLeido
Case 102
_102 = ValorLeido
Case 103
_103 = ValorLeido
End Select
Is there a way that I can use something like me("_" & ColMap) = ValorLeido ??

Here's a simplified example showing CallByName(). Note the the target variables are Public:
Public Class Form1
Public _101 As String
Public _102 As String = "{Default Value}"
Public _103 As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Debug.Print("Before: _102 = " & _102)
Dim ColMap As Integer = 102
Dim ValorLeido As String = "Hello World!"
Dim varName As String = "_" & ColMap
CallByName(Me, varName, CallType.Let, ValorLeido)
Debug.Print("After: _102 = " & _102)
End Sub
End Class
And here's the same thing via Reflection, which allows the target variables to be Private:
Imports System.Reflection
Public Class Form1
Private _101 As String
Private _102 As String = "{Default Value}"
Private _103 As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Debug.Print("Before: _102 = " & _102)
Dim ColMap As Integer = 102
Dim ValorLeido As String = "Hello World!"
Dim varName As String = "_" & ColMap
Dim fi As FieldInfo = Me.GetType.GetField(varName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
fi.SetValue(Me, ValorLeido)
Debug.Print("After: _102 = " & _102)
End Sub
End Class

Try using a storage medium meant to hold a series of ID's to a series of values such as a Dictionary
Dim ColMap As Integer = Val(Microsoft.VisualBasic.Left(dr("MappedTo").ToString(), 3))
Dim ValorLeido As String = temp(dr("NoColumn").ToString())
Dim Lookup as New Generic.Dictionary(of Integer,String)
Lookup(ColMap) = ValorLeido
' 1 way of Reading values out of a dictionary safely
Dim Value as string
Value=""
if lookup.trygetvalue("101",value) then
msgbox("Value for 101 is """ & value & """")
else
msgbox("Value for 101 is not found)
end if
You could also access via a single indexed property if this structure already exists
public property Value(Index as integer) as string
get
select case index
case 101
return _101
case 102
return _102
case 103
return _103
case else
Throw new exception("Index not present " & index)
end get
set (value as string)
' populate with reverse process ...
end set
end property

Related

Display variable content dynamically

I'm trying to do this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim my_variable as String = "hello"
Dim blabla as string = "my_variable"
msgbox(blabla) ' I want this to display: "hello"
End Sub
Any way you guys can help me in VB.NET (not C# please).
What you want cannot be done for a LOCAL variable like my_variable.
If that variable is at CLASS level, though, it can be done with REFLECTION.
If the variable is at class level and is PUBLIC, you can cheat and use CallByName:
Public Class Form1
Public counter As Integer = 911
Public my_variable As String = "Hello World!"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim variable As String = TextBox1.Text
Try
Dim value As String = CallByName(Me, variable, CallType.Get)
MessageBox.Show(variable & " = " & value)
Catch ex As Exception
MessageBox.Show("Variable not found: " & variable)
End Try
End Sub
End Class
Type the name of the variable in TextBox1 and its value will be displayed in the message box....easy peasy.
If you don't want the variables to be public, then it can be accomplished via Reflection, but it doesn't look quite as simple and pretty. Look it up if you want to go that route.
--- EDIT ---
Here's a version that can find public members of a module:
Code:
Imports System.Reflection
Public Class Form2
Public counter As Integer = 911
Public my_variable As String = "Hello World!"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lblResults.Text = ""
Dim variable As String = TextBox1.Text
Try
Dim value As String = CallByName(Me, variable, CallType.Get)
lblResults.Text = variable & " = " & value
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lblResults.Text = ""
Dim moduleName As String = txtModule.Text
Dim moduleField As String = txtModuleMember.Text
Dim myType As Type = Nothing
Dim myModule As [Module] = Nothing
For Each x In Assembly.GetExecutingAssembly().GetModules
For Each y In x.GetTypes
If y.Name.ToUpper = moduleName.ToUpper Then
myType = y
Exit For
End If
Next
If Not IsNothing(myType) Then
Exit For
End If
Next
If Not IsNothing(myType) Then
Dim flags As BindingFlags = BindingFlags.IgnoreCase Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static Or BindingFlags.Instance
Dim fi As FieldInfo = myType.GetField(moduleField, flags)
If Not IsNothing(fi) Then
Dim value As String = fi.GetValue(Nothing)
lblResults.Text = moduleField & " = " & value
End If
End If
End Sub
End Class
Public Module PutThings
Public SomeValue As String = "...success!..."
End Module
My suggestion (just 1 idea, thinking out loud) would be to create a separate, global list of all of your variables, and every single time one of the variables you want to know the contents of changes, update the global list.
For example:
' Global declaration
Dim dictionary As New Dictionary(Of String, String)
Sub Form_Load()
' Add keys to all vars you want to lookup later
With dictionary
.Add("varCarrot", String.Empty)
.Add("varPerl", String.Empty)
End With
End Sub
Sub SomeSub()
strCarrot = "hello"
intPerl = 12
' Any time the vars change that you want to track, update the respective dictionary key
dictionary(varCarrot) = strCarrot
dictionary(varPerl) = intPerl.ToString
Do Until intPerl = 100
intPerl += 1
strCarrot = "hello " & intPerl
dictionary(varCarrot) = strCarrot
dictionary(varPerl) = intPerl.ToString
Loop
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
dim strLookup as String = text1.text ' the variable you want to lookup entered in the text1 textbox; i.e. "varCarrot"
' See if the requested key exists
If dictionary.ContainsKey(strLookup) Then messagebox.show(dictionary.Item(strLookup))
End Sub
When you're ready to no longer have that functionality in your app, such as when all done debugging it, and ready to finally release it, comment out all the dictionary stuff.

How to display random ques from text file in vb?

I have created a Quiz application in Visual Basic.I have stored the questions in a text file and I'm using streamreader to read the lines.The text file looks like this
If x is the first of five consecutive odd numbers then what is their average ?
x
x+1
x+4
x+3
3
Which of the following number is divisible by 24 ?
76300
78132
80424
81234
3
The first line is the question,the lines 2 to 5 is the options and the 6th line is the answer key and there are more than 100 questions and I should print random questions and its corresponding choices each time I open the application and it should not repeat the same question.Can any one give me a code snippet for this?
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Quiz
Public ques As Integer = 1
Dim Shuffle As Integer = 0
Dim SCORE As Integer = 0
Dim val As Integer = 30
Public anskey As String
Private currentQuestion As Integer
Private listOfQuestions As List(Of Question) = New List(Of Question)
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function
Public Sub Reset_all()
val = 30
SCORE = 0
ProgressBar1.Value = 0
Button3.Hide()
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 30
Timer1.Enabled = True
Using reader = New System.IO.StreamReader("Quiz.txt")
Dim line = reader.ReadLine()
While (Not String.IsNullOrWhiteSpace(line))
Dim question = New Question
question.Question = line
question.Choice1 = reader.ReadLine()
question.Choice2 = reader.ReadLine()
question.Choice3 = reader.ReadLine()
question.Choice4 = reader.ReadLine()
question.Answer = reader.ReadLine()
listOfQuestions.Add(question)
line = reader.ReadLine()
End While
End Using
If listOfQuestions.Count > 0 Then
LoadQuestion(0)
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Reset_all()
End Sub
Sub LoadQuestion(questionIndex As Integer)
Dim question = listOfQuestions(questionIndex)
currentQuestion = questionIndex
If listOfQuestions.Count - 1 = currentQuestion Then
End If
With question
Label3.Text = ques
Label1.Text = .Question
RadioButton1.Text = .Choice1
RadioButton2.Text = .Choice2
RadioButton3.Text = .Choice3
RadioButton4.Text = .Choice4
anskey = .Answer
End With
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (SCORE > 0) Then
SCORE -= 1
End If
If (currentQuestion > 0) Then
If (ques > 0) Then
ques -= 1
LoadQuestion(currentQuestion - 1)
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If (anskey = "a" And RadioButton1.Checked = True Or anskey = "b" And RadioButton2.Checked = True Or anskey = "c" And RadioButton3.Checked = True Or anskey = "d" And RadioButton4.Checked = True) Then
SCORE += 1
End If
If (currentQuestion < listOfQuestions.Count - 1) Then
If (ques <= 99) Then
ques += 1
LoadQuestion(currentQuestion + 1)
End If
End If
End Sub
Private Sub Quiz_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dashboard.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
val -= 1
Label2.Text = val & " Sec"
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Enabled = False
End If
If ProgressBar1.Value > 23 Then
SendMessage(ProgressBar1.Handle, 1040, 2, 0)
Button3.Show()
End If
If ProgressBar1.Value = 30 Then
End If
End Sub
Private Sub SubmitResult()
MsgBox("You have Scored " + SCORE.ToString + " Out of 100")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim Re As Integer = MsgBox("Are you sure you want to submit?",
vbYesNo, "Submit")
If (Re = 6) Then
SubmitResult()
Try
Me.Close()
Dashboard.Show()
Catch ex As Exception
End Try
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
End Class
Public Class Question
Public Property Question As String
Public Property Choice1 As String
Public Property Choice2 As String
Public Property Choice3 As String
Public Property Choice4 As String
Public Property Answer As String
End Class
I would make a class "question" like this:
public Class Question
public questionas String
public answer1 as String
public answer2 as String
public answer3 as String
public answer4 as String
public correctAnswer as integer
public sub new(que as string, a1 as string, a2 as string, a3 as string, a4 as string, answer as integer)
question= que
answer1=a1
answer2=a2
answer3=a3
answer4=a4
correctAnswer=answer
end sub
end Class
Now load all your questions in the programm like this:
Imports System
Imports System.IO
Class MainWindow
private listQuestions as List(Of Question)
Public Sub window_loaded() handles MainWindow.loaded
listQuestions = loadAllQuestions()
End Sub
private function loadAllQuestions() as List(Of Question)
Dim str() As String
Try
' Open the file using a stream reader.
Using sr As New StreamReader("example.txt")
Dim line As String
' Read the stream to a string and write the string to the console.
line = sr.ReadToEnd()
Str = line.Split(vbNewLine)
End Using
Catch e As Exception
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
End Try
'So now put the questions in your list:
dim list as new List(Of Question)
For i = 0 to str.count - 1
if (i+1) mod 5 = 0 then 'is divible without rest by 6
list.add(new Question(str(i-5), str(i-4), str(i-3), str(i-2), str(i-1), str(i))
end if
next
return list
end sub
'Load a random question:
private sub btNext_click() handles btNext.click()
dim ranQuestion as Question
dim r as new random
ranQuestion = listFragen.item(r.next(0,listQuestions.count))
End Class
Hope i could help you. To prevent that the programm can show the same question again its your job :)
First, I would recommend using the File.ReadAllLines() function to get an array of lines in the text file with questions. Then you can access them easily.
If you observe, the index of the line of the question will be (n - 1) * 6, where n is the question number. Once you get that, the indices of the options are given by:
i + 1
i + 2
i + 3
i + 4
where i = (n - 1) * 6. The answer key is given by:
i + 5
That should get you started. If you get stuck, leave a comment :)
So you would do the first part by:
Dim lines() As String = File.ReadAllLines("<yourQuestions.txt")
Then, you can generate a random number within the required range using:
Dim questionNumber As Integer = Random.Next(1, (lines.Length / 6) + 1)
After that, you can retrieve the question, options and answer key by:
Dim i As Integer = (questionNumber - 1) * 6
Dim question As String = lines(i)
Dim options() As String = {lines(i + 1), lines(i + 2), lines(i + 3), lines(i + 4)}
Dim answerKey As String = lines(Integer.Parse(lines(i + 5)))
You can also consider creating a class Question:
Public Class Question
Public Property Question As String
Public Property Choice1 As String
Public Property Choice2 As String
Public Property Choice3 As String
Public Property Choice4 As String
Public Property Answer As String
Public Sub New(q As String, c1 As String, c2 As String, c3 As String, c4 As String, ans As String)
Question = q
Choice1 = c1
Choice2 = c2
Choice3 = c3
Choice4 = c4
Answer = ans
End Sub
End Class
Then you can assign the properties.
Another alternative (might be better in performance, in fact, I think it should be) would be to use the File.ReadLines() function and use the Take() and Skip() extension methods of IEnumerable<T> (LINQ):
Dim questionNumber As Integer = Random.Next(1, (File.ReadLines("<yourQuestions.txt").Count() / 6) + 1)
Dim blockLines = File.ReadLines("<yourQuestions.txt").Skip((questionNumber - 1) * 6).Take(6)
Dim currentQuestion As New Question(blockLines(0), blockLines(1), blockLines(2), blockLines(3), blockLines(4), blockLines(blockLines(5)))
To start with, load your questions this way:
Dim questions = _
File _
.ReadLines("questions.txt") _
.Select(Function (x, n) New With { .Line = X, .Index = n }) _
.GroupBy(Function (xn) xn.Index \ 6, Function (xn) xn.Line) _
.Select(Function (xs) New Question() With _
{ _
.Question = xs(0), _
.Choice1 = xs(1), _
.Choice2 = xs(2), _
.Choice3 = xs(3), _
.Choice4 = xs(4), _
.Answer = xs(5) _
}) _
.ToArray()
That'll give you an array for your questions:
Next, you need to create a "queue.txt" file that contains the indices of your questions in a random order that you wish to display them in. Here's how to create your queue:
Dim rnd = New Random()
Dim queue = _
Enumerable _
.Range(0, questions.Length) _
.OrderBy(Function (n) rnd.Next()) _
.Select(Function (n) n.ToString()) _
.ToArray()
File.WriteAllLines("queue.txt", queue)
Then when you load your program, you can read this file and choose the next question to ask, and save the file, skipping the first question for next time, like this:
Dim queue = File.ReadAllLines("queue.txt")
Dim questionToAsk As Question = questions(Integer.Parse(queue.First()))
File.WriteAllLines("queue.txt", queue.Skip(1))
It would be up to you to make sure that the file is created when it doesn't exist and to write the code that checks if you've asked all the questions and need to re-create the queue.
Here are the basics of a class that uses XML.
Public Class QuestionAndAnswer
'the container for all questions/answers
Private ReadOnly qa As XElement = <QandA></QandA>
'the container for a question and some number of possible answers
Private ReadOnly ent As XElement = <entry></entry>
'the question
Private ReadOnly aquestion As XElement = <question></question>
'an answer - the c attribute will be "y" for the correct answer
Private ReadOnly ananswer As XElement = <answer c=""></answer>
Private theQA As XElement
Public Sub New()
Me.theQA = New XElement(qa) 'set up
End Sub
Public Sub New(path As String)
Me.theQA = XElement.Load(path)
End Sub
Public Sub Save(path As String)
Me.theQA.Save(path)
End Sub
Private Function AddQuestion(theQuestion As String, correctAnsw As String) As XElement
Dim e As New XElement(ent)
Dim q As New XElement(aquestion)
Dim a As New XElement(ananswer)
q.Value = theQuestion
a.Value = correctAnsw
a.#c = "y"
e.Add(q)
e.Add(a)
Me.theQA.Add(e)
Return e
End Function
Public Function AddQuestion(theQuestion As String, correctAnsw As String,
ans1 As String) As XElement
Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw)
Dim a As New XElement(ananswer)
a.Value = ans1
e.Add(a)
Return e
End Function
Public Function AddQuestion(theQuestion As String, correctAnsw As String,
ans1 As String, ans2 As String) As XElement
Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw, ans1)
Dim a As New XElement(ananswer)
a.Value = ans2
e.Add(a)
Return e
End Function
Public Function AddQuestion(theQuestion As String, correctAnsw As String,
ans1 As String, ans2 As String, ans3 As String) As XElement
Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw, ans1, ans2)
Dim a As New XElement(ananswer)
a.Value = ans3
e.Add(a)
Return e
End Function
Public Function AddQuestion(theQuestion As String, correctAnsw As String,
ans1 As String, ans2 As String, ans3 As String, ans4 As String) As XElement
Dim e As XElement = Me.AddQuestion(theQuestion, correctAnsw, ans1, ans2, ans3)
Dim a As New XElement(ananswer)
a.Value = ans4
e.Add(a)
Return e
End Function
Private Shared prng As New Random
Public LastQuestionAnswer As String
Public Function RandomQuestion() As String
Dim q As XElement = Me.SelectRandomQ
If q IsNot Nothing Then
Dim rv As New System.Text.StringBuilder
rv.AppendLine(q.<question>.Value)
rv.AppendLine()
Dim ie As IEnumerable(Of XElement)
ie = From qa In q.<answer>
Select qa
ie = ie.OrderBy(Function() prng.Next(q.<answer>.Count))
Dim x As Integer = 1
For Each a As XElement In ie
If a.#c = "y" Then
Me.LastQuestionAnswer = x.ToString
End If
rv.AppendFormat("{0}. {1}", x, a.Value)
rv.AppendLine()
x += 1
Next
rv.AppendLine()
Me.LastQuestionAnswer = Me.LastQuestionAnswer.Insert(0, rv.ToString)
Debug.WriteLine(Me.LastQuestionAnswer)
Return rv.ToString
End If
Return ""
End Function
Private Function SelectRandomQ() As XElement
If Me.theQA IsNot Nothing AndAlso Me.theQA.<entry>.Count > 0 Then
Dim ie As IEnumerable(Of XElement)
ie = From ent In Me.theQA.Elements
Where ent.#used <> "y"
Select ent
Dim rv As XElement = ie(prng.Next(ie.Count))
rv.#used = "y"
Return rv
End If
Return Nothing
End Function
End Class
A form with a richtextbox and a button shows it
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim foo As New QuestionAndAnswer
foo.RandomQuestion()
foo.AddQuestion("Which Vietnam War film directed by Francis Ford Coppola followed a screenplay based on famous adventure story Heart of Darkness?",
"Apocalypse Now",
"Good Morning Vietnam",
"Born on the Fourth of July",
"Band of Brothers")
foo.AddQuestion("What was the name of the baseball pitcher that hit a bird with a pitch during a 2001 Spring Training game?",
"Randy Johnson",
"Mike Mussina",
"Roger Clemens",
"Greg Maddux",
"Johan Santana")
foo.AddQuestion("The third largest desert in the world is the Sahara, what is the first?",
"Antarctic",
"Gobi",
"Sonoran")
foo.AddQuestion("How many US presidents have died while in office?",
"8",
"6",
"7")
foo.AddQuestion("If x is the first of five consecutive odd numbers > 0, then what is their average?",
"x + 4",
"x",
"x + 1",
"x + 3")
Dim qa As New System.Text.StringBuilder
foo.RandomQuestion()
qa.AppendLine(foo.LastQuestionAnswer)
qa.AppendLine()
foo.RandomQuestion()
qa.AppendLine(foo.LastQuestionAnswer)
qa.AppendLine()
foo.RandomQuestion()
qa.AppendLine(foo.LastQuestionAnswer)
RichTextBox1.Text = qa.ToString
End Sub

Set string values inside for without knowing names

I am trying to find the correct way to set the string values inside the For without knowing the actual numbers. here's what i am trying to do as it was possible in vb6 but not sure using vb.net
Public Class Form1
Dim iTest1 As String
Dim iTest2 As String
Dim iTest3 As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To 3
"iTest" & i = "aaa" & i
Next
Debug.Print("iTest1:" & iTest1)
Debug.Print("iTest2:" & iTest2)
Debug.Print("iTest3:" & iTest3)
End Sub
End Class
Try using Arrays instead.
Dim iTest(3) As String
For i = 1 To 3
iTest(i) = "aaa" & i
Next
Or this
Dim variables As New Dictionary(Of String, String)()
For i = 1 To 3
variables("iTest" + i.ToString) = "aaa" & i
Next
Console.WriteLine("iTest1:" + variables("iTest1"))
Console.WriteLine("iTest2:" + variables("iTest2"))
Console.WriteLine("iTest3:" + variables("iTest3"))
It's technically possible, but not really a recommended approach...
If you make the variables Public, then you can use the legacy CallByName() function brought over from VB6:
Public Class Form1
Public iTest1 As String
Public iTest2 As String
Public iTest3 As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 3
CallByName(Me, "iTest" & i, CallType.Let, "aaa" & i)
Next
Debug.Print("iTest1:" & iTest1)
Debug.Print("iTest2:" & iTest2)
Debug.Print("iTest3:" & iTest3)
End Sub
End Class
Without CallByName(), this can be accomplished via Reflection. Note that this works with Private or Public variables:
Public Class Form1
Private iTest1 As String
Private iTest2 As String
Private iTest3 As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim T As Type = Me.GetType
For i As Integer = 1 To 3
Dim F As Reflection.FieldInfo = T.GetField("iTest" & i, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
If Not IsNothing(F) Then
F.SetValue(Me, "aaa" & i)
End If
Next
Debug.Print("iTest1:" & iTest1)
Debug.Print("iTest2:" & iTest2)
Debug.Print("iTest3:" & iTest3)
End Sub
End Class

Reading from and manipulating a .csv file

I have multiple .csv files for each month which go like:
01/04/2012,00:00,7.521527,80.90972,4.541667,5.774305,7,281.368
02/04/2012,00:00,8.809029,84.59028,6.451389,5.797918,7,274.0764
03/04/2012,00:00,4.882638,77.86806,1.152778,15.13611,33,127.6389
04/04/2012,00:00,5.600694,50.35417,-3.826389,15.27222,33,40.05556
The format is : Date in the form dd/mm/yy,Current time,Current temperature,Current humidity,Current dewpoint,Current wind speed,Current wind gust,Current wind bearing
The program needs to calculate the average for
temperature
humidity
wind speed
wind direction
and display them on a text box.
any ideas?
Here is what I have done so far...
Option Strict On
Option Explicit On
Imports System.IO
Imports System
Public Class Form1
Private Sub cmb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
Me.Close()
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndata.Click
'This is for August
If cmb1.SelectedIndex = 1 Then
TextBox1.Clear()
Using reader As New StreamReader("c://temp/DailyAug12log.csv")
Dim line As String = reader.ReadLine()
Dim avgTemp As Integer
Dim fields() As String = line.Split(",".ToCharArray())
Dim fileDate = CDate(fields(0))
Dim fileTime = fields(1)
Dim fileTemp = fields(2)
Dim fileHum = fields(3)
Dim fileWindSpeed = fields(4)
Dim fileWindGust = fields(5)
Dim fileWindBearing = fields(6)
While line IsNot Nothing
counter = counter + 1
line = reader.ReadLine()
End While
avgTemp = CInt(fields(2))
avgTemp = CInt(CDbl(avgTemp / counter))
TextBox1.Text = TextBox1.Text & "Month = August" & vbCrLf & "Temperature Average: " & avgTemp & vbCrLf
End Using
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim files() As String
files = Directory.GetFiles("C:\Temp", "*.csv", SearchOption.AllDirectories)
For Each FileName As String In files
cmb1.Items.Add(FileName.Substring(FileName.LastIndexOf("\") + 1, FileName.Length - FileName.LastIndexOf("\") - 1))
Next
End Sub
End Class
Private Class Weather
Public SampleTimeStamp AS Date
Public Temperature AS Double
Public Humidity As Double
Public WindSpeed AS Double
Public WindBearing AS Double
End Class
Sub Main
Dim samples = ReadFile("c://temp/DailyAug12log.csv")
Dim avgTemperature = samples.Average(Function(s) s.Temperature)
...
End Sub
Private Function ReadFile(ByVal fileName as String) AS List(Of Weather)
Dim samples As New List(Of Weather)
Using tfp As new TextFieldParser(filename)
tfp.Delimiters = new String() { "," }
tfp.TextFieldType = FieldType.Delimited
While Not tfp.EndOfData
Dim fields = tfp.ReadFields()
Dim sample As New Weather()
sample.SampleTimeStamp = Date.ParseExact(fields(0) & fields(1), "dd\/MM\/yyyyHH\:mm", CultureInfo.InvariantCulture)
sample.Temperature = Double.Parse(fields(2), CultureInfo.InvariantCulture)
sample.Humidity = Double.Parse(fields(3), CultureInfo.InvariantCulture)
sample.WindSpeed = Double.Parse(fields(4), CultureInfo.InvariantCulture)
sample.WindBearing = Double.Parse(fields(5), CultureInfo.InvariantCulture)
samples.Add(sample)
End While
Return samples
End Using
End Function
I would not use a this aprroach - if the order of the columns changes, your program will show wrong results.
I would use a good csv reader like http://kbcsv.codeplex.com/ and read the Data to a datatable. then you can calculate your resulting columns quite easily and reliablly, because you can adress each column like MyDatatable.Cooumns["Headername"].

Visual Basic - Object reference not set to an instance of an object

I'm receiving the following error in VB.Net.
"Object reference not set to an instance of an object"
It highlights "Next" at the end of the For Loop.
Any help would be great.
Imports System.IO
Public Class LoginForm
Dim Username() As String
Dim Password() As String
Dim Index As Integer
Public Function encrypt(ByVal data As String) As String
Dim answer As String = ""
Dim I As Integer
data = RTrim(data)
If Mid(data, 1, 1) <> Chr(0) Then
For I = 1 To Len(data)
answer = answer + Chr(Asc(Mid(data, I, 1)) Xor 23)
' Xor 23 is a simple encription cipher, a string can be
' encrypted or de-encrypted by the value following the Xor
'i.e. "23" '
Next I
End If
encrypt = answer
End Function
Private Sub LoginButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles LoginButton.Click
For Each I In Username
If UserNameTextBox.Text = Username(Index) Then
UserAdd.Show()
Me.Hide()
If PasswordTextBox.Text = Password(Index) Then
MessageBox.Show("Correct Password")
Else
MessageBox.Show("Invalid Password, Sorry")
End If
Else : MessageBox.Show("Invalid Username, Sorry")
End If
Next
End Sub
Public Sub ReadUsers()
Dim CurrentFileReader As StreamReader
Dim FileName, Line As String
Dim Delimiter As Char = ","
Dim Feild() As String
Dim Username() As String
Dim Password() As String
Dim Index As Integer
FileName = "C:\Computing\Projects\Login\Users.txt" 'location of
'user file
CurrentFileReader = New StreamReader(FileName)
Do Until CurrentFileReader.EndOfStream
Line = CurrentFileReader.ReadLine
If Line = Nothing Then
Exit Do
End If
ReDim Preserve Username(Index)
ReDim Preserve Password(Index)
Feild = Line.Split(Delimiter)
Username(Index) = encrypt(Feild(0))
Password(Index) = encrypt(Feild(1))
Loop
End Sub
Private Sub LoginForm_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
Call ReadUsers()
End Sub
End Class
Try replacing this code:
For Each I In Username
If UserNameTextBox.Text = Username(Index) Then
UserAdd.Show()
Me.Hide()
If PasswordTextBox.Text = Password(Index) Then
MessageBox.Show("Correct Password")
Else
MessageBox.Show("Invalid Password, Sorry")
End If
Else : MessageBox.Show("Invalid Username, Sorry")
End If
Next
with this code:
For Each I In Username
if Username(i) is not null then
If UserNameTextBox.Text = Username(Index) Then
UserAdd.Show()
Me.Hide()
If PasswordTextBox.Text = Password(Index) Then
MessageBox.Show("Correct Password")
Else
MessageBox.Show("Invalid Password, Sorry")
End If
Else : MessageBox.Show("Invalid Username, Sorry")
End If
else
....handle empty string
end if
Next
Which next are yout refering to?
In your second for, define that is I. That may not solve the problem, but this is definitly a better practive.
Is it possible that your data constains a 'null' character (chr(0))?
Mid will return null if it reaches the end of the string , but it doesn't look like this would happen to you.
Nevertheless, you might want to use String.Substring instead of mid. It's a function found with the string object.
I'll take a guess that it's the "For Each I In Username" loop inside LoginButton_Click that's causing you the problem?
I'm guessing at this loop as the type of variable "I" does not appear to be declared, so it would be type Object by default, matching the error "Object reference not set to an instance of an object".
Sub ReadUsers(), uses the locally defined variables for Username, Index and Password. Remove these lines from Sub ReadUsers().
Dim Username() As String
Dim Password() As String
Dim Index As Integer
At your class level.
A. Add this Imports to the top of the file:
Imports System.Collections.Generic
B. Change your String array definitions to List(of String)
Dim Username As List(Of String)
C. Then you no longer need to Redim. Just:
Username.add(encrypt(Feild(0)))
Loop on the count instead of item:
For i as integer = 0 to Username.length - 1
If UserNameTextBox.Text = Username(i) Then
...
Next
And finally, here's your code:
Imports System.IO
Imports System.Collections.Generic
Public Class LoginForm
' At the Class level Dim is equivalent to Private
Private Username As List(Of String)
Private Password As List(Of String)
Private Index As Integer
Public Function encrypt(ByVal data As String) As String
Dim answer As String = ""
Dim I As Integer
data = RTrim(data)
If Mid(data, 1, 1) <> Chr(0) Then
For I = 1 To Len(data)
answer = answer + Chr(Asc(Mid(data, I, 1)) Xor 23)
' Xor 23 is a simple encription cipher, a string can be
' encrypted or de-encrypted by the value following the Xor
'i.e. "23" '
Next I
End If
encrypt = answer
End Function
Private Sub LoginButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles LoginButton.Click
For i As Integer = 0 To Username.length - 1
If UserNameTextBox.Text = Username(i) Then
UserAdd.Show()
Me.Hide()
If PasswordTextBox.Text = Password(i) Then
MessageBox.Show("Correct Password")
Else
MessageBox.Show("Invalid Password, Sorry")
End If
Else : MessageBox.Show("Invalid Username, Sorry")
End If
Next
End Sub
Public Sub ReadUsers()
Dim CurrentFileReader As StreamReader
Dim FileName, Line As String
Dim Delimiter As Char = ","
Dim Feild() As String
FileName = "C:\Computing\Projects\Login\Users.txt" 'location of
'user file
CurrentFileReader = New StreamReader(FileName)
Do Until CurrentFileReader.EndOfStream
Line = CurrentFileReader.ReadLine
If Line = Nothing Then
Exit Do
End If
Feild = Line.Split(Delimiter)
Username.Add(encrypt(Feild(0)))
Password.add(encrypt(Feild(1)))
Loop
End Sub
Private Sub LoginForm_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
Call ReadUsers()
End Sub
End Class