Accessing a Namespace From a Seperate File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Accessing a Namespace From a Seperate File

    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?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by mcfly1204
    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?
    What is the error that you are receiving?
    Have you included the namespace properly?

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      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
      This should be easily translatable into C# by any of the converters out there...

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        Originally posted by Frinavale
        What is the error that you are receiving?
        Have you included the namespace properly?
        The 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

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by mcfly1204
          The 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

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by mcfly1204
            The 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.
            Post the code that's giving the error.

            Comment

            • balabaster
              Recognized Expert Contributor
              • Mar 2007
              • 798

              #7
              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;
                  }
              }
              file2.cs is as follows:

              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";
              
                      }
                  }
              }
              It all works like a charm for me...haven't used using for importing my namespace file...it just works.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by balabaster
                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;
                    }
                }
                file2.cs is as follows:

                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";
                        }
                    }
                }
                It all works like a charm for me...haven't used using for importing my namespace file...it just works.
                Because you were putting the fully qualified name of the class of course.
                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

                • mcfly1204
                  New Member
                  • Jul 2007
                  • 233

                  #9
                  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.
                  Last edited by Frinavale; Oct 30 '07, 08:18 PM. Reason: Added [code] tags to make more legible

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    The 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

                    • mcfly1204
                      New Member
                      • Jul 2007
                      • 233

                      #11
                      Originally posted by r035198x
                      The 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.
                      Correct. 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?

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Originally posted by mcfly1204
                        Correct. 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 creating a constructor for this class that sets the strExpression variable to a default value?

                        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

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by mcfly1204
                          Correct. 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?
                          Including the namespace makes classes readiliy available. Not variables defined withing classes (unless if the variables are static ...).
                          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

                          • balabaster
                            Recognized Expert Contributor
                            • Mar 2007
                            • 798

                            #14
                            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
                            You can do
                            Code:
                            Dim bVal As Boolean = MyClass.Blah("Stuff")
                            But you can't do:
                            Code:
                            Dim bVal As Boolean = MyClass.Blah2("Stuff")
                            You would have to do
                            Code:
                            Dim oMyInstance As MyClass
                            Dim bVal As Boolean = oMyInstance.Blah2("Stuff")
                            It's the same with properties of classes - you can't refer to them in a shared context, only from an instance context. The reason for this is that the property doesn't actually exist because there's no logical existance of the class...when you call a shared function it doesn't need an instance of the class to execute as it doesn't reference any properties, it just runs as a logical process. In order for the property StringProperty1 to exist, there needs to be a logical instance of the class. Does that make sense?

                            Ergo, you would have to do the following:
                            Code:
                            Dim oInst As MyClass
                            oInst.StringProperty1 = "Hello World"

                            Comment

                            • mcfly1204
                              New Member
                              • Jul 2007
                              • 233

                              #15
                              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

                              Working...