2 Replies - 14159 Views - Last Post: 28 October 2008 - 01:35 PM Rate Topic: -----

#1 benmat2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-October 08

Error:'Myclass' does not contain a constructor that takes '

Post icon  Posted 24 October 2008 - 09:38 PM

Hi all ,
I have created a class to retrieve textbox values and show that on another control.
But when executed I got this error.
Error:'Myclass' does not contain a constructor that takes '0' arguments

Below is my full code .

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyClass._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
	<title></title>
</head>

<body>
	<form id="form1" runat="server">
	<div>
		<asp:Label ID="Label" runat="server" Text="ID"></asp:Label>
	<asp:textbox ID="tbID" runat="server"></asp:textbox><br />
	<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
	<asp:textbox ID="tbName" runat="server"></asp:textbox><br />
		<asp:Button ID="btnUseClass" runat="server" Text="Use Class" 
			onclick="btnUseClass_Click" /><br />
		<asp:Label ID="lblID" runat="server" ></asp:Label>
		<asp:Label ID="lblName" runat="server" ></asp:Label>
		
	
	</div>
	</form>
</body>
</html>



-----------------------------------
Code Behind)Default.aspx.cs

 namespace MyClass
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUseClass_Click(object sender, EventArgs e)
        {
            
            Test t = new Test();
            t.id = tbID.Text;
            t.name = tbName.Text;
            lblID.Text = t.id;
            lblName.Text = t.name;
        }   
    }
}



---------------------------
(class module)Test.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyClass
{
    public class Test
    {
        public string id;
        public string name;
        
        public Test(string id,string name)
        {
            this.name  = name;
            this.id = id;
        }

    }
    

}



Any help appreciated.
Thanks in advance.
Ben

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Error:'Myclass' does not contain a constructor that takes '

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error:'Myclass' does not contain a constructor that takes '

Posted 24 October 2008 - 09:56 PM

That's happening because the only constructor for your Test class is expecting 2 parameters

public Test(string id,string name)
{
      this.name = name;
      this.id = id;
}



And when you're creating an instance of this class you're not passing it anything

Test t = new Test();



Now you have 2 choices, you can create the instance like this

Test t = new Test(Convert.ToInt32(tbID.Text), tbName.Text);



Or create an empty costructor for your Test class

public Test()
{
}



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 benmat2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-October 08

Re: Error:'Myclass' does not contain a constructor that takes '

Posted 28 October 2008 - 01:35 PM

thanks
second the method works for me.
I have another doubt. how to pass textbox values to class.cs insted of code behind file.?
I wrote one class but nothing happens.
Here is the full code. plz check this and help me
Ben.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyClass._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
	<title></title>
</head>

<body>
	<form id="form1" runat="server">
	<div>
		<asp:Label ID="Label" runat="server" Text="ID"></asp:Label>
	<asp:textbox ID="tbID" runat="server"></asp:textbox><br />
	<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
	<asp:textbox ID="tbName" runat="server"></asp:textbox><br />
		<asp:Button ID="btnUseClass" runat="server" Text="Use Class" 
			onclick="btnUseClass_Click" /><br />
		<asp:Label ID="lblID" runat="server" ></asp:Label>
		<asp:Label ID="lblName" runat="server" ></asp:Label>
		
	
	</div>
	</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyClass
{
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void btnUseClass_Click(object sender, EventArgs e)
		{
			
			
					  
			Test t = new Test();

			t.add();
			t.id = tbID.Text;
			t.name = tbName.Text;
			t.fname = lblName.Text;
			
		}   
	}
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyClass
{
	public class Test 
	{
		public string id;
		public string name;
		public string fname;
		
		public Test()
		{
			//this.name  = name;
		   //this.id = id;
		}
		public void add()
		{
					 

			fname = string.Concat(id ,name);
						
				 }
		public string Id
		{
			get { return id; }
			set { id = value; }
			 
			
			 }
	   
		public string Name
		{
			get
			{
				return name; 
					 }
			set { name = value; }
		
		
			}

	}
	

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1