|
string s = "1";
int n = Convert.ToInt32(s);
Paresh
|
|
|
|
|
however,
string s="a";
int n=Convert.toint32(s);
will give an error, won't it? there must be a way to tell...
|
|
|
|
|
|
if you want comparision
then
string s = "1";
if (s.CompareTo("1") == 0 )
{
// blah
}
Paresh
|
|
|
|
|
i found a discussion here:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20391710.html
but i really can't believe this language leads to such a pain in the a$$
|
|
|
|
|
see, i just gave u the temp. working solution. i believe there are tons of solution for this type of problem
u could check char.by char.,
regex, string - try-catch method, int.parse, etc...
Paresh
|
|
|
|
|
Use this:
public static bool IsNumber(string strNumber)
{
Regex objNotNumberPattern=new Regex("[^0-9.-]");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
If this was VB.NET, you could use IsNumeric, so you could wrap that up in a class that is callable from C#.
Another C# approach would probably be to check each character in the string with IsDigit or IsNumber, or maybe use something similar to the following.
string s="1234";<br />
bool b = s.Trim("1234567890".ToCharArray()).Equals("");
--
Ian Darling
|
|
|
|
|
Here's the C# equivalent of VBs IsNumeric function:
using System;
using System.Globalization;
public class Conversion
{
private static bool IsNumericTypeCode(TypeCode t)
{
switch (t)
{
case TypeCode.Boolean:
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
default:
return false;
}
}
public static bool IsNumeric(object expression)
{
string sExp = null;
IConvertible conv = expression as IConvertible;
if (null == conv)
{
if (null == expression as char[])
return false;
sExp = new string((char[])expression);
}
else
{
TypeCode t = conv.GetTypeCode();
if (IsNumericTypeCode(t))
return true;
else if (t == TypeCode.String || t == TypeCode.Char)
sExp = conv.ToString(null);
}
if (null == sExp)
return false;
else
return IsNumeric(sExp);
}
public static bool IsNumeric(string expression)
{
if (null == expression || 0 == expression.Length)
return false;
else
{
double d;
return double.TryParse(expression,
NumberStyles.Any, null, out d);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
That's cool function, Richard!!!
But wait!! I have one doubt... Sometimes C# Compiler shouts if I forget to put a break stating
'Control can not fall through...'
but here in this code snippet, you have said return true for multiple case options and it has compiled.
How?
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
The C# compiler is able to determine that any code after the return statement is unreachable, so execution could never fall through. If you add the break statements after the return statements, you'll see unreachable code warnings when you compile.
From the C# language reference:
The statement list of a switch section typically ends in a break, goto case, or goto default statement, but any construct that renders the end point of the statement list unreachable is permitted. For example, a while statement controlled by the Boolean expression true is known to never reach its end point. Likewise, a throw or return statement always transfers control elsewhere and never reaches its end point.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Hi Richard,
Pl. see here:
switch (x)
{
case 1:
x =4;
break;
case 2:
case 3:
x = 5;
break;
default:
Console.Write("dog");
}
The compiler reports
C:\deepak\test.cs(31): Control cannot fall through from one case label ('default:') to another
What is the actual cause?
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
The end-point of your default case is reachable, since it doesn't end with a jump statement (break, return, goto, etc.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Does anybody know how to show the childs as tabbed documents in an MDI application?
Thanks
|
|
|
|
|
|
Hi,
Does anybody know how to make a MDI child dock (or a control from a windows control library) and slide like the toolbox?
Thanks
|
|
|
|
|
|
Newbie Simple question, I can change a textbox ForeColor, BackColor so that say the text is Blue,
But how do I do the same to the datetimepicker?
Although the properties are hidden, they can be changed but they make no difference to the form.
Thanks
Steve Graham
|
|
|
|
|
Are there "set" properties for the DateTimePicker ? If not, I don't think it's going to work.
R.Bischoff | C++
.NET, Kommst du mit?
|
|
|
|
|
I need to build a user control base class for a very large application. I must build a User Control base class, so it can be reused in embedded HTML pages and in .NET Win Forms. I need a set of common classes that will house common functionality for the application (e.g. securityUtilities, xmlUtilities, errorUtilities). Can anyone tell me a good method of doing this?
I have run into a few problems:
1) I can't do multiple inheritances in C#.
2) A user control can't be an abstract class.
3) I have considered building interfaces to combine the classes, but I don't need each class to be a static class.
Can anyone help?
Thank you,
Shane
|
|
|
|
|
C# does not support multiple inheritance. You can simulate this through interface inheritance.
R.Bischoff | C++
.NET, Kommst du mit?
|
|
|
|
|
Anonymous wrote:
2) A user control can't be an abstract class.
But why mark it abstract then?
Anonymous wrote:
3) I have considered building interfaces to combine the classes, but I don't need each class to be a static class.
Interfaces works with instance methods and has nothing to do with static classes.
MyDUMeter: a .NET DUMeter clone
|
|
|
|
|
2) A user control can't be an abstract class.
As I understand it, the controls can be abstract but not in the IDE. When you edit the control, the editor creates an instance of a control, which it cannot do if it's abstract.
Depending how complicated your design is, you can set it to not abstract whilst working on it in the editor, and change it to abstract when you have finished editing it.
|
|
|
|
|
Hi!
I have the following problem:
I have two application one of them serializes a class(serialization is OK a can Deserialize it in this app too)
The problem is that when I am trying to Deserialize a class from file using the second application, i am getting FileNotFound error.
What can be wrong?
I am using Binary formmaters.
Looking through the serialized file I found that the name of programm i also serialized and probably that name dont let another program to deserialize the class?
|
|
|
|
|
The problem seems to be finding that file, not the object serialization. Have you written the right location of the file? have you used "\\" (double slash) in the string instead of simply "\", that's a very common error
|
|
|
|