|
what's the difference between the method you've used and
result = int.parse("100000");
|
|
|
|
|
|
The difference is that if you pass a string to int.parse() that is not a valid string (contains noon-numeric characters) it will throw an exception that you will need to manually handle.
If you use my method the error is handled automatically and the result is set to 0, its up to you if you want to handle the error yourself thou, depends how much validation control you need.
if you are expecting the result could sometimes be 0 as a result then you may be best to handle the error yourself. but in the case of a port number you shouldnt need to do this, just check if result is 0 for an error.
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Thank you for the reply... next time I convert I'll bare it in mind.
|
|
|
|
|
musefan wrote: just check if result is 0 for an error
Better to assign and check the boolean return value of the TryParse method before working with the value. Still preferable to catching an exception!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Ahh yes, i forgot TryParse() returns a bool
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
If the input string is not a valid integer, int.Parse throws an exception, while int.TryParse returns false without setting result value.
e.g.
string wrong = "xyz";
result = int.Parse(wrong);
string wrong = "xyz";
int result = -1;
bool success = int.TryParse(wrong, out result);
|
|
|
|
|
Thanks for the reply, however according to the above post the result would be set to zero and not stay at -1 or is it only set to zero if there is no valid number in it?
|
|
|
|
|
Yes, I was wrong, int.TryParse puts 0 in the result if the parsing fails.
For some reason, I tought that the resul value was unchanged in case of failure, my bad.
|
|
|
|
|
No worries thanks for the clarification
|
|
|
|
|
hopingToCode wrote: only set to zero if there is no valid number
... or if the number in the text is zero! Check the return value of the TryParse method to determine if successful.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
musefan has given you the perfect answer. Just be aware that if the number can't be converted to an int you will get zero.
I have an extension method that I use when working with text/xml etc files to simplify stuff.
public static class ExtensionMethods
{
public static Int32 ToInt32(this string value)
{
int result;
Int32.TryParse(value, out result);
return result;
}
} Now I can can just use something like
int intFromFile = stringFromFile.ToInt32();
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
If I might...
Is it considered really bad practice to do Convert.ToInt32 ?
I have seen stuff (and well, I have used it, too) like:
string x = "10";
int xInt;
try
{
xInt = Convert.ToInt32(x);
}
catch
{
xInt = 0;
}
Would that not do the same as TryParse?
Or is it an unnecessary way around it, or simply, crap?
var question = (_2b || !(_2b));
|
|
|
|
|
Not bad practice, possibly a little slower going via the Convert class but I would have to check the implementation to be sure.
The convert class is great for converting from a built in type to another (not just from string), but I would guess the Int32.Parse or Int32.TryParse methods would be perhaps a little more efficient.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
How can i automatically increments the build number in an AssemblyInfo.cs?
Any example? using macro or function method?
Kelvin
|
|
|
|
|
MS, he say:
The version number has four parts, as follows:
<major version>.<minor version>.<build number>.<revision>
You can specify all the values or you can accept the default build number, revision number, or both by using an asterisk (*). For example, [assembly:AssemblyVersion("2.3.25.1")] indicates 2 as the major version, 3 as the minor version, 25 as the build number, and 1 as the revision number. A version number such as [assembly:AssemblyVersion("1.2.*")] specifies 1 as the major version, 2 as the minor version, and accepts the default build and revision numbers. A version number such as [assembly:AssemblyVersion("1.2.15.*")] specifies 1 as the major version, 2 as the minor version, 15 as the build number, and accepts the default revision number. The default build number increments daily. The default revision number is random.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
williamnw wrote: [assembly:AssemblyVersion("1.2.*")]
I've tried this and I've never been able to get it to work. It always just gives me a version of "1.2.*" on the actual file.
Have you got it working williamnw? (Sorry for hijacking your thread 8kelvin)
For 8kelvin: There are two other options:
1) The AssemblyInfo[^] task can be used to auto increment the version number by modifying the assemblyinfo.cs file prior to the build being run. You just add an include statement to your project file.
2) There's a task in the MSBuild Community Tasks[^] project that can be used to generate a version number and write it into a generated assemblyinfo.cs file prior to the build running. Again, it just requires some additions to your csproj file (or msbuild script if you use it)
To be honest I have problems with both methods in my projects. We have large projects containing multiple assemblies and always end up with references screwed up because one of them is trying to reference versions prior to the number increment, or version numbers getting incremented twice or stupid things like that. This is the one area of our system I'm very unhappy with, but I can't find a decent solution.
Simon
|
|
|
|
|
I just got this version file working. Thanks guys.
For Simon Stevens,
Simon Stevens wrote: It always just gives me a version of "1.2.*" on the actual file.
you need to delete
[assembly: AssemblyFileVersion("1.0.0.0")]
in order to make it work.
Kelvin
modified on Friday, March 20, 2009 2:58 AM
|
|
|
|
|
Thanks. I'll give it a try.
Simon
|
|
|
|
|
Saved me answering, thanks!
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Hi.
I want to show a control (Like a textbox) with specific opacity !
How can I do it in Win App ?
Could you guide me ?
Thanks in advance.
|
|
|
|
|
I dont think it is posible with built in .Net controls.
You can set opacity of a from, which also alters any opacity for its own controls automatically.
You may have to make a custom control to handle your needs
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi,
How can i get the background colors of all the pixel in a retangle
Thankyou
YPKI
|
|
|
|
|
What kind of rectangle are you talking about?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Read my image processing articles.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|