Luciano Evaristo Guerche

A brazilian geek interested in .NET technologies

About Me

May 2006 - Posts

String.Format("My name is {0}", "Luciano Evaristo Guerche") (VB6)

A feature I loved in .NET (C# and VB.NET) is String.Format method. So I thought how I could have the same feature on VB6 and I came up with the following code.

Public Function StringDotFormat(ByVal strFormat As String, ByVal ParamArray aryPlaceHolders()) As String

Dim intPlaceHolderIndex As Integer

Dim strOutput As String

 

strOutput = strFormat

 

For intPlaceHolderIndex = LBound(aryPlaceHolders) To UBound(aryPlaceHolders)

strOutput = Replace(strOutput, "{" & intPlaceHolderIndex & "}", aryPlaceHolders(intPlaceHolderIndex))

Next

 

StringDotFormat = strOutput

End Function

 

Then used it the following way

Call StringDotFormat("My name is {0}", "Luciano Evaristo Guerche")

 

What do you think about it? Drop me a comment and let me know your opinion.

Public Function IsValidCPF(ByVal strCPF As String) As Boolean

Option Explicit

 

Public Function IsValidCPF(ByVal strCPF As String) As Boolean

Dim intDigito As Integer

Dim intIndex As Integer

Dim intModulo As Integer

Dim intSoma1 As Integer

Dim intSoma2 As Integer

Dim intDigitoVerificador1 As Integer

Dim intDigitoVerificador2 As Integer

 

intSoma1 = 0

intSoma2 = 0

 

For intIndex = 1 To 9

intDigito = CInt(Mid$(strCPF, intIndex, 1))

intSoma1 = intSoma1 + ((11 - intIndex) * intDigito)

intSoma2 = intSoma2 + ((12 - intIndex) * intDigito)

Next

 

intModulo = intSoma1 Mod 11

If intModulo < 2 Then

intDigitoVerificador1 = 0

Else

intDigitoVerificador1 = 11 - intModulo

End If

 

intSoma2 = intSoma2 + (2 * intDigitoVerificador1)

 

intModulo = intSoma2 Mod 11

If intModulo < 2 Then

intDigitoVerificador2 = 0

Else

intDigitoVerificador2 = 11 - intModulo

End If

 

IsValidCPF = ( _

(Mid$(strCPF, 10, 1) = CStr(intDigitoVerificador1)) And _

(Mid$(strCPF, 11, 1) = CStr(intDigitoVerificador2)) _

)

End Function

 

CREATE FUNCTION dbo.IsValidCPF

IF EXISTS(

SELECT *

FROM dbo.sysobjects

WHERE id = object_id(N'[dbo].[IsValidCPF]') AND

xtype in (N'FN', N'IF', N'TF')

)

BEGIN

DROP FUNCTION [dbo].[IsValidCPF]

END

GO

CREATE FUNCTION dbo.IsValidCPF

(

@CPF varchar(11)

)

RETURNS bit

AS

BEGIN

DECLARE @Digito int

DECLARE @Index int

DECLARE @Modulo int

DECLARE @Soma1 int

DECLARE @Soma2 int

DECLARE @DigitoVerificador1 int

DECLARE @DigitoVerificador2 int

 

SET @Index = 1

SET @Soma1 = 0

SET @Soma2 = 0

 

WHILE @Index <= 9

BEGIN

SET @Digito = CAST(SUBSTRING(@CPF, @Index, 1) AS int)

SET @Soma1 = @Soma1 + ((11 - @Index) * @Digito)

SET @Soma2 = @Soma2 + ((12 - @Index) * @Digito)

SET @Index = @Index + 1

END

 

SET @Modulo = @Soma1 % 11

IF @Modulo < 2

BEGIN

SET @DigitoVerificador1 = 0

END

ELSE

BEGIN

SET @DigitoVerificador1 = 11 - @Modulo

END

 

SET @Soma2 = @Soma2 + (2 * @DigitoVerificador1)

SET @Modulo = @Soma2 % 11

 

IF @Modulo < 2

BEGIN

SET @DigitoVerificador2 = 0

END

ELSE

BEGIN

SET @DigitoVerificador2 = 11 - @Modulo

END

 

IF SUBSTRING(@CPF, 10, 1) = CAST(@DigitoVerificador1 AS varchar(1)) AND

SUBSTRING(@CPF, 11, 1) = CAST(@DigitoVerificador2 AS varchar(1))

BEGIN

RETURN 1

END

 

RETURN 0

END

GO

 

More Posts