如何在VB.NET中访问数组以从文本文件中获取文本 [英] How do I access an array to in VB.NET to get text from a text file

查看:151
本文介绍了如何在VB.NET中访问数组以从文本文件中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段文字,其中包含1928年至2014年的奥斯卡电影列表。我正在使用一个数组来查找电影的名称并将其放入一个文本框中,并将其放入下一个。该类型是电影名称的旁边。我试图分别访问这两个文本框并将其放入两个不同的文本框中。



我尝试了什么:



Dim mvs(86)As Movie



Private Sub FrmAcademy_Load()处理MyBase.Load

Dim mviLine As String

Dim mviData(1)As String

Dim allMovies()As String = IO.File.ReadAllLines(Oscars.txt)

对于mviList As Integer = 0到86

mviData = mviLine.Split(,c)

mvs(mviList).mviName = mviData(0)

mvs(mviList).gnrName = mviData(1)

Dim mviYear As Integer = 0

Dim mviIndex As Integer = 0

If(IsNumeric(txtAwards.Text))= True然后

mviYear = CInt(txtAwards.Text)

结束如果

如果(mviYear> = 1928)或(mviYear< = 2014)那么

mviIndex = 19 28 - mviYear

txtFilm.Text = mvs(mviIndex).mviName

txtGenre.Text = mvs(mviIndex).mviName

结束如果< br $>
下一页

结束子



私有子btnPicture_Click(发件人为对象,e为EventArgs)处理btnPicture.Click

If(IsNumeric(txtAwards.Text))= False然后

MessageBox.Show(请输入一年作为数值)

ElseIf CInt(txtAwards.Text)> 2014年然后

MessageBox.Show(请选择一年的范围)

ElseIf CInt(txtAwards.Text)< 1928年

MessageBox.Show(请选择范围内的一年)

结束如果

I have a piece of text that has a list of Oscar movies from 1928 - 2014. I'm using an array to find the name of the film and place it into one textbox and the genre in the next. The genre is next to the movies name. I'm trying to access the two separately and place it into two different textboxes.

What I have tried:

Dim mvs(86) As Movie

Private Sub FrmAcademy_Load() Handles MyBase.Load
Dim mviLine As String
Dim mviData(1) As String
Dim allMovies() As String = IO.File.ReadAllLines("Oscars.txt")
For mviList As Integer = 0 To 86
mviLine = allMovies(mviList)
mviData = mviLine.Split(","c)
mvs(mviList).mviName = mviData(0)
mvs(mviList).gnrName = mviData(1)
Dim mviYear As Integer = 0
Dim mviIndex As Integer = 0
If (IsNumeric(txtAwards.Text)) = True Then
mviYear = CInt(txtAwards.Text)
End If
If (mviYear >= 1928) Or (mviYear <= 2014) Then
mviIndex = 1928 - mviYear
txtFilm.Text = mvs(mviIndex).mviName
txtGenre.Text = mvs(mviIndex).mviName
End If
Next
End Sub

Private Sub btnPicture_Click(sender As Object, e As EventArgs) Handles btnPicture.Click
If (IsNumeric(txtAwards.Text)) = False Then
MessageBox.Show("Please enter a year as a numeric value")
ElseIf CInt(txtAwards.Text) > 2014 Then
MessageBox.Show("Please choose a year in range")
ElseIf CInt(txtAwards.Text) < 1928 Then
MessageBox.Show("Please choose a year in range")
End If

推荐答案

启动调试器并检查 mviIndex 的值,你会发现它是负数。

尝试

Launch debugger and inspect value of mviIndex, you will find that it is negative.
Try
mviIndex = mviYear - 1928





有一个工具允许您查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近一个错误。



There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


If (mviYear >= 1928) Or (mviYear <= 2014) Then
mviIndex = 1928 - mviYear



这将为您提供1928年后任何一年的负面指数。 If 语句中的运算符应为不是


That will give you a negative index for any year after 1928. And that operator in the If statement should be And not Or


这篇关于如何在VB.NET中访问数组以从文本文件中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆