2

How can I use "public variables" in a same or a different namespace but in a same application?

for example:

1st namespace in a BookStore web application

namespace BookStore
{
    public partial class index : System.Web.UI.Page
    {
        public string command; // the public variable declared

2nd namespace, in the Same BookStore application

namespace Admin.BookStore // here i try to include the BookStore namespace..
{
    public partial class admin : System.Web.UI.Page
    {
        void access()
        {
            command = "something text" ;

Here I think that command should be seen in the intellisense. I think because it was a public variable, but it doesn't show up.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Juyal Jee
  • 517
  • 5
  • 10
  • You need an instance of the class to be able to access it's fields/members – DGibbs Apr 12 '13 at 08:35
  • 1
    To be utterly clear - this has nothing to do with namespaces, it's to do with basic object-oriented programming. You need instances of classes (or static members) to set the value of a member. There's no such thing as global variables in .NET. – J. Steen Apr 12 '13 at 08:41
  • Exactly right, by the looks of it, he's looking simply to pass information from one page to another as per my example. – Chris Dixon Apr 12 '13 at 08:44
  • 1
    I think what you are suggesting to do is not a good approach to program web forms in asp.net. If you allow me to, I suggest you use the session to store your variables. – DanielV Jul 22 '15 at 14:35

4 Answers4

1

in the admin class you can also add the following to the top of the file:

Using BookStore;

however, index is not a static class so you would have to have an instance of that class to be able to use the command field;

Index indexInstance = new Index();
indexInstance.command = "something text";
wterbeek
  • 451
  • 9
  • 26
  • That's not the real issue, though, as setting a variable like that wouldn't really do much. They're Page-classes. They're not meant to be instantiated locally. I would question what the OP is trying to do. – J. Steen Apr 12 '13 at 08:39
  • Your basic premise is, however, correct. You need an instance (or a static member) of a class to be able to set the value. =) – J. Steen Apr 12 '13 at 08:42
  • 1
    yep, i'm not that experienced with asp.net. i read the question and thought it was just a c# namespace issue :) i'll read more carefully next time – wterbeek Apr 12 '13 at 08:46
  • acutally i have already tried it in the same way as you suggested. But it was also not helpful !!! – Juyal Jee Apr 12 '13 at 13:32
  • 2
    @user2114182 Don't just say "it wasn't helpful". Instead tell us how or why it didn't work, maybe edit your question. Give us something to work with if you really want to be helped. – slugster Apr 13 '13 at 01:25
1

There's a number of ways of doing this, but the worrying thing is that you're trying to set the same variable across pages - without a Session/static, this won't work.

My way (for a beginner) is to start playing with class inheritance.

Try this:

namespace BookStore
{
    public partial class index : BasePage
    {

    }
}

namespace Admin.BookStore
{
    public partial class index : BasePage
    {
        void access()
        {
            base.command = "something text"; // This accesses the BasePage variable
        }
    }
}

namespace BookStore
{
     public class BasePage : System.Web.UI.Page
     {
          // public static string command { get; set; } // Bad practice IMO
          public string command 
          {
               get {
                   if(Session["command"] != null) return Session["command"].ToString();
                   return String.Empty;
               }
               set {
                   Session["command"] = value;
               }
          }  
     }
}
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
0

In the file that contains the class admin, scroll to the top of the page and insert the line

using BookStore;

note this normally goes above the namespace declaration in the file you are adding it to. This makes your class/page visible to the class that is going to use it.

To follow the example you gave you now need an instance of the class in order to access the public field:

var myIndexObject = new index();
myIndexObject.command = "my value";
slugster
  • 49,403
  • 14
  • 95
  • 145
  • I'm puzzled by the down vote for a 100% correct answer, but I guess that's just the way some people roll... – slugster Apr 12 '13 at 08:55
  • i downvoted the answer, and removed it now. i did this because i posted the same code as anwer, 4 minutes before you posted it. on Meta i read that was not the correct thing to do, so i removed the downvote again – wterbeek Apr 12 '13 at 09:04
  • 1
    @wterbeek No worries, at least you came back and reversed it :) By all means down vote incorrect or bad answers, but try not to down vote simply because an answer is similar to yours - that can just prompt retaliation and messiness. – slugster Apr 13 '13 at 01:23
0

to access the class in a different namespace, you need to specify or 'using' the full namespace, from your application root namespace.

E.g: if your app root namespace is AppNameSpace:

AppNameSpace.BookStore.index.command = "..."

or

using AppNameSpace.BookStore
'
'
'
index.command = "..."
ajakblackgoat
  • 2,119
  • 1
  • 15
  • 10