I know that I am missing something simple here, but if I have fileA.cs and fileB.cs, where fileA.cs contains namespace Access, I had the understanding that I could utilize that namespace in fileB.cs by adding the statement using Access at the top of fileB.cs. However, when I reference an item, such as a public string, from the Access namespace, I receive an error. What am I missing?
Accessing a Namespace From a Seperate File
Collapse
X
-
Originally posted by mcfly1204I know that I am missing something simple here, but if I have fileA.cs and fileB.cs, where fileA.cs contains namespace Access, I had the understanding that I could utilize that namespace in fileB.cs by adding the statement using Access at the top of fileB.cs. However, when I reference an item, such as a public string, from the Access namespace, I receive an error. What am I missing?
Have you included the namespace properly? -
Do you have a code example? I'm not sure exactly of the syntax in C#, however in VB 2005 your namespace would be as follows:
Code:'Demo public namespace which would be equivalent of File1.cs (is actually File1.vb) Public Namespace MyNamespace Public Class MyClass Public MyDemoProperty As String End Class End Namespace 'Demo usage would be equivalent of File2.cs (is actually File1.vb) Public Class MyForm Private Sub Loaded(ByVal Sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim oClassInstance As New MyNamespace.MyClass oClassInstance.MyDemoProperty = "Hello World" End Sub End Class
Comment
-
Originally posted by FrinavaleWhat is the error that you are receiving?
Have you included the namespace properly?
I thought that all I had to do to include the namespace was to add an using statement for it. Apparently that is not the case. Keep in mind I am intending to include the namespace for the entire file.Comment
-
Originally posted by mcfly1204The error is: The name 'strExample' does not exist in the current context. I realize this error is because the proper namespace cannot be accessed from the other file.
I thought that all I had to do to include the namespace was to add an using statement for it. Apparently that is not the case. Keep in mind I am intending to include the namespace for the entire file.
Have you included both .cs files into your project?Comment
-
Originally posted by mcfly1204The error is: The name 'strExample' does not exist in the current context. I realize this error is because the proper namespace cannot be accessed from the other file.
I thought that all I had to do to include the namespace was to add an using statement for it. Apparently that is not the case. Keep in mind I am intending to include the namespace for the entire file.Comment
-
Okay, I created a brand spanking new project - all the VS 2005 defaults...noth ing changed:
file1.cs is as follows:
Code:using System; using System.Collections.Generic; using System.Text; namespace MyNamespace { class MyClass { public string MyProperty; } }
Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MyNamespace.MyClass oInst = new MyNamespace.MyClass(); oInst.MyProperty = "Hello world"; } } }
Comment
-
Originally posted by balabasterOkay, I created a brand spanking new project - all the VS 2005 defaults...noth ing changed:
file1.cs is as follows:
Code:using System; using System.Collections.Generic; using System.Text; namespace MyNamespace { class MyClass { public string MyProperty; } }
Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MyNamespace.MyClass oInst = new MyNamespace.MyClass(); oInst.MyProperty = "Hello world"; } } }
I've asked the OP for the code because the error message they got is probably just a scoping issue and may not have anything to do with namespaces. It looks like they have a some variable that they have not declared first before using.Comment
-
The first file:
[code=cpp]
using System;
using System.IO;
using System.Xml;
using System.Xml.XPat h;
namespace ParseXML
{
public class XMLSelect
{
public string strExpression;
public string strExpression2;
public string strExpression3;
public void Main(string[] args)
{
string strPICKUP;
string[] strFiles;
XPathDocument doc;
XPathNavigator nav;
// Remove the following if unused
XPathNodeIterat or NodeIter;
XPathExpression expr;
[/code]
The second file:
[code=cpp]
using System;
using System.Data;
using System.Data.Sql Client;
using ParseXML;
namespace Queries
{
class DbInsert
{
public static void InsertTemp()
{
SqlConnection tempCon = new SqlConnection() ;
tempCon.Connect ionString = ("server=; uid=; password=; database=");
tempCon.Open();
// verify insert statement
SqlCommand cmdInsert = new SqlCommand("INS ERT INTO [tblTemp] FromContactName VALUES @strExpression" , tempCon);
// is cmdInsert.Execu teReader correct?
cmdInsert.Param eters.AddWithVa lue("@strExpres sion", strExpression);
SqlDataReader DBReader = cmdInsert.Execu teReader();
}[/code]
strExpression is what causes the error. This easily could be a case of not having something declared properly.Comment
-
Originally posted by r035198xThe line
[CODE=cpp] cmdInsert.Param eters.AddWithVa lue("@strExpres sion", strExpression);[/CODE]
is the problem line right? That's because you pass strExpression as the second parameter there but you have not declared it anywhere.Comment
-
Originally posted by mcfly1204Correct. However, given it is declared in the ParseXML namespace, I thought I would be able to use that variable if I included that namespace in this file. Do you see what I was intending to do?
Have you considered using Properties instead of Public Variables?
This way when accessing the string you can make sure a value is returned to the calling code.Comment
-
Originally posted by mcfly1204Correct. However, given it is declared in the ParseXML namespace, I thought I would be able to use that variable if I included that namespace in this file. Do you see what I was intending to do?
Even if you had the two classes withing the same namespace, you'd still get an error because the variable is defined as an instance variable of another class so you need an object (instance) of the class containing that variable in your second class to be able to reference that variable(throug h the instance).Comment
-
This is similar in a manner of speaking to public shared functions and the way you instantiate them...
As a public function you can only call it from an instance of the class:
Code:Class MyClass Public StringProperty1 As String Public Shared Function Blah(ByVal ParamStuff As String) As Boolean Return False End Function Public Function Blah2(ByVal ParamStuff As String) As Boolean Return False End Function End Class
Code:Dim bVal As Boolean = MyClass.Blah("Stuff")
Code:Dim bVal As Boolean = MyClass.Blah2("Stuff")
Code:Dim oMyInstance As MyClass Dim bVal As Boolean = oMyInstance.Blah2("Stuff")
Ergo, you would have to do the following:
Code:Dim oInst As MyClass oInst.StringProperty1 = "Hello World"
Comment
-
So basically, the manner in which I would like to use the variable is impossible because that is pretty much the reason for classes. Variables with the same name but that are in different classes are completely different.
I am going to try a few things, then post what I come up with to get the end results that I want.Comment
Comment