Rinaldo Ferreira

Hi.

I am porting the code for a rule written in C# to VB.NET. That rule verifies if my variables are using the correct prefixes. The code compiles and run, but the test is executed even when it has not to do. That's the code:

Code Snippet

Friend Class ObjectPrefixNamingRule

Inherits BaseRule

Public Sub New()

MyBase.New("ObjectPrefixNamingRule")

End Sub

Public Overrides ReadOnly Property TargetVisibility() As TargetVisibilities

Get

Return MyBase.TargetVisibility

End Get

End Property

Public Overrides Function Check(ByVal membro As Microsoft.Cci.Member) As ProblemCollection

Dim mtMetodo As Method = TryCast(membro, Method)

Dim problem As Boolean = False

If Not (mtMetodo Is Nothing) Then

Dim instructions As InstructionList = mtMetodo.Instructions

If instructions.Length = 0 Then

Return Nothing

End If

Dim lclLista As LocalList = CType(instructions(0).Value, LocalList)

If lclLista Is Nothing Then

Return Nothing

End If

Dim i As Integer = 0

Dim lElemento As Local

Dim intContagem As Integer = lclLista.Length

While i < intContagem

lElemento = lclLista(i)

If Not RuleUtilities.IsCompilerGenerated(lElemento) Then

problem = ContemPrefixoCorreto(lclLista(i))

End If

If problem Then

Problems.Add(New Problem(GetResolution(lElemento.Name.Name)))

End If

i += 1

End While

Else

Dim fldAtributo As Field = TryCast(membro, Field)

If fldAtributo Is Nothing Then

Return Nothing

Else

problem = ContemPrefixoCorreto(fldAtributo)

If problem Then

Problems.Add(New Problem(GetResolution(fldAtributo.Name.ToString)))

End If

End If

End If

Return Problems

End Function

End Class

Friend Class IntegerPrefixNamingRule

Inherits BaseRule

Public Sub New()

MyBase.New("IntegerPrefixNamingRule")

End Sub

Public Overrides ReadOnly Property TargetVisibility() As TargetVisibilities

Get

Return MyBase.TargetVisibility

End Get

End Property

Public Overrides Function Check(ByVal membro As Microsoft.Cci.Member) As ProblemCollection

Dim mtMetodo As Method = TryCast(membro, Method)

Dim problem As Boolean = False

If Not (mtMetodo Is Nothing) Then

Dim instructions As InstructionList = mtMetodo.Instructions

If instructions.Length = 0 Then

Return Nothing

End If

Dim lclLista As LocalList = CType(instructions(0).Value, LocalList)

If lclLista Is Nothing Then

Return Nothing

End If

Dim i As Integer = 0

Dim lElemento As Local

Dim intContagem As Integer = lclLista.Length

While i < intContagem

lElemento = lclLista(i)

If Not RuleUtilities.IsCompilerGenerated(lElemento) Then

problem = ContemPrefixoCorreto(lclLista(i))

End If

If problem Then

Problems.Add(New Problem(GetResolution(lElemento.Name.Name)))

End If

i += 1

End While

Else

Dim fldAtributo As Field = TryCast(membro, Field)

If fldAtributo Is Nothing Then

Return Nothing

Else

problem = ContemPrefixoCorreto(fldAtributo)

If problem Then

Problems.Add(New Problem(GetResolution(fldAtributo.Name.ToString)))

End If

End If

End If

Return Problems

End Function

End Class

When I run it, if the analysis find an Object variable in my code, it will display two messages, one for the Object rule and the other for the Integer rule. Shouldn't it display only the Object rule message Maybe I have something wrong in my code.

Regards,

Rinaldo



Re: Visual Studio Code Analysis and Code Metrics Porting code from C# to VB.NET

David M. Kean - MSFT

Rinaldo,

You're not actually ever checking the type of the local, so both rules will fire on every single variable.

Simply add a check that checks the type of the local (you also want to check the type of the field as well):




If lElemento.Type = FrameworkTypes.Int32 Then Return Nothing


Regards

David






Re: Visual Studio Code Analysis and Code Metrics Porting code from C# to VB.NET

Rinaldo Ferreira

Hi David.

I changed the code to check for the type of the local, but the problem is still there. That's the new code:

Code Snippet

Friend Class ObjectPrefixNamingRule

Inherits BaseRule

Public Sub New()

MyBase.New("ObjectPrefixNamingRule")

End Sub

Public Overrides ReadOnly Property TargetVisibility() As TargetVisibilities

Get

Return MyBase.TargetVisibility

End Get

End Property

Public Overrides Function Check(ByVal membro As Microsoft.Cci.Member) As ProblemCollection

Dim mtMetodo As Method = TryCast(membro, Method)

Dim blnProblema As Boolean = False

Dim i As Integer

If Not (mtMetodo Is Nothing) Then

If mtMetodo.Instructions.Length = 0 Then

Return Nothing

End If

Dim lclLista As LocalList = CType(mtMetodo.Instructions(0).Value, LocalList)

If lclLista Is Nothing Then

Return Nothing

End If

While i < lclLista.Length

Dim lElemento As Local = lclLista(i)

If Not RuleUtilities.IsCompilerGenerated(lElemento) Then

Dim strNome As String = lElemento.Name.Name

If lElemento.Type.Equals(SystemTypes.Object) Then

blnProblema = (strNome.Length < 3 Or Not strNome.Substring(0, 3).Equals("obj"))

End If

If blnProblema Then

Problems.Add(New Problem(GetResolution(strNome)))

End If

End If

i += 1

End While

Else

Dim fldAtributo As Field = TryCast(membro, Field)

If fldAtributo Is Nothing Then

Return Nothing

Else

If fldAtributo.Type.Equals(SystemTypes.Object) Then

blnProblema = (membro.Name.ToString.Length < 3 OrElse Not membro.Name.ToString.Substring(0, 3).Equals("obj"))

End If

If blnProblema Then

Problems.Add(New Problem(GetResolution(membro.Name.Name)))

End If

End If

End If

Return Problems

End Function

End Class

Friend Class IntegerPrefixNamingRule

Inherits BaseRule

Public Sub New()

MyBase.New("IntegerPrefixNamingRule")

End Sub

Public Overrides ReadOnly Property TargetVisibility() As TargetVisibilities

Get

Return MyBase.TargetVisibility

End Get

End Property

Public Overrides Function Check(ByVal membro As Microsoft.Cci.Member) As ProblemCollection

Dim mtMetodo As Method = TryCast(membro, Method)

Dim blnProblema As Boolean = False

Dim i As Integer

If Not (mtMetodo Is Nothing) Then

If mtMetodo.Instructions.Length = 0 Then

Return Nothing

End If

Dim lclLista As LocalList = CType(mtMetodo.Instructions(0).Value, LocalList)

If lclLista Is Nothing Then

Return Nothing

End If

While i < lclLista.Length

Dim lElemento As Local = lclLista(i)

If Not RuleUtilities.IsCompilerGenerated(lElemento) Then

Dim strNome As String = lElemento.Name.Name

If lElemento.Type.Equals(SystemTypes.Int32) Then

blnProblema = (strNome.Length < 3 Or Not strNome.Substring(0, 3).Equals("int"))

End If

If blnProblema Then

Problems.Add(New Problem(GetResolution(strNome)))

End If

End If

i += 1

End While

Else

Dim fldAtributo As Field = TryCast(membro, Field)

If fldAtributo Is Nothing Then

Return Nothing

Else

If fldAtributo.Type.Equals(SystemTypes.Int32) Then

blnProblema = (membro.Name.ToString.Length < 3 OrElse Not membro.Name.ToString.Substring(0, 3).Equals("int"))

End If

If blnProblema Then

Problems.Add(New Problem(GetResolution(membro.Name.Name)))

End If

End If

End If

Return Problems

End Function

End Class

Then, I open a new VS project and declare some variables inside my Form_Load event, without any prefixes:

Code Snippet

Dim testone As Object

Dim testtwo As Integer

I receive 4 warnings instead of the two I was hoping to see. It seems that the rule for Object variables is applied to Integers as well, and vice-versa. Could you help me one more time

Regards,

Rinaldo





Re: Visual Studio Code Analysis and Code Metrics Porting code from C# to VB.NET

David M. Kean - MSFT

Rinaldo,

Have you tried debugging the rule and stepping through the code I've just posted a blog post that explains how to do that very thing over here: http://blogs.msdn.com/fxcop/archive/2007/05/16/faq-how-do-i-debug-a-custom-rule.aspx

This might give you some hints on how to get started.

Regards

David






Re: Visual Studio Code Analysis and Code Metrics Porting code from C# to VB.NET

Rinaldo Ferreira

Hi David.

I tried to folow the steps to debug but I receive an error saying that debugexe is an unknown switch. The help docs say that this switch is not available to VB.NET or C# executables. Is there any way to debug the rules using vsjitdebugger

That's the command line I tried:

>devenv /debugexe fxcopcmd.exe /target:C:\MyDir\bin\Debug\start.exe /rule:C:\MyDir\bin\Debug\myrules.dll /console

And the error says:

Invalid Command Line. Unknown Switch : debugexe.

Could you give me some ideas

Regards,

Rinaldo





Re: Visual Studio Code Analysis and Code Metrics Porting code from C# to VB.NET

Todd King - MSFT

Which version of Visual Studio are you using

You could also try loading your rule in the FxCop standalone (FxCop.exe), setting a break point in your code, attaching Visual Studio to the FxCop.exe process, and then hitting Analyze on the FxCop standalone. You should be able to step into your code that way.

-Todd






Re: Visual Studio Code Analysis and Code Metrics Porting code from C# to VB.NET

Rinaldo Ferreira

Hi Todd!

I managed to reinstall VS.NET, this time selecting C++ also. I don't know if that was the problem, but after that, I could debug the code without problems.

Thanks for your suggestion.

Rinaldo