Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# Static Classes

Options
  • 04-07-2006 11:09am
    #1
    Closed Accounts Posts: 216 ✭✭


    Hi,

    I have a public class setup that calls a static class from within the same .cs file. Is there any way to move the statis class to another .cs file and still be able to call it from the original .cs file? I think this can be done using Visual C# by "Adding Reference", but I am not using Visual C#. Is this still possible to do?

    Cheers

    gogul


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    It's as simple as just creating a new .CS file in your project, and cutting and pasting the class over. The only thing you have to make sure of is that they are in the same namespace (or you include the new namespace with the "using MyApplication.MyNamespace type thing at the top of your CS file).

    This would work with all classes, whether they're static or not. A preferred way of coding puts each class in it's own CS file. Adding "References" is used for adding in already compiled code into your project.


  • Closed Accounts Posts: 216 ✭✭gogul


    That's what I though it would be, but I still cant get the classes to see each other.

    I have 2 .cs files. One contains a namespace called Tools in a file called, unsurprisingly, Tools.cs. In this Tools namespace there is a static class: static class codeVerifier.
    In the other .cs file called Page.cs there is a namespace called Pages, which calls the Tools.cs file and namespace to use the codeVerifier class.

    Whan I put "using codeVerifier.Tools" at the top of the Pages.cs file, it returns "type or namespace name "codeVerifier" could not be found ".

    I am a bit new to this. Any idea on the problem?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    if the complete namespace of tools is "Tools", you write "Using Tools;". If the namespace is "MutantFruit.Applications.NewApplication.Tools" you write "Using "MutantFruit.Applications.NewApplication.Tools;".

    Then you can call your class normally by writing: codeVerifier bob = new codeVerifier(); or whatever.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Also,

    If you wanted to reference the codeVerifier class directly you'd put that after Tools. You seem to have them the wrong way around in your example.

    e.g.
    Tools.codeVerifier cv = new Tools.codeVerifier(); :)


  • Closed Accounts Posts: 216 ✭✭gogul


    if the complete namespace of tools is "Tools", you write "Using Tools;". If the namespace is "MutantFruit.Applications.NewApplication.Tools" you write "Using "MutantFruit.Applications.NewApplication.Tools;".

    Then you can call your class normally by writing: codeVerifier bob = new codeVerifier(); or whatever.

    I have it setup exactly as you specified above, but I am still getting "The type or namespace name 'Tools' could not be found". Did I mention that this was for a .aspx Page? Could that have any bearing on it running?


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Would you be able to either paste the relevant CS files here (wrapped in [ CODE ][ /CODE ] tags) or attach the files somewhere? If you've set it up like you think you have, it should be working fine.


  • Closed Accounts Posts: 216 ✭✭gogul


    Sure, here they are:

    Tools.cs
    namespace Tools
    {
            static class codeVerifier
            {
             ........
            }
    }
    


    Pages.cs
    
    using Tools;
    
    namespace Pages
    {
            public class DefaultPage : Page
            {
             	Tools.codeVerifier cv = new Tools.codeVerifier(); 
            }
    }
    

    The Pages.cs is the page that is called from the ASPX page.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Oops, I just gave that code as an example. If its a static class you don't have to create an instance of it. Soz :o

    Below is how you could call a method Foo() which returns a string on the codeVerifier static class.
    using Tools;
    
    namespace Pages
    {
            public class DefaultPage : Page
            {
             	string foo = Tools.codeVerifier.Foo(); 
            }
    }
    


  • Closed Accounts Posts: 216 ✭✭gogul


    Evil Phil wrote:
    Oops, I just gave that code as an example. If its a static class you don't have to create an instance of it. Soz :o

    Below is how you could call a method Foo() which returns a string on the codeVerifier static class.
    using Tools;
    
    namespace Pages
    {
            public class DefaultPage : Page
            {
             	string foo = Tools.codeVerifier.Foo(); 
            }
    }
    

    The problem is not with calling the methods from a different namespace, it's the actual "using Tools;" that's returning an error (The type or namespace name 'Tools' could not be found)


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Shouldn't you be declaring your static class as public static, and not just static?


  • Advertisement
  • Closed Accounts Posts: 216 ✭✭gogul


    bonkey wrote:
    Shouldn't you be declaring your static class as public static, and not just static?

    Maybe, but the problem still is with finding the namespace. I'll deal with permissions onceI can figure this out.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    gogul wrote:
    The problem is not with calling the methods from a different namespace, it's the actual "using Tools;" that's returning an error (The type or namespace name 'Tools' could not be found)

    Aye, I know. And the first example I gave (for illustration purposes) was incorrect.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    gogul wrote:
    Maybe, but the problem still is with finding the namespace. I'll deal with permissions onceI can figure this out.

    I'm not entirely sure (and don't have a machine to check on at the moment), but I don't think you can find an empty Namespace.

    If this static class is the only one in there, and its hidden from otehr classes on account of being private, then effectively that namespace doesn't exist for those other classes, right?

    I assume you can find other namespaces from your project, so its not a question of references?


  • Closed Accounts Posts: 216 ✭✭gogul


    bonkey wrote:
    I'm not entirely sure (and don't have a machine to check on at the moment), but I don't think you can find an empty Namespace.

    If this static class is the only one in there, and its hidden from otehr classes on account of being private, then effectively that namespace doesn't exist for those other classes, right?

    I assume you can find other namespaces from your project, so its not a question of references?

    Hmm, I have made the static classes public, but it hasnt changed much.
    I actually only have the 2 namespaces Tools, Pages, so maybe it is references. Like I said, I'm kinda new to this.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    I assume that both your CS files are in the same project and solution?


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    I don't know what version of C# your using, not sure about .NET Framework 2.0, but I'm using 1.1, and a static class doesn't exist.

    A class can only have certain modifiers, and static isn't one of them.

    If you want to create a class as static, look into creating a Singleton class.

    I tried creating a dummy project (VS 2003) using a Singleton with namespaces Tools and Pages, and it compiled fine.

    Here's a few examples: Singleton


  • Closed Accounts Posts: 216 ✭✭gogul


    No, I'm using a text editor to create my .cs and .aspx files. I have no solution or project files. My original post was to find a way to get these namespaces to see each other without being contained in such. The only way I had to create such was with using MS Visual C# 2005 and I'm not using this for these pages.

    Surely these pages don't have to be contained in a project. They run perfectly when called from their respective ASPX pages. I just wanted a way to remove the utilites that I use for different pages and have them callable from one Tools file.


  • Closed Accounts Posts: 216 ✭✭gogul


    silas wrote:
    I don't know what version of C# your using, not sure about .NET Framework 2.0, but I'm using 1.1, and a static class doesn't exist.
    ......
    QUOTE]

    Yeah, I'm using 2.0. Static classes work fine. I have had both .cs files merged and had no problems, but like I say above, I want all the utilities that I frequently use to be contained in one Tools namespace so they can all be easily called from any other .cs file.....or is the idea


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    gogul wrote:
    No, I'm using a text editor to create my .cs and .aspx files. I have no solution or project files. My original post was to find a way to get these namespaces to see each other without being contained in such. The only way I had to create such was with using MS Visual C# 2005 and I'm not using this for these pages.

    Surely these pages don't have to be contained in a project. They run perfectly when called from their respective ASPX pages. I just wanted a way to remove the utilites that I use for different pages and have them callable from one Tools file.
    Well if you're only using a text editor to create the files, and not a project or solution then, the compiler probably can't see the Tools.cs, so it can't load the namespace. This explains why it all worked for one file, when you had them merged.

    Why not just use VS 2005, it's a lot more easier to use than a text editor, and use a project/solution set up


  • Closed Accounts Posts: 216 ✭✭gogul


    I'd rather not use Visual C#. Ever since college I've had a real distaste for all Visual Studio apps.


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    what's your commandline for compiling then? Would you think of using SharpDevelop? Or if your the Linux kind, MonoDevelop?


  • Closed Accounts Posts: 216 ✭✭gogul


    I dont compile the files as such. They are compiled when the .NET pages call them. If they was a way for the .NET pages to call two files, then I think my problem would be solved. Alternatively, if there was a direct call from Pages.cs file to the Tools.cs file it would work. The static classes in Tools.cs would be compiled first and then I could call the methods in Tools.cs without any issues.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    All i know is that i have it working fine here on my side with Visual Web Dev express. In VS i have to put any globally visible CS file in a folder called "App_Code", are you doing that? Do you have to do that? Give it a shot...

    what i mean is create a subfolder called "App_Code" in your main webfolder (the one that contains .aspx files) and put your Tools.cs in there.


  • Closed Accounts Posts: 216 ✭✭gogul


    All i know is that i have it working fine here on my side with Visual Web Dev express. In VS i have to put any globally visible CS file in a folder called "App_Code", are you doing that? Do you have to do that? Give it a shot...

    what i mean is create a subfolder called "App_Code" in your main webfolder (the one that contains .aspx files) and put your Tools.cs in there.

    Yeah, that seems to to work. Class!! Let me run a full test on my files and l'll let you know how it goes!


  • Closed Accounts Posts: 216 ✭✭gogul


    Cheers Mutant_Fruit, that's exactly what I was after. I was able to seperate all my .cs files and have them all calling the Tools namespace from the App_Code directory. I didnt realise that this was a new feature in 2.0. No wonder it was so tough to find info on it.

    Thanks again
    gogul


Advertisement