Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 7th, 2012, 06:27 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default Ch: 5 Problems with Date Time Reference Objects

Hey, again. Another problem that i can't find any way out of...
I was trying to write the program shown in figure 5-10. I started writing the code shown in listing 5-2. When i was finished i had 42 errors and i can't find where i made any difference from your code!
Also, most of the errors are the same and a very comon on is: 'frmMain.myTime' is a 'field' but is used like a 'type'

Code:
using System;
using System.Windows.Forms;

public class frmMain : Form
{
    private Label label1;
    private Label label2;
    private Label label3;
    private Label label4;
    private Label label5;
    private Label label6;
    private Label label7;
    private TextBox txtCompleteDateAndTime;
    private TextBox txtLongDate;
    private TextBox txtShortDate;
    private TextBox txtGeneralDateAndTime;
    private TextBox txtLongTime;
    private TextBox txtShortTime;
    private TextBox txtDaysToNewYears;
    private Button btnRefresh;
    private Button btnClose;
   
    #region Windows code
    private void InitializeComponent()
    {
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.txtCompleteDateAndTime = new System.Windows.Forms.TextBox();
this.txtLongDate = new System.Windows.Forms.TextBox();
this.txtShortDate = new System.Windows.Forms.TextBox();
this.txtGeneralDateAndTime = new System.Windows.Forms.TextBox();
this.txtLongTime = new System.Windows.Forms.TextBox();
this.txtShortTime = new System.Windows.Forms.TextBox();
this.txtDaysToNewYears = new System.Windows.Forms.TextBox();
this.btnRefresh = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label1
// 
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(154, 17);
this.label1.TabIndex = 0;
this.label1.Text = "Full Date And Time:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// label2
// 
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(13, 39);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(154, 17);
this.label2.TabIndex = 1;
this.label2.Text = "Long Date:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// label3
// 
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label3.Location = new System.Drawing.Point(13, 65);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(154, 17);
this.label3.TabIndex = 2;
this.label3.Text = "Short Date:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// label4
// 
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Location = new System.Drawing.Point(13, 92);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(154, 17);
this.label4.TabIndex = 3;
this.label4.Text = "General Date And Time:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// label5
// 
this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label5.Location = new System.Drawing.Point(13, 119);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(154, 17);
this.label5.TabIndex = 4;
this.label5.Text = "Long Time:";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// label6
// 
this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label6.Location = new System.Drawing.Point(13, 145);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(154, 17);
this.label6.TabIndex = 5;
this.label6.Text = "Short Time:";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// label7
// 
this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label7.Location = new System.Drawing.Point(13, 172);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(154, 17);
this.label7.TabIndex = 6;
this.label7.Text = "Days Until New Years Eve:";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// 
// txtCompleteDateAndTime
// 
this.txtCompleteDateAndTime.Location = new System.Drawing.Point(184, 12);
this.txtCompleteDateAndTime.Name = "txtCompleteDateAndTime";
this.txtCompleteDateAndTime.Size = new System.Drawing.Size(250, 20);
this.txtCompleteDateAndTime.TabIndex = 7;
// 
// txtLongDate
// 
this.txtLongDate.Location = new System.Drawing.Point(184, 38);
this.txtLongDate.Name = "txtLongDate";
this.txtLongDate.Size = new System.Drawing.Size(250, 20);
this.txtLongDate.TabIndex = 8;
// 
// txtShortDate
// 
this.txtShortDate.Location = new System.Drawing.Point(184, 64);
this.txtShortDate.Name = "txtShortDate";
this.txtShortDate.Size = new System.Drawing.Size(250, 20);
this.txtShortDate.TabIndex = 9;
// 
// txtGeneralDateAndTime
// 
this.txtGeneralDateAndTime.Location = new System.Drawing.Point(184, 91);
this.txtGeneralDateAndTime.Name = "txtGeneralDateAndTime";
this.txtGeneralDateAndTime.Size = new System.Drawing.Size(250, 20);
this.txtGeneralDateAndTime.TabIndex = 10;
// 
// txtLongTime
// 
this.txtLongTime.Location = new System.Drawing.Point(184, 118);
this.txtLongTime.Name = "txtLongTime";
this.txtLongTime.Size = new System.Drawing.Size(250, 20);
this.txtLongTime.TabIndex = 11;
// 
// txtShortTime
// 
this.txtShortTime.Location = new System.Drawing.Point(184, 142);
this.txtShortTime.Name = "txtShortTime";
this.txtShortTime.Size = new System.Drawing.Size(250, 20);
this.txtShortTime.TabIndex = 12;
// 
// txtDaysToNewYears
// 
this.txtDaysToNewYears.Location = new System.Drawing.Point(184, 169);
this.txtDaysToNewYears.Name = "txtDaysToNewYears";
this.txtDaysToNewYears.Size = new System.Drawing.Size(250, 20);
this.txtDaysToNewYears.TabIndex = 13;
// 
// btnRefresh
// 
this.btnRefresh.Location = new System.Drawing.Point(92, 212);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.Size = new System.Drawing.Size(75, 23);
this.btnRefresh.TabIndex = 14;
this.btnRefresh.Text = "Refresh";
this.btnRefresh.UseVisualStyleBackColor = true;
// 
// btnClose
// 
this.btnClose.Location = new System.Drawing.Point(359, 212);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 15;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
// 
// frmMain
// 
this.ClientSize = new System.Drawing.Size(446, 247);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnRefresh);
this.Controls.Add(this.txtDaysToNewYears);
this.Controls.Add(this.txtShortTime);
this.Controls.Add(this.txtLongTime);
this.Controls.Add(this.txtGeneralDateAndTime);
this.Controls.Add(this.txtShortDate);
this.Controls.Add(this.txtLongDate);
this.Controls.Add(this.txtCompleteDateAndTime);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.ResumeLayout(false);
this.PerformLayout();

    }
    #endregion

    public frmMain()
    {
        InitializeComponent();
        UpdateTimeInfo();        //Update textboxes
    }

    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

    private void UpdateTimeInfo();
    {
    int days;
    DateTime myTime = new DateTime();
    myTime = DateTime.Now;
    DateTime newYears = new DateTime(myTime.Year, 12, 31);

    txtCompleteDateAndTime.Text = myTime.ToString("f")
    txtLongDate.Text = myTime.ToString("D")
    txtShortDate.Text = myTime.ToString("d")
    txtGeneralDateAndTime.Text = myTime.ToString("g")
    txtLongTime.Text = mytime.ToString("T")
    txtShortTime.Text = mytime.ToString("t")

    days = newYears.DayOfYear - myTime.DayOfYear;
    txtDaysToNewYears.Text = days.ToString();
    }


     private void btnRefresh_Click(object sender, EventArgs e)
     {
          UpdateTimeInfo();
     }
}
I'm using Visual Studio 2010 if that has anything to do with this problem.
Once again: Thanks ;)
  #2 (permalink)  
Old September 13th, 2012, 06:43 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default DateTime errors

First, look at your line:

private void UpdateTimeInfo();
{

There shouldn't be a semicolon at the end of the method.
Next, look at the lines:

txtCompleteDateAndTime.Text = myTime.ToString("f")
txtLongDate.Text = myTime.ToString("D")
txtShortDate.Text = myTime.ToString("d")
txtGeneralDateAndTime.Text = myTime.ToString("g")
txtLongTime.Text = mytime.ToString("T")
txtShortTime.Text = mytime.ToString("t")

All of these need semicolons at the end. In this list, look at the last two lines. You did not write the variable "mytime" with a capital "T" like you did when you defined it. The program will work after these corrections, although the Close button still has no code tied to it.

When you get this many errors, look at the first error in the list, then look backwards from there. Often an earlier error (i.e., the semicolon at the end of UpdateTimeInfo()) triggers an avalanche of error messages because things get out of sync for the debugger.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)

Last edited by DrPurdum; September 13th, 2012 at 06:50 AM..
The Following User Says Thank You to DrPurdum For This Useful Post:
tobbeebbot (September 13th, 2012)
  #3 (permalink)  
Old September 13th, 2012, 10:34 AM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks. I found the solution when i downloaded all the code from wrox some days ago. But thanks for your reply. It's realy cool that you always answer peoples questions. :) By the way, is it possible to use a program loop to make the program update every second?
  #4 (permalink)  
Old September 13th, 2012, 10:43 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Automatic update

Sure. Rather than give you the answer, I'll give you a hint. Read the last value for a variable named second for the DateTime variable and, if it is one greater than a variable named lastSecondValueRead, reassign the current value for second to lastSecondValueRead and then call the update function.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
The Following User Says Thank You to DrPurdum For This Useful Post:
tobbeebbot (September 14th, 2012)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Time Field not displaying Date and Time webmeister Access 2 January 31st, 2011 12:49 AM
date and time in EST time zone anboss XSLT 1 May 21st, 2008 01:42 PM
Objects are always passed by reference or value? deb_kareng C# 2 November 2nd, 2006 03:53 AM
Time objects comparison and manipulation x_ray Javascript How-To 1 November 29th, 2005 09:55 AM



All times are GMT -4. The time now is 10:01 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.