Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Public Class

Public Class

Scheduled Pinned Locked Moved C#
csharphelpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nooie
    wrote on last edited by
    #1

    Hi all, I am still very new to C#. I have created a Windows Application in VS2005, and added a new class (Class1) that I created. I've then added another Form to the Application. My problem is that I want to declare the Class (Class1) so that I can use it in both Forms. Where about's in the Application would I do this please? Many Thanks Tony

    C 1 Reply Last reply
    0
    • N Nooie

      Hi all, I am still very new to C#. I have created a Windows Application in VS2005, and added a new class (Class1) that I created. I've then added another Form to the Application. My problem is that I want to declare the Class (Class1) so that I can use it in both Forms. Where about's in the Application would I do this please? Many Thanks Tony

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Nooie wrote:

      Application

      You are using "Application" as a proper noun here. Is there any significance in that? Or should the initial "A" be lowercased back to a common noun?

      Nooie wrote:

      My problem is that I want to declare the Class (Class1) so that I can use it in both Forms. Where about's in the Application would I do this please?

      It really depends on what you want to do with the class. Class1 is not a very good name, it doesn't decribe what the class represents. You could create an instance of the class in the first form then pass it to the second form in its constructor (you'll have to modify the constructor of the second form to accomodate the object). Or you could do one of many other things. What is the purpose of Class1?


      Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      N 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Nooie wrote:

        Application

        You are using "Application" as a proper noun here. Is there any significance in that? Or should the initial "A" be lowercased back to a common noun?

        Nooie wrote:

        My problem is that I want to declare the Class (Class1) so that I can use it in both Forms. Where about's in the Application would I do this please?

        It really depends on what you want to do with the class. Class1 is not a very good name, it doesn't decribe what the class represents. You could create an instance of the class in the first form then pass it to the second form in its constructor (you'll have to modify the constructor of the second form to accomodate the object). Or you could do one of many other things. What is the purpose of Class1?


        Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        N Offline
        N Offline
        Nooie
        wrote on last edited by
        #3

        Hi Sorry I was using simple names to try and no over complicate it. Basically I created a new windows form project using VS2005. I then created a new Class called NavConnection. This class contains details such as Servername, databasename etc... Next I added application settings which match the props in the class NavConnection. From the Main Form I added a new MenuStrip and a call to a new form I created. On the new form I have textboxes that mimic the Usersettings/Navconnection. The idea being that when this form is run it will populate the textbox's with the data from the class. Then when the app is closed the User Settings will be saved from the Class NavConnection. So when I run the app I want to create an instance that it used through out the app. Does that make more sense? It may not be the best way to do this but I am inventing little projects to help learn C# Cheers for your help. Tony

        C C 2 Replies Last reply
        0
        • N Nooie

          Hi Sorry I was using simple names to try and no over complicate it. Basically I created a new windows form project using VS2005. I then created a new Class called NavConnection. This class contains details such as Servername, databasename etc... Next I added application settings which match the props in the class NavConnection. From the Main Form I added a new MenuStrip and a call to a new form I created. On the new form I have textboxes that mimic the Usersettings/Navconnection. The idea being that when this form is run it will populate the textbox's with the data from the class. Then when the app is closed the User Settings will be saved from the Class NavConnection. So when I run the app I want to create an instance that it used through out the app. Does that make more sense? It may not be the best way to do this but I am inventing little projects to help learn C# Cheers for your help. Tony

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You need to create an instance of your class as a member. As in NavConnection conn = new NavConnection();

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          N 1 Reply Last reply
          0
          • C Christian Graus

            You need to create an instance of your class as a member. As in NavConnection conn = new NavConnection();

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            N Offline
            N Offline
            Nooie
            wrote on last edited by
            #5

            Hi Christian, Thats kind of what I was asking ;-) where do I put it? I've tried a few places, but when I go to code on the second form using the instance conn, intellisense does not pick it up. Tony

            P 1 Reply Last reply
            0
            • N Nooie

              Hi Christian, Thats kind of what I was asking ;-) where do I put it? I've tried a few places, but when I go to code on the second form using the instance conn, intellisense does not pick it up. Tony

              P Offline
              P Offline
              phantanagu
              wrote on last edited by
              #6

              Hi, You write "public static MyClass myClass1 = new MyClass () outside "Form1_Load()" or "Form2_Load()" to use in both forms. Hope my help.

              C 1 Reply Last reply
              0
              • P phantanagu

                Hi, You write "public static MyClass myClass1 = new MyClass () outside "Form1_Load()" or "Form2_Load()" to use in both forms. Hope my help.

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Public fields are generally a bad idea.


                Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                1 Reply Last reply
                0
                • N Nooie

                  Hi Sorry I was using simple names to try and no over complicate it. Basically I created a new windows form project using VS2005. I then created a new Class called NavConnection. This class contains details such as Servername, databasename etc... Next I added application settings which match the props in the class NavConnection. From the Main Form I added a new MenuStrip and a call to a new form I created. On the new form I have textboxes that mimic the Usersettings/Navconnection. The idea being that when this form is run it will populate the textbox's with the data from the class. Then when the app is closed the User Settings will be saved from the Class NavConnection. So when I run the app I want to create an instance that it used through out the app. Does that make more sense? It may not be the best way to do this but I am inventing little projects to help learn C# Cheers for your help. Tony

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  NavConnection sounds like it should be a Singleton (i.e. There exists only one instance of it in the application) If that is correct, you can create the class as static (i.e. put static on its class definition and all fields, properties and methods). Then you don't need to store an instance of the class anywhere. You can reference a method on it like this:

                  NavConnection.ServerName;
                  NavConnection.SaveSettings();

                  The class might look something like this:

                  public static class NavConnection
                  {
                  private static string serverName;

                  public static string ServerName
                  {
                      get { return this.serverName; }
                      set { this.serverName = value; }   
                  }
                  
                  public static void SaveSettings()
                  {
                      // Put code to save the settings in here.
                  }
                  

                  }


                  Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups