有条件成员的结构 [英] Structures with conditional members

查看:61
本文介绍了有条件成员的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VB中构建结构,以允许我存储和解析以ASCII编码的HEX格式接收的基于IP / UDP的消息。

I'm trying to build a structure in VB to allow me to both store and parse an IP/UDP based message that I'm receiving as ASCII encoded HEX.

技巧是,在经过IP标头和UDP标头之后,之后的数据是可变结构类型。我的意思是说,存在一个基本的初始消息结构,该结构在特定字段中具有数据...其中三分之二是可选的...但是,实际数据(传输的真实内容)在15种不同的消息之一中

The trick is that after I get past the IP header and the UDP header the data that follows after is of a variable structure type. By that I mean that there is a basic initial message structure that has data in specific fields... two thirds of which are optional... but then the actual data, the real meat of the transmission, is in one of 15 different message structures.

那么有没有一种方法可以根据我放入的数据有条件地定义结构的成员之一?

So is there a way to conditionally define one of the members of the structure based on the data I'm putting in it?

我的计划是创建一个New()函数,您只需将消息字符串传递给它,它将解析出所有数据并填充结构的成员如所须。但是,如何使结构成员之一的声明类型依赖于将在New()函数中解析的数据?还是没有办法真正做到这一点?

My plan was to create a New() function that you could simply pass the message string to and it would parse out all of the data and populate the members of the structures as needed. But how can I make the declaration type of one of the structure members dependent on the data that will be parsed out in the New() function? Or is there no way to really do this?

基本上是这样的东西(尽管很粗略):

Basically something (albeit crudely) like this:

Private Structure DevMsg
    ReadOnly IPHeader As IP_HDR
    ReadOnly UDPHeader As UDP_HDR
    ReadOnly MsgType As DevMsgType
    Select Case MsgType
        Case Msg0
            ReadOnly Data as MsgType0
        Case Msg1
            ReadOnly Data as MsgType1
        ...
    End Select

    Public Sub New(ByVal msg as String)

        ... String parser ...

        MsgType = blah

        ... More parsing ...

        Data.property0 = blah2
        Data.property1 = blah3
        ...

    End Sub
End Structure


推荐答案

我认为您应该做的是为每种消息类型设置基类/抽象类和具体类

I think what you should be doing is having a base/abstract class and concrete classes for each type of message that inherit from the base.

某些东西像这样:

Public MustInherit Class MessageBase
    Public ReadOnly Header As String
    Public ReadOnly MsgType As Integer
    Public Function CreateMessage(message As String) As MessageBase
        If message = "type1" Then Return New Type1Message
        If message = "type1" Then Return New Type2Message
        'etc.
    End Function
End Class

Public Class Type1Message
    Inherits MessageBase
End Class

Public Class Type2Message
    Inherits MessageBase
End Class

这样,您可以共享基类中的常用属性并添加

This way you can share common properties in the base class and add additional properties for each message where they differ.

请注意,您需要在每个具体类中使用一个构造函数来根据其自己的规则来解析数据,但我将其留给了清晰度

note you need a constructor in each concrete class to parse the data according to its own rules, but I have left this out for clarity

这篇关于有条件成员的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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