在VB中更新变量

时间:2016-03-22 11:30:25

标签: .net vb.net random integer lifetime

我想将当前游戏周期的猜测序数增加1.我已将该值初始设置为0,但在1之后不会更新 同样适用于trys的数量。我已将该值设置为21.但是一旦更新到20但不在那之后

def makeProductTable():
"""This creates a database with a blank table."""
with connect("products.db") as db:
    cursor = db.cursor()

    cursor.execute("""
        CREATE TABLE Product(
        ProductID integer,
        GTIN integer,
        Description string,
        StockLevel integer,
        Primary Key(ProductID));""")
    db.commit()

def editStockLevel():
with connect("products.db") as db:
    cursor = db.cursor()
    Product_ID=input("Please enter the id of the product you would like to change: ")
    Stock_Update=input("Please enter the new stock level: ")
    sql = "update product set StockLevel = ('Stock_Update') where ProductID = ('Product_ID');"
    cursor.execute(sql)
    db.commit()
return "Stock Level Updated." 

1 个答案:

答案 0 :(得分:2)

在Button事件处理程序中,RandNo始终为。请注意,在Form Load事件处理程序中声明的具有相同名称RandNo的变量的生命周期仅保留给该方法。

要避免这种情况,请按以下步骤操作:

首先(但可选),使用 Random 类,确保只初始化一次(否则您可以按照MSDN文档中的说明重复编号)。

其次,声明整数变量,该变量将以相同的方式存储随机值一次,方法块范围之外,分配其值并完成工作:

Public Class Form1

    Private ReadOnly rand As New Random
    Private value As Integer

    Private Sub Form1_Load() Handles MyBase.Load

        Me.value = rand.Next(minValue:=0, maxValue:=100)

    End Sub

    Private Sub Button1_Click() Handles Button1.Click
        ' Do what you want with Me.value
        '...
    End Sub

End Class