有没有一种方法来“别名”一个类属性? [英] Is there a method to 'alias' a class property?

查看:61
本文介绍了有没有一种方法来“别名”一个类属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VB.Net 2010中编写一个泛型类,用于多个程序,并希望该类使用通用属性名称,以便我可以简单地在文件之间复制类并保持相同。我遇到的问题是,如果每个不同的实例化程序能够''别名''属性名称,以便intellisense将使用别名但是我不必重写所有属性名称,那将会更有用。类。诸如



  Class  BitFlags 

公共 属性 flag0 作为 布尔
获取
返回 GetBit( 0
结束 获取
设置 ByVal As 布尔
如果值= True 然后 SetBit( 0
如果值= False 然后 ResetBit( 0
结束 设置
结束 属性


结束





以及实例化程序可以做这样的事情



  dim  myflags =  new  BitFlags 
' 一些别名函数
别名 myAlias = myflags.flag0

myAlias = True
myAlias = 错误





谢谢!

解决方案

首先,答案是:没有没有这样的事情。


您可以获得类似的效果,但它不能是别名或行为与别名完全相同。



我不想以一个非常简单的理由讨论你想要的理由。我可以看到:我可以简单地在文件之间复制类并保持相同。从那一刻开始,你就不会进行任何编程,你会遭受如此大的滥用,任何编程活动根本就没有任何意义;这将是一个完全错误的方式。



我很乐意在这里帮助你,但我不知道现在怎么样,因为我看不到哪里是你的困惑。无论如何,我不知道该属性的别名如何有用,它可以提供什么样的抽象。我不认为它可以。



在你展示的例子中,我甚至看不到通用的需要。有点儿。您可以通过某个索引(位移)设置或清除(清除,而不是重置)。如果你想知道怎么做,我可以告诉你。也许你需要解释你的最终目的,以获得进一步的帮助。







另外两个注释:



你的代码看起来像类 System.Collections.BitArray http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx [ ^ ]。



您可能需要查看本课程以获得一些基本想法。如果你想知道它是如何工作的,我可以解释一下。这是一件非常简单的事情。



此外,没有类属性这样的东西。属性只能是 struct 的成员。在某些语言中,类属性是.NET中通常称为静态属性的更精确的术语,所以这个术语至少令人困惑。







在OP的评论澄清之后:



请参阅我在回复中的评论,关于枚举类型的使用。我有一篇关于相关主题的详细文章可以帮到你:​​枚举类型做不列举!解决.NET和语言限制 [ ^ ]。



-SA


我有工业自动化的背景,我理解你要做的事情。我建议你编写一个在控制器中实现字长的基类。我建议一些能支持16位和32位字长的东西(除非你知道你将永远使用32位系统)。您可以在类构造函数中指定默认值所需的长度。



在内部,我会将这些位保留在数组中。它将使访问它们变得容易。然后,您可以编写访问各个位(bit0,bit1等)的类属性,以访问整个数组(位),以及其他以方便的块提供位的类...如低位和高位的LWORD或HWORD 。您可能还提供MSB(最高有效位)和LSB(最低有效位)属性,这些属性将返回等于MSB或LSB的int值。这对于需要优先级概念的HMI上的错误或状态显示通常很方便。



一旦创建了实现的类在PLC中使用一个单词或双字,您可以简单地创建派生自第一个类并添加细节的类......使用虚拟方法,您可以指定一个知道如何偷看或戳的方法''PLC中的内存位置。实际上,基类不了解你将如何做到,但派生类将具有那些知道如何与PLC通信或通过你正在使用的任何方法对I / O进行通信的功能的具体实例(EthernetIP,DeviceNet,等等。



如果你需要一些额外的指示,请直接给我留言。我想听听你的项目。当应用于自动化项目时,OOP原则可以非常强大,因为系统构建方式的继承冗余以及OOP可以解决这种冗余的方式。如果你正确地构建这些基础对象,你的系统应该非常灵活。


放弃并将使用枚举列表初始化为2 ^ n幂并执行按位运算并设置我的单词/整数值到我将使用的每个单词/整数的枚举值。它不如单独使用这些位好,但它会起作用。感谢大家的建议和指示,我很感激他们。



< flagsattribute()> _ 
公共 枚举 Machine_State_Enum_Flags As ULong
InitializationNotComplete = 0
ControllerInitializing = 1
MachineError = 2
ControllerError = 4
DriveError = 8
MotionError = 16
ExternalError = 32
EStop = 64
空闲= 128
暂停= 256
InCycle = 512
Homing = 1024
Moving = 2048
结束 枚举
#End Region

私有 Sub machine_state_test()

Dim machine_state As Machine_State_Enum_Flags = 0
' 使用按位运算,此语句添加Machine_State_Enum_Flags.InitializationNotComplete $ b $的值b ' 通过设置所需位到机器状态
machine_state = machine_state Machine_State_Enum_Flags.Moving
Debug.Print( 机器状态为:& machine_state& & machine_state.ToString)
machine_state = machine_state Machine_State_Enum_Flags.InCycle
Debug.Print( 机器状态为:& machine_state.ToString)

' 检查并查看是否在机器状态上设置了特定标志,
如果(machine_state < span class =code-keyword>和 Machine_State_Enum_Flags.Moving)> 0 然后
Debug.Print( 移动标志设置为
Debug.Print( 机器状态为:& machine_state.ToString)
结束 如果

' 以清除机器状态
machine_state = machine_state Xor Machine_State_Enum_Flags.Moving
Debug.Print( 机器状态是:& machine_state.ToString)

结束


I am writing a generic class in VB.Net 2010 to use in multiple programs and would like the class to utilize generic property names so that I can simply copy the class between files and keep it the same. The problem I have is that it would be much more useful if each different instantiating program were able to ''alias'' the property names such that intellisense would use the alias but I wouldn''t have to rewrite all the properties names in the class. Something such as

Class BitFlags

    Public Property flag0 As Boolean
        Get
            Return GetBit(0)
        End Get
        Set(ByVal Value As Boolean)
            If Value = True Then SetBit(0)
            If Value = False Then ResetBit(0)
        End Set
    End Property


End Class



and where the instantiating program could do something like this

dim myflags = new BitFlags
'some alias function
Alias myAlias = myflags.flag0

myAlias = True
myAlias = False



Thank you!

解决方案

First of all, the answer: no there is no such thing.

You can get some similar effect, but it cannot be the alias or behave exactly like the alias.

I don''t want to discuss the rationale behind what you want by a very simple reason. I can see this: "I can simply copy the class between files and keep it the same". Since that moment, you are not going to do any programming, you are going into such a huge abuse that any programming activity would not make any sense at all; this would be a totally wrong way.

I would be happy to help you here, but I don''t know how at this moment, as I cannot see where is your confusion. Anyway, I don''t see how the alias of the property could be useful, what kind of abstraction could it provide. I don''t think it could.

In the example you show, I don''t even see a need in generic. A bit is a bit. You set it or clear (clear, not "reset"), by some index (bit shift). If you want to know how to do it, I can show you. Perhaps you need to explain your ultimate purpose, to get further help.

[EDIT #1]

Two more notes:

Your code looks like the class System.Collections.BitArray: http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx[^].

You may need to look at this class to get some basic ideas. If you want to know how it works, I can explain you. This is a pretty simple thing.

Also, there is no such thing as "class property". A property can only be a member of a class or a struct. In some languages, "class property" is a more precise term for what is usually called "static property" in .NET, so this term is at least confusing.

[EDIT #2]

After the clarification in OP''s comment below:

Please see my comment in reply, about the use of enumeration types. I have a detailed article on related topic which should help you: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

—SA


I have a background in Industrial Automation and I understand what you are trying to do. I suggest you write a base class that implements the word-length in the controller. I would suggest something that would support both 16 and 32 bit word lengths (unless you know you will always be working against a 32-bit system). You could take the desired length in the class constructor specifying a default value.

Internally, I would keep the bits in an array. It will make accessing them easy. You can then write class properties that access individual bits (bit0, bit1, etc) another that accesses the whole array (bits), and other ones that provide the bits in convenient chunks... like LWORD or HWORD for the low and high halves. You could probably also provide a MSB (most significant bit) and LSB (least significant bit) properties that would return int values equal to the MSB or LSB. This is often handy for error or status displays on HMI''s where you need the concept of a "priority" level.

Once you have created the class that implements one word or double-word in the PLC, you can simply create classes which derive from the first class and add specifics... And with virtual methods, you can specify a method that knows how to ''peek'' or ''poke'' the memory location in the PLC. In reality, the base class knows nothing of how you would do that but the derived classes would have concrete instances of those functions that know how to talk to the PLC or to field I/O over whatever method you are using (EthernetIP, DeviceNet, etc).

Message me directly if you need some additional direction on this. I''d like to hear about your project. OOP principles can be very powerful when applied to automation projects because of the inherit redundancy in the way the systems are built and the manner in which OOP can address that redundency. If you architect these base objects correctly, your system should be very flexible.


Gave up and will be utilizing an enumerated list initialized with 2^n powers and performing bitwise operations and setting my word/integer value to the enumerated values for each word/integer that I will be working with. Its not as nice as working with the bits individually but it will work. Thanks everyone for suggestions and pointers, I appreciate them all.

<flagsattribute()> _
    Public Enum Machine_State_Enum_Flags As ULong
        InitializationNotComplete = 0
        ControllerInitializing = 1
        MachineError = 2
        ControllerError = 4
        DriveError = 8
        MotionError = 16
        ExternalError = 32
        EStop = 64
        Idle = 128
        Pause = 256
        InCycle = 512
        Homing = 1024
        Moving = 2048
    End Enum
#End Region

       Private Sub machine_state_test()

        Dim machine_state As Machine_State_Enum_Flags = 0
        'using bitwise operations, this statement adds the value of Machine_State_Enum_Flags.InitializationNotComplete
        ' to the machine state by setting the desired bit
        machine_state = machine_state Or Machine_State_Enum_Flags.Moving
        Debug.Print("machine state is: " & machine_state & ", " & machine_state.ToString)
        machine_state = machine_state Or Machine_State_Enum_Flags.InCycle
        Debug.Print("machine state is: " & machine_state.ToString)

        'to check and see if a specific flag is set on machine state,
        If (machine_state And Machine_State_Enum_Flags.Moving) > 0 Then
            Debug.Print("moving flag is set")
            Debug.Print("machine state is: " & machine_state.ToString)
        End If

        'to clear a bit on the machine state
        machine_state = machine_state Xor Machine_State_Enum_Flags.Moving
        Debug.Print("machine state is: " & machine_state.ToString)

    End Sub


这篇关于有没有一种方法来“别名”一个类属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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