What's wrong with this program?

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

What's wrong with this program?

 
0
  #1
Jan 9th, 2009
Now I still learning C#, and in one of the tutorials I found this program:

  1. using System;
  2.  
  3. namespace Returning_a_Class_From_a_Method
  4. {
  5. public class MVADate
  6. {
  7. private int dayOfBirth;
  8. private int monthOfBirth;
  9. private int yearOfBirth;
  10.  
  11. public void SetDate(int d, int m, int y)
  12. {
  13. dayOfBirth = d;
  14. monthOfBirth = m;
  15. yearOfBirth = y;
  16. }
  17.  
  18. public string ProduceDate()
  19. {
  20. string result = dayOfBirth + "/" + monthOfBirth + "/" +
  21. yearOfBirth;
  22.  
  23. return result;
  24. }
  25. }
  26.  
  27. public class MotorVehicleAdministration
  28. {
  29. public MotorVehicleAdministration()
  30. {
  31. birthdate = new MVADate();
  32. }
  33.  
  34. private string fullName;
  35. private MVADate RequestDateOfBirth();
  36. private bool isAnOrganDonor;
  37.  
  38. 1! private MVADate RequestDateOfBirth()
  39. {
  40. MVADate date = new MVADate();
  41.  
  42. Console.Write("Day of Birth: ");
  43. int d = int.Parse(Console.ReadLine());
  44.  
  45. Console.Write("Month of Birth: ");
  46. int m = int.Parse(Console.ReadLine());
  47.  
  48. Console.Write("Year of Birth: ");
  49. int y = int.Parse(Console.ReadLine());
  50.  
  51. date.SetDate(d, m, y);
  52. return date;
  53. }
  54.  
  55. static void Main(string[] args)
  56. {
  57. MotorVehicleAdministration MVA =
  58. new MotorVehicleAdministration();
  59.  
  60. Console.WriteLine("To process a registration, enter the information");
  61. Console.Write("Full Name: ");
  62. MVA.fullName = Console.ReadLine();
  63.  
  64. MVA.birthdate = RequestDateOfBirth();
  65.  
  66. Console.Write("Is the application an organ donor (0=No/1=Yes)? ");
  67. string ans = Console.ReadLine();
  68.  
  69. if (ans == "0")
  70. MVA.isAnOrganDonor = false;
  71. else
  72. MVA.isAnOrganDonor = true;
  73.  
  74. Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
  75. Console.WriteLine(" -=- Driver's License Application -=-");
  76.  
  77. Console.WriteLine("Full Name: {0}", MVA.fullName);
  78. Console.WriteLine("Dateof Birth: {0}", MVA.birthdate.ProduceDate());
  79. Console.WriteLine("Organ Donor? {0}", MVA.isAnOrganDonor);
  80.  
  81. Console.WriteLine();
  82.  
  83. Console.WriteLine("Press any key to continue...");
  84. Console.ReadKey();
  85. }
  86. }
  87. }

When I tried compile this program I received in line (marked by me as 1!) error :
Type Returning_a_Class_From_a_Method.MotorVehicleAdministration
already defines a member called 'RequestDateOfBirth' with the same parameter types which points to RequestDateOfBirth()

I think that error caused because of declaration
  1. private MVADate RequestDateOfBirth();
but I still can't find any solution to this. It's tutorial, but this program won't run. What I must do to correct error?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: What's wrong with this program?

 
0
  #2
Jan 9th, 2009
Yes you can remove the line you highlighted, and for the other 2 functions too.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: What's wrong with this program?

 
0
  #3
Jan 9th, 2009
Thanks for replay. All three private variables just a front highlighted line should be public - in this case program works fine.

Anyway thanks for replay.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

Re: What's wrong with this program?

 
0
  #4
Jan 9th, 2009
I think there's a couple of ways you could solve the problem you are having. One of the options you could use would be to make MotorVehicleAdministration a separate object.

  1. using System;
  2.  
  3. namespace Returning_a_Class_From_a_Method
  4. {
  5. public class MVADate
  6. {
  7. private int dayOfBirth;
  8. private int monthOfBirth;
  9. private int yearOfBirth;
  10.  
  11. public void SetDate(int d, int m, int y)
  12. {
  13. dayOfBirth = d;
  14. monthOfBirth = m;
  15. yearOfBirth = y;
  16. }
  17.  
  18. public string ProduceDate()
  19. {
  20. string result = dayOfBirth + "/" + monthOfBirth + "/" +
  21. yearOfBirth;
  22.  
  23. return result;
  24. }
  25. }
  26.  
  27. public class MotorVehicleAdministration
  28. {
  29. public MotorVehicleAdministration()
  30. {
  31. birthdate = new MVADate();
  32. }
  33.  
  34. private string fullName;
  35. private MVADate birthdate;
  36. private bool isAnOrganDonor;
  37.  
  38. public void RequestDateOfBirth()
  39. {
  40. Console.Write("Day of Birth: ");
  41. int d = int.Parse(Console.ReadLine());
  42.  
  43. Console.Write("Month of Birth: ");
  44. int m = int.Parse(Console.ReadLine());
  45.  
  46. Console.Write("Year of Birth: ");
  47. int y = int.Parse(Console.ReadLine());
  48.  
  49. birthdate.SetDate(d, m, y);
  50. }
  51.  
  52. public MVADate Birthdate
  53. {
  54. get
  55. { return birthdate; }
  56. }
  57.  
  58. public bool IsOrganDonor
  59. {
  60. get
  61. { return isAnOrganDonor; }
  62. set
  63. { isAnOrganDonor = value; }
  64. }
  65.  
  66. public string FullName
  67. {
  68. get
  69. { return fullName; }
  70. set
  71. { fullName = value; }
  72. }
  73. }
  74.  
  75. public class Test
  76. {
  77. static void Main(string[] args)
  78. {
  79. MotorVehicleAdministration MVA =
  80. new MotorVehicleAdministration();
  81.  
  82. Console.WriteLine("To process a registration, enter the information");
  83. Console.Write("Full Name: ");
  84. MVA.FullName = Console.ReadLine();
  85.  
  86. MVA.RequestDateOfBirth();
  87.  
  88. Console.Write("Is the application an organ donor (0=No/1=Yes)? ");
  89. string ans = Console.ReadLine();
  90.  
  91. if (ans == "0")
  92. MVA.IsOrganDonor = false;
  93. else
  94. MVA.IsOrganDonor = true;
  95.  
  96. Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
  97. Console.WriteLine(" -=- Driver's License Application -=-");
  98.  
  99. Console.WriteLine("Full Name: {0}", MVA.FullName);
  100. Console.WriteLine("Dateof Birth: {0}", MVA.Birthdate.ProduceDate());
  101. Console.WriteLine("Organ Donor? {0}", MVA.IsOrganDonor);
  102.  
  103. Console.WriteLine();
  104.  
  105. Console.WriteLine("Press any key to continue...");
  106. Console.ReadKey();
  107. }
  108. }
  109. }

The issue you were experiencing came from the fact that you had the RequestDateOfBirth declared twice. It didn't like that you had

private MVADate RequestDateOfBirth();

declared before

private MVADate RequestDateOfBirth()
{
...
}

I'm pretty sure you would have to do something like this in say...C++...but not C#. Even after fixing that issue, there were a lot of bugs in the code that dealt with accessibility issues with the variables all being declared private and then trying to access them from a static method.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: piotr_kast is an unknown quantity at this point 
Solved Threads: 0
piotr_kast piotr_kast is offline Offline
Newbie Poster

Re: What's wrong with this program?

 
0
  #5
Jan 9th, 2009
I didn't wrote this program. I find it on web pages tutorials from FunctionX. I am absolute beginer in Visual C# and so far I didn't found any book which is for absolute beginner (read: with no any contact with this programming).

Of course I found some books from which I was able to learn, but at some point this learning always stopped. On this web pages are true lessons for absolute beginners. But of course - from time to time - everyone can find some bugs...

But thanks for this answer...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: What's wrong with this program?

 
0
  #6
Jan 9th, 2009
then try the video training material at http://www.3dbuzz.com which is designed with people just like you in mind, and theres 3 sections all absolutely free for c#
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 365 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC