LoginSignup
5
6

More than 5 years have passed since last update.

VBおじさんあるあるにほげほげするメモ

Posted at

はじめに

いまどきのクラスに値を持たせる実装で、
VB6おじさんが組んだレガシーなコードあるある

なにこれ

Qiitaへの初投稿テスト的な記事

どんなの

要件
 ・HTTPのGETリクエストパラメータをhogeRequestに持たせる。
 ・hogeRequestをクエリパラメータに変換する

hogeRequestの定義はこんな感じ

hogeRequest
    Public Class hogeRequest
        Private value1Field As String
        Private value2Field As String

        Public Sub New()
            Me.value1Field = Nothing
        End Sub

        Public Property value1() As String
            Get
                Return Me.value1Field
            End Get
            Set(value As String)
                Me.value1Field = value
            End Set
        End Property

        Public Property value2() As String
            Get
                Return Me.value2Field
            End Get
            Set(value As String)
                Me.value2Field = value
            End Set
        End Property
    End Class

ここまでは割とふつう

が、肝心のクエリパラメータ作成ロジックがこんなんなってたりする

foo
    Private Sub foo()
        Dim x As New hogeRequest
        x.value1 = "hogehoge"
        x.value2 = Nothing

        Dim s As String = ""

        s &= "value1=" & x.value1

        If Not IsNothing(x.value2) Then
            s &= "&value2=" & x.value2
        End If

        Debug.Print(s)
    End Sub

これだと、プロパティが増える度にメンテしないとダメだし、なんというか、うん。
(職業プログラマおじさんは動けば問題ない。メンテする奴なんか知らんと言う)

なので

それリフレクション使ったらすっきりするよ!

まずこんな感じのコードを書きます。

convertUtil
Imports System.Reflection
    Public Class convertUtil
        ''' <summary>
        ''' 任意の型のプロパティをHTTP GETパラメータに変換
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="model"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function toQueryString(Of T)(model As T) As String
            Dim reqParm As String = ""

            Try
                Dim typ As Type = model.GetType
                Dim members() As MemberInfo = typ.GetMembers(BindingFlags.Public Or
                                                            BindingFlags.Instance Or
                                                            BindingFlags.DeclaredOnly)
                'リクエストパラメータ作成
                For Each m As MemberInfo In members
                    If m.MemberType <> MemberTypes.Property Then Continue For

                    Dim pr As PropertyInfo = typ.GetProperty(m.Name)
                    Dim ores As Object = pr.GetValue(model, Nothing)

                    If IsNothing(ores) Then Continue For

                    reqParm &= IIf(reqParm.Length = 0, "", "&")
                    reqParm &= m.Name & "=" & ores.ToString
                Next m
            Catch ex As Exception
                Throw ex
            End Try
            Return reqParm
        End Function
    End Class

※パラメータ多いときはStringBuilder使ったほうがたぶんしあわせになれる

で、呼び出しはこんな感じ。シンプル。

bar
    Private Sub bar()
        Dim x As New hogeRequest
        x.value1 = "hogehoge"
        x.value2 = Nothing

        Dim s As String = convertUtil.toQueryString(x)
        Debug.Print(s)
    End Sub

これだとクエリパラメータ作成ロジックをいちいち修正しなくていいから楽だよねって話でした。

最後に

この物語はフィクションです。たぶん。

おわれ。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6