简体   繁体   English

如何以编程方式重新定义变量的类?

[英]How do I redefine the class of a variable programmatically?

Ok, so I have 2 classes, Note and HeldNote. 好的,所以我有2个类,即Note和HeldNote。 HeldNote inherits from Note and adds only 2 variables, plus a different way of setting visual dimensions. HeldNote继承自Note,仅添加了2个变量,以及设置视觉尺寸的另一种方法。 I have an array of an undefined amount of items, with each item defined as a member of class Note. 我有一个未定义数量的项目数组,每个项目都定义为Note类的成员。

Occasionally during my program, I want one of these items in the array to be a HeldNote rather than a Note, and I do not know which one until the program is running. 有时在我的程序中,我希望数组中的这些项之一是HeldNote而不是Note,并且在程序运行之前我不知道哪一项。

VB.Net seems fine with me declaring array(i) = New HeldNote but then wont let me access the variables that HeldNote has but Note does not. VB.Net似乎很好,我声明了array(i)= New HeldNote,但随后让我无法访问HeldNote拥有的变量,但Note没有。 Using Breaks in the program leads me to believe that array(i) is not actually being redefined as a HeldNote rather than a Note. 在程序中使用Breaks使我相信,array(i)实际上没有被重新定义为HeldNote而不是Note。

So, my question is, how can I make array(i) a HeldNote, rather than a Note? 所以,我的问题是,如何使array(i)成为HeldNote,而不是Note?

I'm using VB.net 2008. 我正在使用VB.net 2008。

Edit: I asked my Teacher about this problem and he said he didn't know and told me to look it up. 编辑:我问我的老师这个问题,他说他不知道,并告诉我查一下。 I couldn't anything online (had been looking since before i asked him) so I've come here for help. 我什么都在线(自问我之前就一直在找),所以我来这里寻求帮助。

A few suggestions: 一些建议:

1) Change your design and have just one class rather than one that inherits from another. 1)更改设计,只有一个类,而不是从另一个类继承的一个类。

2) Declare your array as type Object. 2)将数组声明为Object类型。 Each element of an Object array can be of a different type. 对象数组的每个元素可以具有不同的类型。 This way you can assign each element's type at runtime. 这样,您可以在运行时分配每个元素的类型。

Dim notearray As Object() = New Object() {New Note(), New HeldNote(), New HeldNote(), New Note()}

This is not a code issue, it's almost a OO question. 这不是代码问题,几乎是一个面向对象的问题。

"I have an array of an undefined amount of items, with each item defined as a member of class Note." “我有一个数量不确定的项目数组,每个项目都定义为Note类的成员。”

Then all of them are "Note". 然后所有这些都是“注”。 You can create class instances so: 您可以创建类实例,以便:

Note theNote = new HeldNote()

but you can't "downgrade" a instance on execution declared so: 但您不能在声明的执行中“降级”实例,因此:

Note theNote = new Note()".

If you need "randomly" a type change, you can create a ListOf(HeldNote). 如果需要“随机”更改类型,则可以创建ListOf(HeldNote)。 Then, you can decide the real type so: 然后,您可以这样确定实际类型:

Dim aList as new ListOf(HeldNote)
aList.add(new HeldNote())
aList.add(new HeldNote())
aList.add(new HeldNote())

Dim oNote as Note 
oNote = Ctype(aList(0),Note) 

Dim oNote as HeldNote 
oNote = aList(1)
...

The Array of Note (better use a List(Of Note) if you are unsure about the size) can store both Note and HeldNote because the latter derives from the first. Note数组(如果不确定大小,最好使用List(Of Note))可以存储Note和HeldNote,因为后者是从第一个派生的。

But, when you store an HeldNote in the array it is not automatically transformed in a Note, it is still an HeldNote, what you need is to try casting it back to an HeldNote 但是,当您将HeldNote存储在数组中时,它不会自动转换为Note,它仍然是HeldNote,您需要尝试将其强制转换回HeldNote

For example 例如

Sub Main

    Dim X(2) As Note

    X(0) = New Note() WIth {.Note = "A Message"}
    X(1) = New HeldNote() With {.Note = "Held Message", .Author = "Steve"}


    Dim h As HeldNote

    h = TryCast(X(0), HeldNote)
    If h Is Nothing Then
        Console.WriteLine("X(0) is NOT an HeldNote")
    Else
        Console.WriteLine(h.Author)
    End If

    h = TryCast(X(1), HeldNote)
    If h Is Nothing Then
        Console.WriteLine("X(1) is NOT an HeldNote")
    Else
        Console.WriteLine(h.Author)
    End If
End Sub

Public Class Note
    Public Note As String
End Class

Public Class HeldNote 
    Inherits Note
    Public Author As String
End Class 

In thisway you still have your HeldNote without using an array of Object that is something to avoid because you loose all the strong typing allowed by a specific Note array (In an Object array you could store anything. A Button or a String makes no difference) 这样,您仍然可以在不使用Object数组的情况下拥有HeldNote,这是可以避免的,因为您松开了特定Note数组允许的所有强类型输入(在Object数组中,您可以存储任何内容。Button或String没什么区别)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM