[英]VB.net Logic Error
This is supposed to be a program that tells how long it would take you to download a given size of file after the user inputs their download speed. 这应该是一个程序,它告诉用户在输入用户的下载速度后,下载给定大小的文件将花费多长时间。 intOne doesn't seem to be affected at all by the function that I have created to determine what intOne's value should be. 我创建的用于确定intOne值应该是什么的函数似乎根本不影响intOne。 I'm reasonably sure that the equation I'm using is correct. 我有理由确定我使用的方程式是正确的。
Public Class Form1
Private tSize As System.Drawing.Size
Private checkUserSpeed As Long = 0
Private checkBitByte As Integer = 0
Private setSize As Integer = 0
Private checkUserSize As String = ""
Private Answer As Double = 0
Private ReadOnly Property checkDwnldSize() As String
Get
Return ComboBox1.Text
End Get
End Property
Function checkDownloadSize(ByVal checkUserSize As String)
Dim suffixes As New List(Of String)({"b", "k", "m", "g", "t", "p", "e", "z"})
Dim index As Long = suffixes.IndexOf(checkUserSize.Substring(0, 1).ToLower) > -1
If index > -1 Then
Return checkUserSpeed / 1024 ^ index
Else
Return False
End If
End Function
Function checkForByte(ByVal checkUserSize)
If Microsoft.VisualBasic.Right(checkUserSize.ToLower, 7) = "byte(s)" Then
checkBitByte = 1
checkDownloadSize(checkUserSize)
Return True
End
Else
Return False
End If
Return checkBitByte
Return checkUserSpeed
End Function
Function checkForBit(ByVal checkUserSize)
If Microsoft.VisualBasic.Right(checkUserSize.ToLower, 6) = "bit(s)" Then
checkBitByte = 8
checkDownloadSize(checkUserSize)
Return True
End
Else
checkForByte(checkUserSize)
Return False
End If
Return checkBitByte
Return checkUserSpeed
End Function
Function Calculate(ByVal checkUserSpeed, ByVal checkUserSize)
checkForBit(checkUserSize)
Return Answer
End Function
Private Sub FitContents()
tSize = TextRenderer.MeasureText(TextBox3.Text, TextBox3.Font)
TextBox3.Width = tSize.Width + 10
TextBox3.Height = tSize.Height
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Calculate(Convert.ToInt32(TextBox3.Text), ComboBox3.Text)
Answer = checkBitByte * ((1024 ^ setSize) / checkUserSpeed)
TextBox1.Text = Answer
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Call FitContents()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Form3.Show()
End Sub
End Class
Help would be greatly appreciated. 帮助将不胜感激。
You never assign any value to intOne, so it starts with value 0. So this: 您永远不会为intOne分配任何值,因此它以值0开头。因此:
intOne = intOne / 1024 ^ 3
is the same as this: 与此相同:
intOne = 0 / 1024 ^ 3
which is of course this: 这当然是这样的:
intOne = 0
As an aside: 作为旁白:
Don't use Dim at the class/form level, use Private. 不要在课程/表单级别使用Dim,而要使用Private。
Always use As <ReturnType>
when decalring you functions 在对函数进行As <ReturnType>
时始终使用As <ReturnType>
Turn on Option Strict in your language settings. 在您的语言设置中启用“严格选项”。
Dim intTwo As Integer = Nothing
doesn't do anything useful. Dim intTwo As Integer = Nothing
都没做。 Use Dim intTwo As Integer = 0
instead. 请改用Dim intTwo As Integer = 0
。
Always declare your variables as a type as well ( Dim tSize
is missing its type) 始终也将变量声明为类型( Dim tSize
缺少其类型)
Followup to your edits: 跟进您的编辑:
This: 这个:
Function checkForByte(ByVal checkUserSize)
Should be this: 应该是这样的:
Function checkForByte(ByVal checkUserSize As Long)
Fix this in all places in your code. 在代码的所有位置修复此问题。 Everything should have an As DataType . 一切都应具有As DataType 。 If you turned on Option Explict and Option Strict like was suggested the compiler will locate these problems for you. 如果您像建议的那样打开Option Explict和Option Strict,则编译器将为您找到这些问题。
Next, do not name local variables and/or parameters the same as your class/form level private variables. 接下来,不要将局部变量和/或参数命名为与类/表单级私有变量相同的名称。 I personally prefix my instance variables like this: 我个人为实例变量添加了如下前缀:
Private _checkUserSpeed As Long = 0
Note the underscore. 注意下划线。 Some people use a lowercase m instead: 有人改用小写的m:
Private mCheckUserSpeed As Long = 0
Pick something and be consistent. 选择一些东西并保持一致。 This makes it clear the scope of a variable. 这样可以清楚地说明变量的范围。
Also this code is bad, the second Return will never be hit: 这段代码也很糟糕,第二个Return永远不会被击中:
Return checkBitByte
Return checkUserSpeed
Finally, assuming you are trying to populate the variable _checkUserSpeed
with a value, you are still not ever doing it. 最后,假设您试图用一个值填充变量_checkUserSpeed
,那么您仍然不会这样做。 Search your code and look for any place you are doing this: 搜索您的代码并查找您正在执行此操作的任何位置:
_checkUserSpeed = something
You won't find it. 你找不到。 So that variable will always be 0. 因此该变量将始终为0。
None of these are really VB.NET specific issues. 这些都不是真正的VB.NET特定问题。 All of these best-practices would apply equally to C# or any other langue. 所有这些最佳实践将同样适用于C#或任何其他语言。
You should look into cleaning up your code first, to improve readability. 您应该先清理代码,以提高可读性。 Avoid code repetition at all costs. 不惜一切代价避免代码重复。 If you think you cannot do otherwise, lean towards data repetition instead, ie create an XML file to store your values, but keep the code clean. 如果您认为自己无法做到这一点,请改用数据重复,即创建一个XML文件来存储您的值,但保持代码干净。 Any errors you find later will then stand out like white crows. 您以后发现的任何错误将像乌鸦一样突出。
For example, something like this: 例如,如下所示:
Function checkDownloadSize(ByVal strOne As String)
Dim suffixes As New List(Of String)({"b", "k", "m", "g", "t", "p", "e", "z"})
Dim index As Long = suffixes.IndexOf(strOne.Substring(0, 1).ToLower) > -1
If index > -1 Then
Return intOne / 1024 ^ index
Else
Return False
End If
End Function
roughly replaces your two pages of checkDownloadSize
and checkSize
. 大致替换您的两页checkDownloadSize
和checkSize
。
Notice how every function here is called only once. 注意这里的每个函数仅被调用一次。 If you later decide to swap ToLower
with your custom implementation, it needs to be done only once. 如果您以后决定将ToLower
与您的自定义实现交换,则只需完成一次。 Not only a performance improvement, it also makes your code more maintainable in the long run. 从性能上来讲,从长远来看,它还使您的代码更具可维护性。
Another point is that I am not using Microsoft.VisualBasic
namespace in the above code, and switched to Substring
instead. 另一点是,我不在上面的代码中使用Microsoft.VisualBasic
命名空间,而是改用了Substring
。 This will ease your burden with migration to C#, and also make native C# devs understand your code better. 这将减轻您迁移到C#的负担,并使本机C#开发人员更好地了解您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.