Thread: Syntax problem

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Unhappy Syntax problem

    hi, I'm new to C# and have a few syntax problems.

    I will appreciate a little help.
    I'm trying to verify a string and can't get the comp to use isnum
    or isnumeric ect.

    Code:
    class Person
    	{
    		private string Name;
    		private int Age;
    		private int Height;
    		private int Weight;
    
                  //properties
                 
                     public string Name_User
    		{
                                          set
    		      {
    		              if (isnum(Name_User))
                                                       Name="ERROR"
                                                  else
                                                       Name=Name_User;
    		      }
    
    		     get
    		     {
    		            return Name;
    		     }		
                    }
    
    		// end Name_User
    I know I'm not doing something. But I don't know what

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you want to make sure your input is a number, it might be better to actually use a numeric datatype.

    Anyway, data validation functions for single characters are static member functions of the System.Char class. You can enumerate the single characters of a string and check them by using the char functions.

    Example:

    string s = "12345";

    foreach( char c in s )
    {
    if( ! Char.IsNumeric( c ) ) // error
    }

    ( Please note that I don't have a compiler or helpfile handy, so it's probably not 100% correct, use the MSDN Library to verify this )
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    Thanks.

    It works perfectly , but.....

    now I have a new problem:

    I have to print my Persons properties 3 times it takes up most of
    my Main and looks like this:

    Code:
    //show properties
    Per1.Show();
    Console.Writeline();
    Per2.Show();
    Console.Writeline();
    Per3.Show();
    Console.Writeline();
    I tried to write a ShowAll() function outside the Main and using it
    instead, but the compiler says that it cant recognise Per1,2,3 so it's basically usless.

    It was something like this: public/static void ShowAll () { ...}

    It should work shouldn't it?

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    If your Per1, Per2 and Per3 objects are local then any other functions you create won't be able to access them (as they aren't defined in their scope). You could either make them 'global' (NB: C# and all .NET languages don't have 'global' variables, but you can make 'static' member variables that work in the same way), or try something like this:
    Code:
    void Main()
    {
        [...]
        Person[] People = {Per1, Per2, Per3};
        foreach (Person p in People)
        {
            p.Show();
            Console.Writeline();
        }
        [...]
    }

  5. #5
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    Quote Originally Posted by IWannaLearn
    hi, I'm new to C# and have a few syntax problems.

    I will appreciate a little help.
    I'm trying to verify a string and can't get the comp to use isnum
    or isnumeric ect.

    Code:
    class Person
        {
            private string Name;
            private int Age;
            private int Height;
            private int Weight;
    
                  //properties
                 
                     public string Name_User
            {
                                          set
                  {
                          if (isnum(Name_User))
                                                       Name="ERROR";
                                                  else
                                                       Name=Name_User;
                  }
    
                 get
                 {
                        return Name;
                 }        
                    }
    
            // end Name_User
    Why you are using the Name_User property to set the Name field???

    You should use the value reserved word to check its type and assign the received string to the Name field.

    Regards,
    Jawwad Alam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  2. Syntax problem
    By newbiec++ in forum C++ Programming
    Replies: 6
    Last Post: 08-26-2005, 08:26 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. DJGPP assembly syntax ills...
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-11-2001, 02:54 AM