Click to See Complete Forum and Search --> : trying to learn


devilonline
September 24th, 2009, 09:18 AM
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:


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

eclipsed4utoo
September 24th, 2009, 09:47 AM
look in your project. do you see a "Program.cs" file? If so, open it and you will probably see another "Main()".

devilonline
September 24th, 2009, 09:55 AM
ops yes it has. how can i solve this?

thanks

bhushan1980
September 24th, 2009, 11:30 AM
Hey ....just try and remove the Main in this class and let the one in Program.cs remain...let me know
Bhushan

bhushan1980
September 24th, 2009, 11:31 AM
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

JonnyPoet
September 24th, 2009, 01:20 PM
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.