VB6ã§ï¼ãªãã¡ãã£ã¦ï¼æ§é åä¾å¤å¦ç
æè¿ã¯ãã£ã¨VB6*1ã§ä»äºããã¦ããã®ã§ãã§ããªãäºãå¤ãã¦ã¤ã©ã¤ã©ãã¦ããã
ã ããã¨è¨ã£ã¦ãããå æ¸ãªç©ãä½ãããã«ã¯ãããªãã®ã§ãVB6ã§ãã§ããéãã®äºãããçºã«è²ã ã¨ãã¬ã¼ã ã¯ã¼ã¯çãªãã®ãä½ãå§ãããããã使ãã°ä¸å®ã®å質ãä¿ã¦ãã£ã¦ç¨åº¦ã®ãã¤ã
ã¾ãã¯VB6ã®ããã¼ãã¨ã©ã¼å¦çã®æ©è½ãããå°ãã¾ãã«ããæ©è½ãä½ã£ãã
ããããäºã¯
- ä¸è²«ããã¨ã©ã¼å¦ç
- å¼ã³åºãå±¥æ´ãåç §ã§ããï¼ã¹ã¿ãã¯ãã¬ã¼ã¹ï¼
- ã¨ã©ã¼ã«è¿½å æ å ±ãè¼ãã
ããããå®ç¾ããçºã«ãã¾ãä¾å¤ã¯ã©ã¹ããä½ããã¨ã«ããã
ãã®åã«ãã®ã¯ã©ã¹ãå¿ è¦ãªã®ã§ãè¼ãã¨ããåãªãæååãããã¡ã¯ã©ã¹ãã
clsStringBuilder.cls
Option Explicit '#--------------------------------------------------------------------------- '# ã¯ã©ã¹åï¼clsStringBuilder '# æ¦è¦ããï¼æååãããã¡ã¯ã©ã¹ '#--------------------------------------------------------------------------- Private m_strBuffer As String Private Sub Class_Initialize() m_strBuffer = "" End Sub '#--------------------------------------------------------------------------- '# é¢æ°åï¼Append '# æ©è½ ï¼ãããã¡ã«æååã追å ãã¾ãã '# '# å¼æ° ï¼strValueï¼ æåå '# ï¼varParam0ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam1ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam2ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam3ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam4ï¼ ãã©ã¡ã¼ã¿ï¼ '# '# æ»ãå¤ï¼èªåèªèº« '# åèãï¼æååã«ã{çªå·}ãã¨ããå½¢å¼ã®æååãå ¥ããã¨ããã®é¨åã '# å¼æ°ã§æå®ããçªå·ã®ãã©ã¡ã¼ã¿ã¨ç½®ãæããã¾ãã '# ä¾ï¼ objBuffer.Append("my name is {0}.", "hoge") '# ' ãmy name is hoge.ãã¨ããæååã追å ãããã '#--------------------------------------------------------------------------- Public Function Append( _ Optional ByRef strValue As String = "", _ Optional ByRef varParam0 As Variant, _ Optional ByRef varParam1 As Variant, _ Optional ByRef varParam2 As Variant, _ Optional ByRef varParam3 As Variant, _ Optional ByRef varParam4 As Variant) As clsStringBuilder Dim i As Integer Dim varParams(4) As Variant If Not IsMissing(varParam0) Then varParams(0) = varParam0 If Not IsMissing(varParam1) Then varParams(1) = varParam1 If Not IsMissing(varParam2) Then varParams(2) = varParam2 If Not IsMissing(varParam3) Then varParams(3) = varParam3 If Not IsMissing(varParam4) Then varParams(4) = varParam4 For i = LBound(varParams) To UBound(varParams) strValue = Replace(strValue, "{" & CStr(i) & "}", varParams(i)) Next m_strBuffer = m_strBuffer & strValue Set Append = Me End Function '#--------------------------------------------------------------------------- '# é¢æ°åï¼AppendLine '# æ©è½ ï¼ãããã¡ã«æååã追å ãã¦ãæ¹è¡ãæ¿å ¥ãã¾ãã '# '# å¼æ° ï¼strValueï¼ æåå '# ï¼varParam0ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam1ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam2ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam3ï¼ ãã©ã¡ã¼ã¿ï¼ '# ï¼varParam4ï¼ ãã©ã¡ã¼ã¿ï¼ '# '# æ»ãå¤ï¼èªåèªèº« '#--------------------------------------------------------------------------- Public Function AppendLine( _ Optional ByRef strValue As String = "", _ Optional ByRef varParam0 As Variant, _ Optional ByRef varParam1 As Variant, _ Optional ByRef varParam2 As Variant, _ Optional ByRef varParam3 As Variant, _ Optional ByRef varParam4 As Variant) As clsStringBuilder Set AppendLine = Append( _ strValue & vbCrLf, _ varParam0, varParam1, varParam2, varParam3, varParam4 _ ) End Function '#--------------------------------------------------------------------------- '# é¢æ°åï¼Clear '# æ©è½ ï¼ãããã¡ã«ããæååãã¯ãªã¢ãã¾ãã '# '#--------------------------------------------------------------------------- Public Sub Clear() m_strBuffer = "" End Sub '#--------------------------------------------------------------------------- '# é¢æ°åï¼ToString '# æ©è½ ï¼ç¾å¨ã®ãããã¡ã®æååãåå¾ãã¾ãã '# '# æ»ãå¤ï¼æåå '#--------------------------------------------------------------------------- Public Function ToString() As String ToString = m_strBuffer End Function '#--------------------------------------------------------------------------- '# ããããã£åï¼Length '# æ©è½ ï¼ç¾å¨ã®ãããã¡ã®æååé·ããåå¾ãã¾ãã '# '# æ»ãå¤ï¼æååã®é·ã '#--------------------------------------------------------------------------- Public Property Get Length() As Long Length = Len(m_strBuffer) End Property
å®è£
ã§ãä¾å¤ã¯ã©ã¹ã以ä¸
clsException.cls
Option Explicit '#--------------------------------------------------------------------------- '# ã¯ã©ã¹åï¼clsException '# æ¦è¦ããï¼ã¦ã¼ã¶å®ç¾©ã®ã¨ã©ã¼ã®æ å ±ãæ ¼ç´ããã¯ã©ã¹ '#--------------------------------------------------------------------------- Private m_lngNumber As Long Private m_strSource As String Private m_strDescription As String Private m_objInnerException As clsException '#--------------------------------------------------------------------------- '# ããããã£åï¼Number '# æ©è½ ï¼ã¨ã©ã¼çªå·ãåå¾ãè¨å®ãã¾ãã '# '# æ»ãå¤ï¼ã¨ã©ã¼çªå· '#--------------------------------------------------------------------------- Public Property Let Number(ByRef lngNumber As Long) m_lngNumber = lngNumber End Property Public Property Get Number() As Long Number = m_lngNumber End Property '#--------------------------------------------------------------------------- '# ããããã£åï¼Source '# æ©è½ ï¼ã¨ã©ã¼ã®ã½ã¼ã¹ãåå¾ãè¨å®ãã¾ãã '# '# æ»ãå¤ï¼ã¨ã©ã¼ã®ã½ã¼ã¹ '#--------------------------------------------------------------------------- Public Property Let Source(ByRef strSource As String) m_strSource = strSource End Property Public Property Get Source() As String Source = m_strSource End Property '#--------------------------------------------------------------------------- '# ããããã£åï¼Description '# æ©è½ ï¼ã¨ã©ã¼ã®è©³ç´°ãåå¾ãè¨å®ãã¾ãã '# '# æ»ãå¤ï¼ã¨ã©ã¼ã®è©³ç´° '#--------------------------------------------------------------------------- Public Property Let Description(ByRef strDescription As String) m_strDescription = strDescription End Property Public Property Get Description() As String Description = m_strDescription End Property '#--------------------------------------------------------------------------- '# ããããã£åï¼InnerException '# æ©è½ ï¼å é¨ã¨ã©ã¼ãªãã¸ã§ã¯ããåå¾ãè¨å®ãã¾ãã '# '# æ»ãå¤ï¼ã¨ã©ã¼ãªãã¸ã§ã¯ã '#--------------------------------------------------------------------------- Public Property Set InnerException(ByRef objInnerException As clsException) Set m_objInnerException = objInnerException End Property Public Property Get InnerException() As clsException Set InnerException = m_objInnerException End Property Private Sub Class_Initialize() m_lngNumber = 0 m_strSource = "" m_strDescription = "" End Sub '#--------------------------------------------------------------------------- '# é¢æ°åï¼ToString '# æ©è½ ï¼ç¾å¨ã®ãªãã¸ã§ã¯ããæååã«å¤æãã¾ãã '# '# æ»ãå¤ï¼æåå '#--------------------------------------------------------------------------- Public Function ToString() As String Dim objExp As clsException Set objExp = Me With New clsStringBuilder .AppendLine "ã¨ã©ã¼çªå·ï¼[{0}]", Me.Number .AppendLine .AppendLine Me.Description Do Until objExp Is Nothing .AppendLine " at {0}", objExp.Source Set objExp = objExp.InnerException Loop ToString = .ToString End With End Function
ãã¨ã¯ãã®ã¯ã©ã¹ã使ã£ã¦è²ã ã¨ã¨ã©ã¼ãçºçãããããã®ã¢ã¸ã¥ã¼ã«ãå®ç¾©ããã
modException.bas
Option Explicit ' ä¸æ£ãªæä½ Public Const InvalidOperation = 1000 ' ä¸æ£ãªå¼æ° Public Const InvalidArgument = 1001 ' ã¤ã³ããã¯ã¹ãç¯å²å¤ Public Const IndexOutOfRange = 1002 ' æªå®è£ Public Const NotImplementation = 1003 Private m_objLastException As clsException '#--------------------------------------------------------------------------- '# é¢æ°åï¼modException_Throw '# æ©è½ ï¼æå®ããã¨ã©ã¼å 容ã§ä¾å¤ãã¹ãã¼ãã¾ãã '# '# å¼æ° ï¼lngNumberï¼ ã¨ã©ã¼çªå· '# ï¼strSourceï¼ ã¨ã©ã¼ã½ã¼ã¹ '# ï¼strDescriptionï¼ ã¨ã©ã¼ã®è©³ç´° '#--------------------------------------------------------------------------- Public Sub modException_Throw( _ ByRef lngNumber As Long, _ ByRef strSource As String, _ ByRef strDescription As String) Dim objException As New clsException With objException .Number = lngNumber .Source = strSource .Description = strDescription If Not m_objLastException Is Nothing Then Set objException.InnerException = m_objLastException End If End With Set m_objLastException = objException Call Err.Raise(lngNumber, strSource, strDescription) End Sub '#--------------------------------------------------------------------------- '# é¢æ°åï¼modException_Throw '# æ©è½ ï¼ç¾å¨ã®ã¨ã©ã¼ãåã¹ãã¼ãã¾ãã '# '# å¼æ° ï¼strSourceï¼ ã½ã¼ã¹ '#--------------------------------------------------------------------------- Public Sub modException_Rethrow( _ Optional ByRef strSource As Variant) If Err.Number <> 0 Then Call modException_Throw(Err.Number, _ IIf(IsMissing(strSource), Err.Source, strSource), Err.Description ) End If End Sub '#--------------------------------------------------------------------------- '# é¢æ°åï¼modException_GetLastException '# æ©è½ ï¼æå¾ã«ã¹ãã¼ãããã¨ã©ã¼ãåå¾ãã¾ãã '# '# æ»ãå¤ï¼ã¨ã©ã¼å 容 '#--------------------------------------------------------------------------- Public Function modException_GetLastException() As clsException If Not m_objLastException Is Nothing Then Set modException_GetLastException = m_objLastException ' æ¨æºã®ã¨ã©ã¼ã§ä»£æ¿ãã¦ãã ElseIf Err.Number <> 0 Then Dim objException As New clsException With objException .Number = Err.Number .Source = Err.Source .Description = Err.Description End With Set modException_GetLastException = objException End If End Function '#--------------------------------------------------------------------------- '# é¢æ°åï¼modException_ClearLastException '# æ©è½ ï¼æå¾ã«ã¹ãã¼ãããã¨ã©ã¼ãæ¶å»ãã¾ãã '# '#--------------------------------------------------------------------------- Public Sub modException_ClearLastException() Set m_objLastException = Nothing Call Err.Clear End Sub '#--------------------------------------------------------------------------- '# é¢æ°åï¼modException_ExistsException '# æ©è½ ï¼ã¨ã©ã¼ãã¹ãã¼ããããã©ããã調ã¹ã¾ãã '# '# æ»ãå¤ï¼ã¹ãã¼ããã¦ããã°True '#--------------------------------------------------------------------------- Public Function modException_ExistsException() As Boolean modException_ExsitsException = Not modException_GetLastException() Is Nothing End Function
使ãæ¹
使ãæ¹ã¨ãã¦ã¯ãããªæã
ã¨ã©ã¼ãçºçããã¦ãã£ãã
Sub Main() ' æåã«å¿ ãå¼ã³åºã Call modException_ClearLastException() On Error Goto EXCEPT: Call Execute Exit Sub EXCEPT: With modException_GetLastException ' ã¹ã¿ãã¯ãã¬ã¼ã¹ãåºå Debug.Print .ToString MsgBox .Description End With End Sub Sub Execute() Call modException_Throw(666, "Execute", "ã¨ã©ã¼çºç") End Sub
ã©ã³ã¿ã¤ã ã¨ã©ã¼ããã£ãããã¦ãåã¹ãã¼
Sub Main() ' æåã«å¿ ãå¼ã³åºã Call modException_ClearLastException() On Error Goto EXCEPT: Call Execute1 Exit Sub EXCEPT: With modException_GetLastException ' ã¹ã¿ãã¯ãã¬ã¼ã¹ãåºå Debug.Print .ToString MsgBox .Description End With End Sub Sub Execute1() On Error Goto EXCEPT: Call Execute2 Exit Sub EXCEPT: ' ã¹ã«ã¼ Call modException_Rethrow("Execute1") End Sub Sub Execute2() On Error Goto EXCEPT: Call Execute3 Exit Sub EXCEPT: ' ã¹ã«ã¼ Call modException_Rethrow("Execute2") End Sub Sub Execute3() On Error Goto EXCEPT: ' ãªããã¨ã©ã¼ãçºçããå¦ç Exit Sub EXCEPT: ' åã¹ãã¼ Call modException_Rethrow("Execute3") End Sub
æåã§ã¹ã¿ãã¯ãã¬ã¼ã¹ãç©ã¿ä¸ãã¦ãããªããã°ãããªãã®ã§ãã³ã¼ãéã¯å¢ãã¡ãããã©å¿
ãå½¹ã«ç«ã¤æãæ¥ãã®ã§ããã£ã¨ãããã
ããããäºã¯ãã£ã¦ããªããã©ãããã¸ã§ã¯ãã§ã¨ã©ã¼å¦çãå
±éåã§ããã®ã§ããããªãã«å¹æã¯ããã¯ãã