Ok, I'm sorry I am a php developer and I am looking into C#.NET and really interested. The only problem I am having is how the @*#(@ do I make and use classes in C#.NET???????
For example, if I had a class in PHP and I wanted to keep it in a separate file I wold just include the file so..
<?php
include('
classes/class.dbconnect.php'
);
$conn = new DBConnect(); ?>
But now, if I want to do the same thing in C#.NET how do I do that? I dont know how to bring in a .cs file... I'm SO CONFUSED!!! I keep finding tutorials about complieing this and that, but I can't do it on my Windows Home edition of my computer, but my hosting
provider has ASP.NET 2.0. I dont know man... someone please explain this too me. Tell me what I need... dumb it down.
Right now all I need to do is understand how to bring in classes that I make. I can get everything else from there.
It's actually very easy in .NET. For example, if you create a .cs file in the App_Code directory, you can use the code within it with no effort. Other than that, you might need to include a namespace.
<%
/*using (YLU c = new YLU())
{
Response.Write(c.SayHello());
}
*/
GRS_CS.YLU c = new GRS_CS.YLU();
Response.Write(c.SayHello());
//using System.GRC_RS;
string [] hello = {"John","Peter","Bobby","Jake","Tim","Will","Hello","Mike","Dave"};
int [] hello1 = {1,32,32,3,232,4545,232,423,2232};
%>
<select id="hello" name="hello">
<%
int i=0;
foreach(int val in hello1){
string inner = hello[i];
%>
<option value="<%=val%>"><%=inner%></option>
<%
i++;
}//end foreach
%>
</select>
</body>
</html>
It throws this error...
Compiler Error Message:
CS0246: The type or namespace name 'GRS_CS' could not be found (are you missing a using directive or an assembly reference?)
If you're using inline code, then you need to import namespaces as follows instead of a using statement.
<%@ Import Namespace="" %>
As for the error message, that means it's unable to find the namespace. Did you include the assembly that that namespace resides in in the bin directory?
The aspnet_client directory has JavaScript files that are used for stuff like client-side validation. You shouldn't be putting your files in there. They should be in the root of the site.
alright.., I gave up and bought a book.. it cleared up just about all my questions. The more and more I look into ASP.NET I like it.. but I know this might seem like a bad place to ask this question, but I figure who better to ask, but ASP.NET programmers.
mrmatrix
Member
30 Points
15 Posts
Classes
May 17, 2006 10:04 AM|LINK
For example, if I had a class in PHP and I wanted to keep it in a separate file I wold just include the file so..
<?php include('But now, if I want to do the same thing in C#.NET how do I do that? I dont know how to bring in a .cs file... I'm SO CONFUSED!!! I keep finding tutorials about complieing this and that, but I can't do it on my Windows Home edition of my computer, but my hosting provider has ASP.NET 2.0. I dont know man... someone please explain this too me. Tell me what I need... dumb it down.
Right now all I need to do is understand how to bring in classes that I make. I can get everything else from there.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Classes
May 17, 2006 01:26 PM|LINK
It's actually very easy in .NET. For example, if you create a .cs file in the App_Code directory, you can use the code within it with no effort. Other than that, you might need to include a namespace.
i.e.
using MyNamespace.Blah
That's about it.
HTH,
Ryan
mrmatrix
Member
30 Points
15 Posts
Re: Classes
May 19, 2006 07:01 AM|LINK
I'm using these class inside of an aspx file. so
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" Debug="true" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<%
/*using (YLU c = new YLU())
{
Response.Write(c.SayHello());
}
*/
YLU c = new YLU();
Response.Write(c.SayHello());
//using System.GRC_RS;
string [] hello = {"John","Peter","Bobby","Jake","Tim","Will","Hello","Mike","Dave"};
int [] hello1 = {1,32,32,3,232,4545,232,423,2232};
%>
<select id="hello" name="hello">
<%
int i=0;
foreach(int val in hello1){
string inner = hello[i];
%>
<option value="<%=val%>"><%=inner%></option>
<%
i++;
}//end foreach
%>
</select>
</body>
</html>
This is working if the class is NOT inside of a name space..
But if I put it inside of a namespace and call the namespace GRC_CS.. Then it doesn't work
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" Debug="true" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<%
/*using (YLU c = new YLU())
{
Response.Write(c.SayHello());
}
*/
GRS_CS.YLU c = new GRS_CS.YLU();
Response.Write(c.SayHello());
//using System.GRC_RS;
string [] hello = {"John","Peter","Bobby","Jake","Tim","Will","Hello","Mike","Dave"};
int [] hello1 = {1,32,32,3,232,4545,232,423,2232};
%>
<select id="hello" name="hello">
<%
int i=0;
foreach(int val in hello1){
string inner = hello[i];
%>
<option value="<%=val%>"><%=inner%></option>
<%
i++;
}//end foreach
%>
</select>
</body>
</html>
It throws this error...
Compiler Error Message: CS0246: The type or namespace name 'GRS_CS' could not be found (are you missing a using directive or an assembly reference?)
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Classes
May 19, 2006 07:29 AM|LINK
If you're using inline code, then you need to import namespaces as follows instead of a using statement.
<%@ Import Namespace="" %>
As for the error message, that means it's unable to find the namespace. Did you include the assembly that that namespace resides in in the bin directory?
Ryan
mrmatrix
Member
30 Points
15 Posts
Re: Classes
May 19, 2006 12:13 PM|LINK
/App_Code/
And here is the contents of that file...
using System;
namespace GRC_CS
{
public class YLU
{
private string sayhello;
public YLU()
{
this.sayhello = "Hello World!";
}//end YLU()
public string SayHello()
{
return this.sayhello;
}//end SayHello()
}//end YLU :: Class
}//end GRC_CS
Remember, the aspx file is inside of:-
/cs/
So yea.. what is a directive reference, and what is a assembly reference?
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Classes
May 19, 2006 01:31 PM|LINK
Don't know where the problem lies exactly. I just copied your code verbatum into a class inside of App_Code and could access the code.
Ryan
mrmatrix
Member
30 Points
15 Posts
Re: Classes
May 19, 2006 02:13 PM|LINK
When I got the hosting there was this directory in there with the following subdirectories
/aspnet_client/system_web/2_0_50727/
I can put files in there, but I can't run them. What is this folder all about?
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: Classes
May 19, 2006 03:13 PM|LINK
The aspnet_client directory has JavaScript files that are used for stuff like client-side validation. You shouldn't be putting your files in there. They should be in the root of the site.
Ryan
mrmatrix
Member
30 Points
15 Posts
Re: Classes
May 21, 2006 08:58 PM|LINK
What are the downfalls of ASP.NET?