Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Range Validation

Status
Not open for further replies.

Llazwas

IS-IT--Management
Feb 16, 2007
58
US
I'm a C# newbie so please go easy. I'm writing a simple console app that I'm testing that needs to diplay someone's age but validate their birth year is within a range. I step through the code but it doesn't validate and I can't figure out why. Any help would be greatly appreciated. P.S. I'm using VS 2008.


using System;

public class Age
{
private int year;
private int age;

//constructor
public Age()
{

}//end constructor

public int Year
{
get
{
return year;
}//end get
set
{
if (value >= 1950 && value <= System.DateTime.Today.Year)
year = value;
}//end set
}//end property Year




public void DisplayAge()
{

Console.WriteLine("Enter your year of birth:");
year = Convert.ToInt32(Console.ReadLine());


age = System.DateTime.Today.Year - year;


Console.WriteLine("Age: {0}", age);

}//end DisplayAge

}//end class Age



________________
Test application
________________


using System;

public class AgeTest
{
public static void Main(string[] args)
{
Age myAge = new Age();

myAge.DisplayAge();

}//end Main method

}//end class HealthProfileTest
 
Your "set" doesn't look like it's ever being called. Either call it before your DisplayAge or call it within your DisplayAge function. Also, your if check(in set) will not set your member variable "year" if its not within range, which may result in unexpected results.. printing a null value.
 
Sorry, I'm not sure how I would call it within my DisplayAge function.
 
its a public function, so you should have access to it everywhere. Also, you're using c# so for more specifics of c# standards you can check this forum:

I would add a parameter list to set:

Code:
int set(int value)
{
      if (value >= 1950 && value <= System.DateTime.Today.Year)
        year = value;
else
 year = -1;

return year;
}

then in either the DisplayAge or in the main call set:

Code:
 public void DisplayAge()
   {

      Console.WriteLine("Enter your year of birth:");
      year = Convert.ToInt32(Console.ReadLine());

      year = set(year); //note that if its not in range year becomes -1


      age = System.DateTime.Today.Year - year;


      Console.WriteLine("Age: {0}", age);

   }//end DisplayAge

or

Code:
public class AgeTest
{
   public static void Main(string[] args)
   {
      Age myAge = new Age();

      myAge.set(); //note that calling set here will disregard the DisplayAge user input 

      myAge.DisplayAge();

   }//end Main method

Also, it should be noted that c# is c++ extended to use namespaces out the wazoo. this means you dont need instances of your classes, you can call the functionality directly with the Scope Resolution Operator:):).

so rather than:
Code:
Age myAge = new Age();

you can simpley make the calls:
Code:
Age::set(someInt);
Age::DisplayAge();

assuming that the namespace you contain it in is available.

for instance your declaration of "using System" tells the compiler that you have access to system. Console is an object/namespace within system and WriteLine is a function contained in Console.
It almost seems like you're trying to implement c# via half c# and half c++. its kind of an awkward statement to make seeing how c# is c++ extended to use the crap out of namespaces.
Personally, if you're just trying to teach yourself, i would start with c++, then move on to c#, because once you realize whats going on in the lower level it will increase your understanding of the namespace concept.

I hope that helps a little bit. My primary "profficiency" is c++, and i only know a little c#.
 
That is a HUGE help, thanks very much! Just trying to digest all the new stuff.
 
It looks like you're trying to follow this:

i would start out simple:

define your own name space:

Code:
namespace AgeManipulation
{
}

then add a basic class to it:

Code:
namespace AgeManipulation
{
      public class Age
      {
            int m_nAge;
      }
}

then in your main include your newlyl defined namespace:

Code:
using AgeManipulation;

int main()
{
}

then do something like setting the variable inside it:

Code:
using system;
using AgeManipulation;
int main
{
     AgeManipulation::Age::m_nAge = 12;
}

then print it out:

Code:
using system;
using AgeManipulation;
int main
{
     AgeManipulation::Age::m_nAge = 12;
     
     //not sure this is exactly how to use WriteLine with and int variable
     Console.WriteLine(AgeManipulation::Age::m_nAge);
}
 
Edit:

Code:
Console.WriteLine("Age: {0}", AgeManipulation::Age::m_nAge);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top