Why FXCop recommends me not to do this way of method calling?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 write like a property rather than method to this:

public void CallFuncA(ushort port)
{
this.port = port;
}

Any idea? Is it a must and best practices for us to follow what fxcop says? I had so many errors in there. Sigh... i didn't know i code so bad. :(
 
You have here ambiguous variable name (port). Try not to use the same name
for external and internal variables. eg. change you function to "public void
CallFuncA(ushort Port);" This should solve the problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Chua Wen Ching said:
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 write like a property rather than method to this:

public void CallFuncA(ushort port)
{
this.port = port;
}

Any idea? Is it a must and best practices for us to follow what fxcop
says? I had so many errors in there. Sigh... i didn't know i code so bad. :(
 
Hi Tamir Khason,

This is what i normally will do.

private ushort port;

public ushort Port
{
get { return port; }
set { this.port = port; }
}

public void CallFuncA(ushort port) {}

---

Does it means, now i just ignore the code above and just do this:

private ushort port;

public void CallFuncA(ushort Port) {}

Just to confirm. Thanks again.
--
Regards,
Chua Wen Ching :)


Tamir Khason said:
You have here ambiguous variable name (port). Try not to use the same name
for external and internal variables. eg. change you function to "public void
CallFuncA(ushort Port);" This should solve the problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Chua Wen Ching said:
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 write like a property rather than method to this:

public void CallFuncA(ushort port)
{
this.port = port;
}

Any idea? Is it a must and best practices for us to follow what fxcop
says? I had so many errors in there. Sigh... i didn't know i code so bad. :(
 
OK,
The only thing it notice, that if you have method return void it'll better
to make it property with things you want to do in get/set area. However me
too prefer void method rather then complicated property even for user
friendly reflection of things, however there are people prefer properties
rather then methods.

You choose...

The only best practice I can think about in your case is avoiding of using
many member variables and declaration of local variables and pass it to
methods instead of sharing a member variable between methods. If you share a
member variable between methods, it will be difficult to track which method
changed the value and when.

As I said - you choose, maybe I'm wrong

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Chua Wen Ching said:
Hi Tamir Khason,

This is what i normally will do.

private ushort port;

public ushort Port
{
get { return port; }
set { this.port = port; }
}

public void CallFuncA(ushort port) {}

---

Does it means, now i just ignore the code above and just do this:

private ushort port;

public void CallFuncA(ushort Port) {}

Just to confirm. Thanks again.
--
Regards,
Chua Wen Ching :)


Tamir Khason said:
You have here ambiguous variable name (port). Try not to use the same name
for external and internal variables. eg. change you function to "public void
CallFuncA(ushort Port);" This should solve the problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Chua Wen Ching said:
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 write like a property rather than method to this:

public void CallFuncA(ushort port)
{
this.port = port;
}

Any idea? Is it a must and best practices for us to follow what fxcop
says? I had so many errors in there. Sigh... i didn't know i code so bad. :(
 
CallFuncA has a parameter with the same name as one of the fields of
the class. You might want to consider using a different convention to
avoid possible confusion, or take another look at the class design to
see if you really need to pass a port to the method at all.
 
Can you clarify something -- you mentioned an IAnimal, which to me implies
an interface -- which has no implementation, so I don't see where there's
room for fxcop to suggest use of a property.

But looking at what you've written below, the set accessor in the code you
gave is effectively a no-op as it just assigns a value to itself.

Try to follow this pattern (note capitalization):

private ushort port;

public ushort Port
{
get { return this.port; }
set { this.port = value; }
}

Note the use of 'this.' even where it's not technically needed (thus does
'this.' function much as the 'm_' or whatever from the days of old). If you
get into this habit, you will help avoid errors and make your code easier to
read.

Are you saying that the above code would cause fxcop to warn on

public void FuncA(ushort port) {}

as a class method? Seems odd if that's the case.


Chua Wen Ching said:
Hi Tamir Khason,

This is what i normally will do.

private ushort port;

public ushort Port
{
get { return port; }
set { this.port = port; }
}

public void CallFuncA(ushort port) {}

---

Does it means, now i just ignore the code above and just do this:

private ushort port;

public void CallFuncA(ushort Port) {}

Just to confirm. Thanks again.
--
Regards,
Chua Wen Ching :)


Tamir Khason said:
You have here ambiguous variable name (port). Try not to use the same name
for external and internal variables. eg. change you function to "public void
CallFuncA(ushort Port);" This should solve the problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Chua Wen Ching said:
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 write like a property rather than method to this:

public void CallFuncA(ushort port)
{
this.port = port;
}

Any idea? Is it a must and best practices for us to follow what fxcop
says? I had so many errors in there. Sigh... i didn't know i code so bad. :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top