I making a C# asp.net website in visual studio and all my pages are in the same folder so far(as are there corresponding .cs files). In one of the C# classes I'm trying to instantiate an object of another class, but it isn't recognizing it.
2 classes, taxcalc and taxcalcres.
in taxcalc:
public taxcalc() {
}
in taxcalcres:
taxcalc tc = new taxcalc();
the part in bold it puts a red zigzag under and when I hold the cursor over it it says
The type or namespace name 'taxcalc' could not be found(are you missing a using directive or an assembly reference?)
There both public partial classes in the same folder. I need a taxcalc type in taxcalcres to use taxcalc's methods.
Please help I'm stuck(I'm new to C#, but I've done a bit of Java before. I chose C# over VB because it's more similar to java)
If you are using code-behind method, which it sounds like, then all your aspx and aspx.cs files can be in the same folder. But if you want to write a new class then it should exist in a separte .cs file in your App_Code folder.
Remember: mark posts that helped you as the answer to aid future readers
..but each aspx.cs file is a new class isn't it? I just need to use one of its methods in another file. You didn't answer the question about why it wont recognize the taxcalc type though
Essentially each of your pages is a new class. The aspx.cs is only part of the class. Notice the keyword
partial in the declaration and that it inherits from System.Web.UI.Page.
Any methods you define in one page will not be visible to another page unless you create a new instance of that page or mark the method as static. But I don't recommend doing either.
As for your taxcalc class, this should be created in its own .cs file in the App_Code folder. That way it will be dynamically compiled and useable by all of your pages in the same app.
Its a good practice to create classes to encapsulate common functionality across pages and the App_Code folder is the place for them to live.
Hope that helps.
Remember: mark posts that helped you as the answer to aid future readers
So i should make a seperate standalone csharp file in the appcode folder and then I can create an instance of it in my partial classes? And then use the methods? The only problem is the methods use the values of the textboxes in the html half of the class,
so how would the cs file in the appcode access them?
What you are attempting to do here is not going to work I'm afraid.
You cannot from one page reference objects from another because a pages objects only exist while the page exists.
You will have to save their values to SessionState, then you could access them later on from another page.
Let's say you have 2 pages and 1 class in your App_Code. We'll call them Page1, Page2, and MyClass.
Lets say on Page1 you have 2 textboxes that take in an X and a Y value. Then on Page2 you want to show the sum of those values.
First lets create MyClass:
public static class MyClass {
public static string X {
get {
return HttpContext.Current.Session["XValue"];
}
set {
HttpContext.Current.Session.Add("XValue", value);
}
}
public static string Y {
get {
return HttpContext.Current.Session["YValue"];
}
set {
HttpContext.Current.Session.Add("YValue", value);
}
}
public static string Sum() {
int x = Convert.ToInt32(this.X);
int y = Convert.ToInt32(this.Y);
return (x + y).ToString;
}
}
In Page1 you have 2 TextBoxes : txtVal_x and txtVal_y, and a button : btnSubmit
In your btnSubmit() method you will store the values of X and Y. Since MyClass is declared "static" you don't need an instance of it. Just go
mk12
Member
1 Points
4 Posts
C# class not recognized
Aug 18, 2008 03:49 PM|LINK
I making a C# asp.net website in visual studio and all my pages are in the same folder so far(as are there corresponding .cs files). In one of the C# classes I'm trying to instantiate an object of another class, but it isn't recognizing it.
2 classes, taxcalc and taxcalcres.
in taxcalc:
public taxcalc() {
}
in taxcalcres:
taxcalc tc = new taxcalc();
the part in bold it puts a red zigzag under and when I hold the cursor over it it says
The type or namespace name 'taxcalc' could not be found(are you missing a using directive or an assembly reference?)
There both public partial classes in the same folder. I need a taxcalc type in taxcalcres to use taxcalc's methods.
Please help I'm stuck(I'm new to C#, but I've done a bit of Java before. I chose C# over VB because it's more similar to java)
C sharp help not recognized object
whatispunk
Contributor
4074 Points
876 Posts
Re: C# class not recognized
Aug 18, 2008 03:55 PM|LINK
If you are using code-behind method, which it sounds like, then all your aspx and aspx.cs files can be in the same folder. But if you want to write a new class then it should exist in a separte .cs file in your App_Code folder.
Why UpdatePanels Are Dangerous
Why You Should Not Place Your Whole Site In An UpdatePanel
mk12
Member
1 Points
4 Posts
Re: C# class not recognized
Aug 18, 2008 05:01 PM|LINK
..but each aspx.cs file is a new class isn't it? I just need to use one of its methods in another file. You didn't answer the question about why it wont recognize the taxcalc type though
whatispunk
Contributor
4074 Points
876 Posts
Re: C# class not recognized
Aug 18, 2008 05:27 PM|LINK
Essentially each of your pages is a new class. The aspx.cs is only part of the class. Notice the keyword partial in the declaration and that it inherits from System.Web.UI.Page.
Any methods you define in one page will not be visible to another page unless you create a new instance of that page or mark the method as static. But I don't recommend doing either.
As for your taxcalc class, this should be created in its own .cs file in the App_Code folder. That way it will be dynamically compiled and useable by all of your pages in the same app.
Its a good practice to create classes to encapsulate common functionality across pages and the App_Code folder is the place for them to live.
Hope that helps.
Why UpdatePanels Are Dangerous
Why You Should Not Place Your Whole Site In An UpdatePanel
mk12
Member
1 Points
4 Posts
Re: C# class not recognized
Aug 18, 2008 05:32 PM|LINK
So i should make a seperate standalone csharp file in the appcode folder and then I can create an instance of it in my partial classes? And then use the methods? The only problem is the methods use the values of the textboxes in the html half of the class, so how would the cs file in the appcode access them?
whatispunk
Contributor
4074 Points
876 Posts
Re: C# class not recognized
Aug 18, 2008 06:44 PM|LINK
What you are attempting to do here is not going to work I'm afraid.
You cannot from one page reference objects from another because a pages objects only exist while the page exists.
You will have to save their values to SessionState, then you could access them later on from another page.
Let's say you have 2 pages and 1 class in your App_Code. We'll call them Page1, Page2, and MyClass.
Lets say on Page1 you have 2 textboxes that take in an X and a Y value. Then on Page2 you want to show the sum of those values.
First lets create MyClass:
public static class MyClass { public static string X { get { return HttpContext.Current.Session["XValue"]; } set { HttpContext.Current.Session.Add("XValue", value); } } public static string Y { get { return HttpContext.Current.Session["YValue"]; } set { HttpContext.Current.Session.Add("YValue", value); } } public static string Sum() { int x = Convert.ToInt32(this.X); int y = Convert.ToInt32(this.Y); return (x + y).ToString; } }In Page1 you have 2 TextBoxes : txtVal_x and txtVal_y, and a button : btnSubmit
In your btnSubmit() method you will store the values of X and Y. Since MyClass is declared "static" you don't need an instance of it. Just go
MyClass.X = txtVal_x.Text;
MyClass.Y = txtVal_y.Text;
Now in Page2 you have another TextBox : txtSum that you will initialize in the Page_Load()
txtSum.Text = MyClass.Sum();
Hopefully that example wasn't too lame, but it should give you a basic idea of where you are going wrong.
You should read a bit about the Page life-cycle and how pages are rendered in ASP.NET.
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Why UpdatePanels Are Dangerous
Why You Should Not Place Your Whole Site In An UpdatePanel
mk12
Member
1 Points
4 Posts
Re: C# class not recognized
Aug 18, 2008 08:26 PM|LINK
Thanks
rjcox
Contributor
7064 Points
1444 Posts
Re: C# class not recognized
Aug 18, 2008 09:59 PM|LINK
For a Web Site project, then this is true (or put it into a separate class project referenced by the site).
For a Web Application Project then there is no restriction on the folder containing the .cs file (and using a class library is again an option).