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.

4
  • You need an instance of the class to be able to access it's fields/members Commented Apr 12, 2013 at 8: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. Commented Apr 12, 2013 at 8:41
  • Exactly right, by the looks of it, he's looking simply to pass information from one page to another as per my example. Commented Apr 12, 2013 at 8: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. Commented Jul 22, 2015 at 14:35

4 Answers 4

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";
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Your basic premise is, however, correct. You need an instance (or a static member) of a class to be able to set the value. =)
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
acutally i have already tried it in the same way as you suggested. But it was also not helpful !!!
@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.
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;
               }
          }  
     }
}

Comments

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";

3 Comments

I'm puzzled by the down vote for a 100% correct answer, but I guess that's just the way some people roll...
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 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.
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 = "..."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.