CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: trying to learn

  1. #1
    Join Date
    Sep 2009
    Posts
    15

    trying to learn

    hi, i'm learning c# by this book: "Wrox beginning c sharp 3.0 an introdution...." however the code he puts there never works fine for me. for exemple i have this code: "has more than one entry point defined: "windowsFormsApllication3.Main(). compile with /main to specficy the type that contains the entry point" how can i solve this. sorry im at the begining. (how can i put the code here?)


    here is the code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication3
    {
        public partial class frmain : Form
        {
    
            private Label label1;
            private TextBox txtLongDate;
            private Label label2;
            private TextBox txtShortDate;
            private Label label3;
            private TextBox txtGeneralDateAndTime;
            private Label label4;
            private TextBox txtLongTime;
            private Label label5;
            private TextBox txtShortTime;
            private Label label6;
            private TextBox txtDaysToNewYears;
            private Label label7;
            private Button btnClose;
            private Button btnRefresh;
            private TextBox txtCompleteDateAndTime;
            #region Windows code
            #endregion
    
            public frmain()
            {
                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();
            }
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    thanks

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: trying to learn

    look in your project. do you see a "Program.cs" file? If so, open it and you will probably see another "Main()".
    Last edited by eclipsed4utoo; September 24th, 2009 at 09:50 AM.
    ===============================
    My Blog

  3. #3
    Join Date
    Sep 2009
    Posts
    15

    Re: trying to learn

    ops yes it has. how can i solve this?

    thanks

  4. #4
    Join Date
    Jun 2006
    Posts
    645

    Re: trying to learn

    Hey ....just try and remove the Main in this class and let the one in Program.cs remain...let me know
    Bhushan

  5. #5
    Join Date
    Jun 2006
    Posts
    645

    Re: trying to learn

    Hey sorry, just change the body of the main in the Program.cs to the body in this Main and comment the frmMain class...
    Bhushan

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: trying to learn

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication3
    {
        public partial class frmain : Form
        {
     
            private Label label1;
            private TextBox txtLongDate;
            private Label label2;
            private TextBox txtShortDate;
            private Label label3;
            private TextBox txtGeneralDateAndTime;
            private Label label4;
            private TextBox txtLongTime;
            private Label label5;
            private TextBox txtShortTime;
            private Label label6;
            private TextBox txtDaysToNewYears;
            private Label label7;
            private Button btnClose;
            private Button btnRefresh;
            private TextBox txtCompleteDateAndTime;
            #region Windows code
            #endregion
     
            public frmain()
            {
                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();
            }
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    /* and in the program.cs 
    change the code in the Application.Run method to
    Application.Run(new frmain()); 
    */
    Basically that happens when people are creating a program automatically using VS2005 or VS 2008 and then copying code out of a book and they dont really know whats gong on in their program.
    Method Main is the method that starts the application and it starts by creating a new Form in that case which you have named frmain
    while the automatically created code if you did a WinForm Application wants to run new Form1()
    . But logically you cannot start a program twice. For your information. The book may have code that is done in the Time of VS 2003 and in that time it was done to have the method to start the application directly in the form. So this change in design has brought that difficulties. Enjoy your book, but you will need to adapt the code. I dont suggest to use the books CD, even if there is one, because typing is an additional step of learning.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured