多个对象实例 [英] multiple object instances
问题描述
你好,我有两个问题
1)
我已经编写了一个类,并希望创建它的多个实例而没有预先定义的限制(例如,在dim语句的实例中实例化并命名新实例)
我是编程新手,但已经读了几本书,但是它们只向您展示如何创建一个实例,或者两个具有预定义名称的实例.我尝试在dim语句中使用变量作为字段名称,但是它不允许我这样做
管理多个实例的技术是什么
2)
我实际上有两个类,我想要多个实例,例如"A"和"B".然后,我要说,A的第三个实例能够在"B"的第五个实例中调用方法,再次实例是即时生成的.
我已经搜索并阅读了有关引用和指针的信息,这在Google中只是一种猜测,但它们似乎都不相关,除非我只是没有理解它
我会在Google上阅读并在
上阅读的内容会更加满意.
谢谢您的帮助.
Hello I have two problems
1)
I have written a class and would like to create multiple instances of it without having a pre-defined limit(ie like a select case of dim statements instantiating and naming the new instance''s)
I am new to programming but have read a couple of books ,but they only show you how to create a single instance,or a couple with pre defined names. I tried using a variable as the field name in the dim statement ,but it wont let me do that
Whats the technique for managing multiple instances
2)
I actually have two classes that I want multiple instances of say, "A" and "B" .Then I want say, the third instance of A to be able to call a method in the fifth instance of "B" ,with again the instances being made on the fly.
I have searched and read about references and pointers as a guess in Google but neither of them seem relevant,unless I just didnt get it
I will be more then happy with just something I can google and read up on
Thank you for your help
推荐答案
我写了一个类,想要创建它的多个实例而没有预先定义的限制(例如,在dim语句的实例中实例化并命名新实例)
I have written a class and would like to create multiple instances of it without having a pre-defined limit(ie like a select case of dim statements instantiating and naming the new instance''s)
创建对象的任意数量的实例的方法是使用通用列表,然后您可以随意添加该列表.
The way to create an arbitrary number of instances of an object, is to use a generic List, which you can then add to as often as you like.
我实际上有两个类,我想要多个实例,例如"A"和"B".然后,我想说第三个A的实例可以在"B"的第五个实例中调用方法,而这些实例又是即时生成的.
I actually have two classes that I want multiple instances of say, "A" and "B" .Then I want say, the third instance of A to be able to call a method in the fifth instance of "B" ,with again the instances being made on the fly.
您可以使用委托在类之间连接方法,但是以任意方式执行您要描述的内容对我来说是行不通的.您当然可以创建两个类的特定实例,并使用委托将它们彼此挂钩,但是要以任意,临时的方式这样做,我看不出这样做的原因.代理人是您的工作方式.
You can use delegates to hook up methods between classes, but to do what you''re describing in an arbitrary way sounds unworkable to me. You can certainly create a specific instance of both classes, and use a delegate to hook them to each other, but to do that in an arbitrary, ad hoc manner, I don''t see a reason for it. Delegates are how you do it tho.
我会仔细研究.代理人与我关系不大,谢谢基督徒
I will look into it .Delegates are not something I''ve had much to do with yet,thank you Christian
克里斯蒂安建议的替代方法是使用字典,该字典使用键"和值".您可以将对象添加到字典中,并稍后使用键将其检索.键可以是您想要的任何类型.
An alternative to Christian''s suggestion would be to use a dictionary, which uses "keys" and "values". You can add your objects to the dictionary and retrieve them later using a key. The keys can be any type you want them to be.
这篇关于多个对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!