Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to implement a function which can receive a string parameter and return a bool type variable. Then the function must verify if the said sent parameter is integer and return true if it is or false if it isn't. Can you please help? i am new to C# and trying to learn on my own. Thank you.

What I have tried:

C#
class Program
    {

        static void Main(string[] args)
        {
            string[] values = { "10x", "10", "100xx" };
            foreach (var value in values)
            {
                int g;
                if (int.TryParse(value, out g))
                    Console.WriteLine("Value '{0}' is a number.", value, g);
                else
                    Console.WriteLine("Value '{0}' is not a number.", value == null ? "<null>" : value);
            }
            
        }
    }
Posted
Updated 24-Nov-16 23:36pm
v9
Comments
Philippe Mori 25-Nov-16 10:18am    
In Visual Studio, you can select some code and right-click and there will be an option to generate a function.

Otherwise, you are better to read some books or tutorial and try thing as you learn them...
Philippe Mori 25-Nov-16 16:59pm    
Is it an assignment or something you want to do by yourself. If it is an assignment, then checkString from solution 2 would be what you ask.

And if it is your own code, then in that case, it does not make sense to replace int.TryParse by something else as it already does exactly what you need.
Member 12869977 27-Nov-16 13:51pm    
It's sort of an assignment. I am doing a sheet of exercises from a book I've been studying and that what the exercise asked me to do .. I know my code works without the function but I need it because it said to create a function who will be called from Main and the second part of the exercise is saying to modify the function to receive a another string parameter in which I will the send the type of parameter I wish to validate: number or data. The function must be modified in such way to take account of this new parameter. If the parameter is a number we will display it's a number, if it's a data we will say it's a data... so that's the reason .. sorry for the bad English it's my second language .. still haven't figured out the second part so if you have any ideas feel free to give me some .. and thank you

An example of how you could do it with
" tryParse ".
The code could be even smaller by removing the method
" checkString(string str) "
and replacing it in the if statement with
a TryParse.

C#
static void Main(string[] args)
{
	string[] strArray = {"100k", "10", "10k" };
	string strPlaceholder;

	Console.WriteLine("\n ~~ Press F12 to quit ~~\n");

	do
	{
		foreach (string str in strArray)
		{
			strPlaceholder = null;

			if (!checkString(str))
			    strPlaceholder = "not";

			Console.WriteLine($"# {str} is {strPlaceholder} a valid Number");
		}

	} while (Console.ReadKey(true).Key != ConsoleKey.F12);
			
}

static bool checkString(string str)
{
	int placeholder;

	return int.TryParse(str, out placeholder);
}
 
Share this answer
 
Comments
Member 12869977 25-Nov-16 5:22am    
i changed it a little bit but it worked ! thank you
Sossi.Mond 25-Nov-16 5:26am    
You're welcome :-)
C#
A simplifield way like this should do. 

(you should test it if a string containing a real/float/double stringified value is passed in such as "3.14159" etc... (you could use the string contains method and check for decimal being present in the string (except the last position on the right).


static bool IsThisAnInteger( string input = "" ){
  int g = 0;
  try {
    Int.Parse(input,out g);   // the Parse method will throw an error if not int.
    return true;
  } catch( Exception x){
    ;  // you'll end up here if input is not a stringified int.
  }
  return false;
}
 
Share this answer
 
Comments
F-ES Sitecore 25-Nov-16 4:18am    
Never use exceptions for this, exceptions are very expensive which is why the TryParse method exists. Only use Parse if you know the input is of the correct format and won't throw an exception, otherwise use TryParse.
Philippe Mori 25-Nov-16 10:20am    
By the way, what OP does is already better that what you propose to do...
[no name] 25-Nov-16 16:15pm    
Can you expand on that?
Philippe Mori 25-Nov-16 16:25pm    
Calling int.TryParse is preferable over your IsThisAnInteger function as it will do the validation, the value is ready for use, the code is shorter, it require to pass a string and it use framework code.
[no name] 25-Nov-16 16:46pm    
Please read the first sentence in the posters question.

It is imperative that one understand the question prior to providing an answer.

Please provide your solution to the poster's original question as this would be indicative of you being part of the solution as the poster would gain from your input.

As they say, you're either part of the solution, or you are part of the problem.

I look forward to your solution as asked by the poster in the first place.

I believe TryParse does a try/catch internally.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900