473,418 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

FxCop rookie question

Just beginning using FxCop in my asp.net projects...

I get a lot of error messages under the header: AvoidUnusedParameters

for funtions and routines lik:

Sub isUserAuthenticated(ByVal blnLoggedIn As Boolean)
If Not (blnLoggedIn) Then
System.Web.HttpContext.Current.Response.Redirect(p cPageLogin)
End If
End Sub

Where the suggested resolution is:
"The 'this' parameter (or 'Me' in VB) of UtilsAdmin.
isUserAuthenticated(Boolean):Void is never used. Mark
the method as static (or Shared in VB) or use 'this'/Me
in the method body."

I don't get it? How do I use 'this'?
Nov 19 '05 #1
8 1885
FxCop suggests that your method should be static,
as it does not use or depend upon any instance members (non-static state or
methods)
(What is the scope of pcPageLogin?).
How do I use 'this'?

'this' is a keyword (or in this context, as far as FxCop is concerned, an
implicit parameter) that may be used to access
the current instance in your code. Each time you reference instance data or
method FxCop considers that to be "using 'this'".

You should consider making the method static.
I don't think this FxCop rule should be taken literally, in my opinion it
should only be used as a tool for not forgetting to consider whether a
method should be static or not (my experience tells me that it is quite
common just to use objects and instance methods just by habit).
So, there will definetely be cases where you will want to break this
particular FxCop rule (sometimes you HAVE to).
As a rule of thumb, make members static when it doesn't make logical sense
to associate the member with a particular object instance.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.
Nov 19 '05 #2
"hansiman" <ha***@hotmail.com> wrote in message
news:60********************************@4ax.com...
Just beginning using FxCop in my asp.net projects...

I get a lot of error messages under the header: AvoidUnusedParameters

for funtions and routines lik:

Sub isUserAuthenticated(ByVal blnLoggedIn As Boolean)
If Not (blnLoggedIn) Then
System.Web.HttpContext.Current.Response.Redirect(p cPageLogin)
End If
End Sub

Where the suggested resolution is:
"The 'this' parameter (or 'Me' in VB) of UtilsAdmin.
isUserAuthenticated(Boolean):Void is never used. Mark
the method as static (or Shared in VB) or use 'this'/Me
in the method body."

I don't get it? How do I use 'this'?


FxCop is complaining that you do _not_ use Me.

For instance, if your Sub had said

If Not (blnLoggedIn) Then
Response.Redirect(pcPageLogin)
End If

Then it would not have complained, because "Response" is actually
"Me.Response". By Using HttpContext.Current.Response, you're going out of
your way to avoid using Me.Response.

(Why, by the way? Was that line transported from elsewhere?)

At any rate, the other way to meet FxCop's suggestion would be to declare
the sub as

Shared Sub isUserAuthenticated(ByVal blnLoggedIn As Boolean)
John Saunders

P.S. I don't know if FxCop will complain about it, but as long as we're
nitpicking, I'm going to complain about your choice of method name. A method
named Is<something> should be a Function returning Boolean, and should test
to see if the current instance is <something>, or if the supplied parameter
is <something>. An example would be IsBlank or IsNull or IsNumeric. From the
looks of things, you already know whether your user is authenticated, and
you're just looking to _do_ something with that knowledge.

I'd call the method MaybeRedirect or RedirectIfAuthenticated.
Nov 19 '05 #3
I generally agree John. I think you got the method name wrong, though.
What about "EnsureAuthenticated" or something?

Tor Bådshaug
tor.badshaug [//at\\] bekk.no
I'd call the method MaybeRedirect or RedirectIfAuthenticated.

Nov 19 '05 #4
"Tor Bådshaug" <to**********@nospam.com> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
I generally agree John. I think you got the method name wrong, though.
What about "EnsureAuthenticated" or something?


Not really, since it's being informed of whether or not the user is
authenticated. In fact, if it weren't for the names of the methods and
parameters, we would have no reason to suspect that this method had anything
to do with authentication. Take a look at what you get if you rename:

Sub Something(a As Boolean)
If a Then
Response.Redirect(wherever)
End If
End Sub

It looks like this method conditionally redirects, or rather it's more like
"redirect if I tell you to".

I'd say some refactoring was in order. I wonder if that sub is really called
from multiple places with different expressions for "a", or do all callers
use the same expression? If that's the case, then the expression should be
moved inside of the Sub:

Sub RedirectIfNotAuthenticated
If Not Request.IsAuthenticated Then
Response.Redirect(pcPageLogin)
End If
End Sub
John Saunders
"There's nothing worse than a developer learning refactoring"
Nov 19 '05 #5
The message is saying that the isUserAuthenticated function doesn't appear
to be an instance method (in that it doesn't work against a specific
instance of a class), but instead seems to be more of a utility method. It
recommends (and from what I can tell, it's right), to declare the sub as
shared:

shared sub isUserAuthenticated()
...
end sub

If this "shared" word is new to you, you might wanna do some reading:
http://www.amazon.com/exec/obidos/AS...479544-3615055

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"hansiman" <ha***@hotmail.com> wrote in message
news:60********************************@4ax.com...
Just beginning using FxCop in my asp.net projects...

I get a lot of error messages under the header: AvoidUnusedParameters

for funtions and routines lik:

Sub isUserAuthenticated(ByVal blnLoggedIn As Boolean)
If Not (blnLoggedIn) Then
System.Web.HttpContext.Current.Response.Redirect(p cPageLogin)
End If
End Sub

Where the suggested resolution is:
"The 'this' parameter (or 'Me' in VB) of UtilsAdmin.
isUserAuthenticated(Boolean):Void is never used. Mark
the method as static (or Shared in VB) or use 'this'/Me
in the method body."

I don't get it? How do I use 'this'?

Nov 19 '05 #6
Yes, of course, I was a little quick there (although RedirectIfAuthenticated
actually was wrong - RedirectIfNotAuthenticated is fine), imagining the
method in question actually looked like
the RedirectIfNotAuthenticated method you suggest in your last post (which
is exactly the same
form of the method I have used for my EnsureAuthenticated-method in one of
my projects)....
Sorry.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no

Sub RedirectIfNotAuthenticated
If Not Request.IsAuthenticated Then
Response.Redirect(pcPageLogin)
End If
End Sub
John Saunders
"There's nothing worse than a developer learning refactoring"

Nov 19 '05 #7
"Tor Bådshaug" <to**********@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Yes, of course, I was a little quick there (although
RedirectIfAuthenticated
actually was wrong - RedirectIfNotAuthenticated is fine), imagining the
method in question actually looked like
the RedirectIfNotAuthenticated method you suggest in your last post (which
is exactly the same
form of the method I have used for my EnsureAuthenticated-method in one of
my projects)....
Sorry.
No problem. I understood the use of the term "Ensure" (perhaps you use
EnsureChildControls at times?).

As you point out, it's a matter of perspective. In my Forms Authentication
applications, I have been able to allow ASP.NET to do my ensuring for me,
and haven't found a need to call such a method. On the other hand, I _have_
had the need to do conditional redirects on my Forms Authentication login
page. This page can also be considered as the "unauthorized access handler
page", and a few such login pages of mine have had to redirect based on the
_reason_ the user was bounced back to the login page.

So, to me, this was a conditional redirect.

Different beholders, different eyes, same code.

John Saunders


Tor Bådshaug
tor.badshaug [//at\\] bekk.no

Sub RedirectIfNotAuthenticated
If Not Request.IsAuthenticated Then
Response.Redirect(pcPageLogin)
End If
End Sub
John Saunders
"There's nothing worse than a developer learning refactoring"


Nov 19 '05 #8
Thanks for your insights on both FxCop and how to improve the code...
I use the sub redirect an unauthenticated user. I think I will
reconsider the security structure! Hadn't heard of
Request.IsAuthenticated! I'll dig into this.
On Sun, 12 Dec 2004 16:50:36 -0500, "John Saunders" <johnwsaundersiii
at hotmail.com> wrote:
"Tor Bådshaug" <to**********@nospam.com> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
I generally agree John. I think you got the method name wrong, though.
What about "EnsureAuthenticated" or something?


Not really, since it's being informed of whether or not the user is
authenticated. In fact, if it weren't for the names of the methods and
parameters, we would have no reason to suspect that this method had anything
to do with authentication. Take a look at what you get if you rename:

Sub Something(a As Boolean)
If a Then
Response.Redirect(wherever)
End If
End Sub

It looks like this method conditionally redirects, or rather it's more like
"redirect if I tell you to".

I'd say some refactoring was in order. I wonder if that sub is really called
from multiple places with different expressions for "a", or do all callers
use the same expression? If that's the case, then the expression should be
moved inside of the Sub:

Sub RedirectIfNotAuthenticated
If Not Request.IsAuthenticated Then
Response.Redirect(pcPageLogin)
End If
End Sub
John Saunders
"There's nothing worse than a developer learning refactoring"


Nov 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Crocker Data Processing | last post by:
Hi. I've written a bunch of custom rules for FxCop version 1.23, and today happened to download and install (separately) the new FxCop 1.321. Arrgghh ! The new introspection stuff is a lot...
5
by: Chua Wen Ching | last post by:
I had use fxcop to check my code. I had 1 confusion here. I would normally call a method by this way in my IAnimal: Example: public void CallFuncA(ushort port); But fxcop says i need to...
2
by: Uri Dor | last post by:
Hi, everyone, The question is whether there is (or can be) an FxCop rule that makes sure I don't do this: String.Format("{0}-{1}-{2}", 5); Or similar things. Some explanations: 1) of course I...
3
by: Rasmus | last post by:
I VS 2005 beta 2 i have a solution with - a number of classes - a website - a httphandler - a http module I want to run fxcop on my class files - but cant find out how to enable it. I've...
0
by: Naveen Mukkelli | last post by:
Hi, I'm using FxCop to make final changes to my class library developed in Visual C#.NET 2003. I have tried to add a new word in to the custom dictionary in the following way, I have...
3
by: Velvet | last post by:
I ran FxCop on one of the components for my web site and the security rules what me to add " tags like the ones listed below: This breaks my ASP.NET application. So my question is,...
4
by: Bruce One | last post by:
Given a project with some .cs files, Is there a way to block some of these files to be read by FxCop ?
5
by: Peter Ritchie [C# MVP] | last post by:
I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time. I've just recently had the cycles to investigate the warning. The warning message...
3
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.