Namespace

M

marcus

I have a problem trying to communicate between 2 namespaces, which I
have in the same file (news.aspx.cs). I'd like to create a new
variable in my first namespace "News" which is an instance of my class
Daddy.DBConnection.DBSQLConnection. But when I try to create my new
variable I get the error message: "expected class, delegate, enum,
interface, or struct".

I try create my new variable with the following line:
private Daddy.DBSQLConnection oConnection;

Anyone knows what I'm doing wrong?

----------- news.aspx.cs ---------------------------------------


namespace News
{
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class News : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

}
}
}

namespace Daddy.DBConnection
{
using System;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
/// "Daddy.Connection" is a class for handeling data, such as XML,
DB, SQL etc
/// </summary>
public class DBSQLConnection
{
protected SqlConnection oConn;
protected SqlCommand oCmd;
protected string strConnection;

public DBSQLConnection() {
// Don't do nothing yet
}

public string GetConnectionString()
{
return ConfigurationSettings.AppSettings["ConnectionString"];
}
protected void Connect()
{
oConn.ConnectionString = GetConnectionString();
oConn.Open();
}
protected void CloseConnection()
{
if (oConn.State.ToString()=="Open")
{
oConn.ConnectionString = GetConnectionString();
oConn.Close();
}
}
public int ExecuteStmt(string strSQL)
{
oCmd.CommandText = strSQL;
oCmd.CommandType = CommandType.Text;
return oCmd.ExecuteNonQuery();
}
}
}
 
T

The Crow

u cannot declare a variable between in namespace block before class block
unless the variable is a "class, delegate, enum,
interface, or struct". this is exactly what the error message tells.
this is a misunderstanding most beginners failed.
 
R

Ryan Trudelle-Schwarz

marcus said:
I have a problem trying to communicate between 2 namespaces, which I
have in the same file (news.aspx.cs). I'd like to create a new
variable in my first namespace "News" which is an instance of my class
Daddy.DBConnection.DBSQLConnection. But when I try to create my new
variable I get the error message: "expected class, delegate, enum,
interface, or struct".

I try create my new variable with the following line:
private Daddy.DBSQLConnection oConnection;

Is this a typo in the message or did you leave out the second level of the
namespace in your code?
 
M

marcus å

Is this a typo in the message or did you leave out the >second level of
the
namespace in your code?

I am declaring the variable as a private inside the class News.

What do you mean typo? What have I missed?

Thanks!
 
M

marcus å

Sorry guys - if found the problem. I hadn't defined it in the class, it
was defined in the namespace, like you told me.

Thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

what is base() in inheritance 2
Simple question 4
inheritance problem 2
tostring 3
Windows Service Error 7
Base Class constructor problem 9
Help! Stupid C# Problem 5
Composite UI Application Block issues 3

Top