使用类似于任何其他语言的变量 [英] Using variables like they are used in any other language

查看:58
本文介绍了使用类似于任何其他语言的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Guys,

今晚决定选择一些Visual Basic。我正在尝试制作一个简单的"税收"计算器。选择一个国家(使用组合框)并获得一个速率乘以输入另一个文本框的金额

Decided to pick up some Visual Basic tonight. I'm trying to make a simple 'tax' calculator. Select a country (with a combobox) and get a rate that is multiplied by the amount of money entered into another textbox

我过去两年主要使用python和c ++所以我'我习惯于编程,类和函数。我似乎无法弄清楚它在Visual Basic中是如何工作的。我正在使用SelectedIndexChanged事件(不确定是否被调用)到
来选择该县。现在,我想在按下"计算税金"按钮时从文本框和国家/地区获取值,以计算剩余金额并在消息框中显示。我可以打开一个消息框,但无法访问组合框事件中创建的
的值。任何人都有关于如何做到这一点的提示?我是否需要公开这些变量?

I've used mainly python and c++ the last two years so i'm quite used to programming, classes and functions. I can't really seem to figure out how this works in Visual Basic. I'm using a SelectedIndexChanged-event (not sure if it is called that) to get the county selected. Now I want to get the value from the text box and the country when the 'calculate taxes' button is pressed, to calculate the amount of money left and display this in a messagebox. I can open a message box but cannot access values created in the combobox event. Anyone got tips on how to do this? Do I need to make these variables public?










推荐答案

你可以使用
字典
(Of String,Double)和加载它的关键是国名和价格的价值。比如我在这个帖子中显示

组合框值显示问题!

Well you could use a Dictionary(Of String, Double) and load its key with the country names and the value for the rate. Such as I show in this thread Combo Box Value Display Issues!.

下面是代码。在ComboBox的选定索引更改事件子中,字典将返回提供给它的Key的值。或者代码可能会帮助您获取ComboBox的选定项ToString。

Below is the code. In the ComboBox's selected index changed event sub the dictionary will return the Value for the Key provided to it. Or perhaps the code will assist you with getting the ComboBox's selected item ToString.

除非您正在使用需要访问公共的多个类或模块,否则您不需要公开任何变量变量。虽然可能需要全局变量声明。由于你已经编程了一段时间,你可以在代码窗口的顶部放置
Option Strict On,如果代码中有任何基本错误,它们将会出错。例如不转换类型等。

You shouldn't need to make any variables public unless you are working with multiple classes or modules that need to access public variables. Although global declaration of variables may be necessary. Since you've been programming for awhile you can place Option Strict On at the top of the code window and if any basic errors are in the code they will error. Such as not converting types and such.

这是
Visual Basic指南

MSDN Library搜索引擎

Option Strict On

Public Class Form1

    Dim ComboBox1Input As New Dictionary(Of String, Integer)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))

        ComboBox1Input.Add("Weight Training", 70)
        ComboBox1Input.Add("Yoga Class", 80)
        ComboBox1Input.Add("Cardio Training", 95)
        ComboBox1Input.Add("Swimming Lessons", 40)
        ComboBox1Input.Add("Mind and Body Training", 50)

        For i = 0 To ComboBox1Input.Count - 1
            ComboBox1.Items.Add(ComboBox1Input.Keys(i))
        Next

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox1.Text = CStr(ComboBox1Input(ComboBox1.SelectedItem.ToString))
    End Sub

End Class

还使用代码块发布代码。在您的帐户经过验证之前,您无法发布图片和可能的超链接。

Also use a Code Block for posting code. You can't post images and possibly hyperlinks until your account is verified.


这篇关于使用类似于任何其他语言的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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