把一个列表到一个数组? [英] putting a list into an array?

查看:195
本文介绍了把一个列表到一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我只想说,我在vb.net一个初学者,只是一般的编码。
如果可能的话,我们的教授可能是为了供我们使用可能的最简单的方法
所以,请不要认为做什么,我试图做的花哨和抽象方法。
谢谢:)

Firstly, I just want to say I'm a beginner in vb.net and just coding in general. If possible, our professor probably intended for us to use the simplest method possible So please don't suggest fancy and abstract methods for doing what I'm trying to do. Thank you :)

所以我有一个列表框。有项目没有定数。
因为我想我可以进入尽可能多的数字
我想在列表框中的所有项目转换为数组
但很明显,你看不到一个数组。
所以我做了它,这样它打印出自己的标签。
但是,只有输入到列表框中的最后一个数字就出来了。

So I have a listbox. There's no set number of items. I can enter as many numbers as I want And I want to convert all the items in that listbox into an array But obviously, you can't see an array. So I made it so that it prints itself out in a label. But only the last number inputted into the listbox came out.

我的code获取列表框到一个数组是这样的:

My code for getting the listbox into an array is this:

Dim i As Integer

For i = 0 To lstbxInput.Items.Count
    dblarray(i) = CDbl(lstbxInput.Items(i))
Next i

和在该子,dblarray如双用作参考参数

And in this sub, dblarray as double was used as a reference parameter.

和打印出数组中,我用这个code:

And to print out the array, I used this code:

Dim DblArray(lstbxInput.Items.Count - 1) As Double

getNumbers(DblArray)
lblLrgAns.Text = DblArray(lstbxInput.Items.Count - 1).ToString

我不完全理解BYREF,希望我用正确的方式。
我用灰色那里dblarray因为这是教授告诉我们做的。

I don't fully understand Byref and hopefully I used it the right way. I used dimmed the dblarray there because that's what the professor told us to do.

推荐答案

根据下方的code,你得到了数组( DblArray )正是你想

Based on your code below, you have got the array (DblArray) exactly what you wanted

Dim DblArray(lstbxInput.Items.Count - 1) As Double

getNumbers(DblArray)

现在关于你的问题:

但是,只有输入到列表框中的最后一个数字就出来了。

But only the last number inputted into the listbox came out.

这是因为这种语法

lblLrgAns.Text = DblArray(lstbxInput.Items.Count - 1).ToString

您只需要 DblArray 的最后一个项目,这是 DblArray(lstbxInput.Items.Count - 1) 。你需要得到什么是 DblArray(0)(第一项), DblArray(1)(第二项), DblArray(2)(第三项),...,直到 DblArray(lstbxInput.Items.Count - 1)(最后一个项目)。假设你想将所有的 DblArray 项指定到 lblLrgAns.Text 用逗号隔开,你需要像一个循环以下

You only take the last item of DblArray, which is DblArray(lstbxInput.Items.Count - 1). What you need to get is DblArray(0) (the first item), DblArray(1) (the second item), DblArray(2) (the third item), ... , up until DblArray(lstbxInput.Items.Count - 1) (the last item). Assuming you want to assign all of DblArray items to lblLrgAns.Text separated by comma, you'll need a loop like below

Dim i As Integer

For i = 0 To DblArray.Length - 1
    lblLrgAns.Text = lblLrgAns.Text & DblArray(i).ToString

    ' add a comma unless it's the last index
    If (i < DblArray.Length - 1)
        lblLrgAns.Text = lblLrgAns.Text & ", "
    End If
Next i

这篇关于把一个列表到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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