How do I name the same variable but of different type
I often come across situations when I need to use the same variable but converted to a different type.
for example:
string port;
...
ValidatePort(int port);
Here in ValidatePort
I need to use the same variable port
but its type should be integer. In order to do that I need first to convert original port
to int
and use a temporary variable like iPort
or something similar to pass it then to ValidatePort
This is not the only case of naming collision and in every other situation I used different approaches (if I ne开发者_如何学JAVAeded a string I called it variableName + String
or some other endings)
Is there a naming convention in C# for that or a common approach for naming variables that are similar but of different types?
I would say it is wise to make it obvious what a variable is by looking at it.
var time;
you have no idea what that represents. It could be any of these:
int time; // number of seconds
DateTime time;
TimeSpan time;
I would say
int port = 7;
string portAsString = port.toString();
but for your code above, there is no problem with just calling it port as this code is perfectly valid:
string port = "7";
int portAsInteger = int.parse(port); // If you need a temporary variable
myMethod(int.parse(port)); // You can use variable 'port' twice as scope is different
myMethod(portAsInteger);
public void myMethod(int port) { .... }
I don’t really see the problem – why can’t you just call it like this:
ValidatePort(int.Parse(port));
?
That said, I don’t like such code – it essentially uses “stringly typing” and that’s a bad thing. The fact that you need to resort to type prefixes to disambiguate your names is a clear sign that you are doing something wrong.
And that is: your Port
variable should never be of type String
in the first place, if it’s really a number. Use the right type as soon as possible.
For instance, if you get the port number from the user via a TextBox
, the don’t ever store the content as a string, use the correct type right away:
int port;
if (! int.TryParse(portInput.Text, out port)) {
// Handle wrong user input.
}
(Notice the error handling which is essential for all user input.)
There are many notations you can use:
strSomething
somethingStr
szSomething // Microsoft uses this in their C stuff
It's a matter of preference. Best practice is to have descriptive variables, and deal with collisions in whatever way seems best for the particular situation. Just don't switch styles everywhere so it ends up begin really confusing.
You can use a prefix for your class wide fields:
string _port;
object _anyObject;
bool _anyBool;
public bool AnyBool {get{return _anyBool;}}
void Validate(int port)
{
/..
}
The current version C# does not support this (also, I don't think future release will either support this). You could define your own standards through out your project to avoid this naming collision. Just a wild guess - When you want same name but different types, does it mean source of values coming for both are different. For example, when reading from textbox the value might be a string and you are using string port
, but for actual operation it requires it to be an integer
(int port
). If this is the case then you could use some prefix indicating where this value belongs to.
精彩评论