如何在循环中声明具有唯一名称的变量? [英] How do I declare variables with unique names in a loop?

查看:122
本文介绍了如何在循环中声明具有唯一名称的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有相似名称的数字变量,例如name1,name2,name3等。我可以使用固定循环来执行此操作吗?不,我不能使用列表或数组。



我不能使用数组,因为我正在尝试制作标签。

I want to create a number variables all with similar names e.g. name1, name2, name3 and so on. Can I use a fixed loop to do this? And nope I can't use a list or an array.

I can't use an array because I'm trying to make labels.

Dim lbl As New Label
            lbl.Size = New System.Drawing.Size(20, 35)
            lbl.Location = New System.Drawing.Point(15, 15)
            lbl.Text = "label text"
            Me.Controls.Add(lbl)collection



我的问题是如何将这段代码放入循环中?


My question is how can I put this code inside a loop?

推荐答案

我知道这个问题没有列表但是如果您使用KeyValuePair列表,则可以完全执行你在找什么。



这是最基本的形式,看起来像这样



I know that question says no lists but if you use a list of KeyValuePair, you can do exactly what you are looking for.

In it's most basic form, it looks like this

Dim Keys As New List(Of KeyValuePair(Of String, String))
Keys.Add(New KeyValuePair(Of String, String)("Example1", "Value1"))
Keys.Add(New KeyValuePair(Of String, String)("Example2", "Value2"))





然后当你想根据你可以使用的变量获取值...





Then when you want to fetch the value based on the variable you can use this...

Dim Output As String = Keys("Example1").Value





在你的例子中,只需创建一个基础类...





In your example, just create a basic class...

Public Class PlotPoint

    Public Size As System.Drawing.Size
    Public Location As System.Drawing.Size
    Public Text As String

End Class





并将其用作KeyPair的价值部分





And use that as the Value part of the KeyPair

Dim NewPoint As New PlotPoint
     NewPoint.Size = New System.Drawing.size(20, 35)
     NewPoint.Location = New System.Drawing.size(15, 15)
     NewPoint.Text = "Label Text"

     Dim PlotKeys As New List(Of KeyValuePair(Of String, PlotPoint))
     PlotKeys.Add(New KeyValuePair(Of String, PlotPoint)("Example1", NewPoint))





然后将它调回原来它们和以前一样。





In then calling it back out is they same as before.

Dim FetchPlot As PlotPoint = PlotKeys("Example1").Value





列出所有方式!

希望有所帮助,



List's all the way!
Hope that helps,


否 - 变量在编译时声明,与保存真实数据的实际实例不同。所以,即使你能做到这一点,你也无法使用你在其他任何地方创建的变量,因为系统不知道它们是什么 - 并且它们在循环结束时会超出范围,所以你不能使用它们即使它可以!



请记住,当你输入

No - variables are declared at compile time and aren't the same thing as the actual instance which holds the "real" data. So even if you could do it, you couldn't use the variables you created anywhere else because the system wouldn't know what they were - and they would go out of scope at the end of the loop anyway, so you couldn't use them even if it could!

Remember that when you type
Dim myVariable As New MyClass

您正在创建一个名为myVariable的新局部变量(它将超出范围并在包含块的末尾自动删除,无论是方法还是For循环),并分配一个新的MyClass实例 - 实例在方法退出后仍然存在,并且如果你保留了对它的引用,你仍然可以使用它,尽管myVariable不再存在。



说真的,这是为数组和列表创建的!

You are creating a new local variable called myVariable (which will go out of scope and be automatically deleted at the end of the containing block, be it method or For loop), and assigning a new instance of MyClass to it - the instance will still exist after the method exits, and provided you have kept a reference to it you can still use it, despite myVariable no longer existing.

Seriously, this is what arrays and lists are created for!


这篇关于如何在循环中声明具有唯一名称的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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