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. how to call 1 class method or constructor in other class ?

how to call 1 class method or constructor in other class ?

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
13 Posts 5 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.
  • X Offline
    X Offline
    xingselex
    wrote on last edited by
    #1

    i have a class name DataConnection. and i want to call this in my form. so how to do with C# ? please give me some guide line ....

    M 1 Reply Last reply
    0
    • X xingselex

      i have a class name DataConnection. and i want to call this in my form. so how to do with C# ? please give me some guide line ....

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Well it all depends how you have them set up. Are your classes both in the same namespace? If so, then you can access the Class directly. Do you want to create an instance of the class? or have you already done that and what to access one of its functions? If you want to create an instance you can use

      DataConnection dc= new DataConnection();

      then call a function...

      dc.FunctionCall();

      from inside your form. Unless it is a static class in which case you cannot create an instance of it, but you can call any of its public methods like so...

      DataConnection.FunctionCall();

      Any help?

      Life goes very fast. Tomorrow, today is already yesterday.

      X 1 Reply Last reply
      0
      • M musefan

        Well it all depends how you have them set up. Are your classes both in the same namespace? If so, then you can access the Class directly. Do you want to create an instance of the class? or have you already done that and what to access one of its functions? If you want to create an instance you can use

        DataConnection dc= new DataConnection();

        then call a function...

        dc.FunctionCall();

        from inside your form. Unless it is a static class in which case you cannot create an instance of it, but you can call any of its public methods like so...

        DataConnection.FunctionCall();

        Any help?

        Life goes very fast. Tomorrow, today is already yesterday.

        X Offline
        X Offline
        xingselex
        wrote on last edited by
        #3

        yes. please tell me more detail ... i have a project name Stock. and it has a form name "form", and a class name DataConnection. in DataConnection has only property "S" as string. and why i cannot access to "S" directly by "string str = DataConnection.S" ? why need to create an instand of it first ?

        H M 2 Replies Last reply
        0
        • X xingselex

          yes. please tell me more detail ... i have a project name Stock. and it has a form name "form", and a class name DataConnection. in DataConnection has only property "S" as string. and why i cannot access to "S" directly by "string str = DataConnection.S" ? why need to create an instand of it first ?

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          You need to create an instance first because, until you do, nothing will exist. You need to make sure that S is declared public as well. Alternatively, you can declare S as public static, in which case you can use it as you currently do. In any event get yourself a beginners C# book and work through it. You will save yourself a lot of pain.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          X 1 Reply Last reply
          0
          • H Henry Minute

            You need to create an instance first because, until you do, nothing will exist. You need to make sure that S is declared public as well. Alternatively, you can declare S as public static, in which case you can use it as you currently do. In any event get yourself a beginners C# book and work through it. You will save yourself a lot of pain.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            X Offline
            X Offline
            xingselex
            wrote on last edited by
            #5

            is C# has readonly keyworld ? and how it use in declare a property in Class ?

            H L 2 Replies Last reply
            0
            • X xingselex

              yes. please tell me more detail ... i have a project name Stock. and it has a form name "form", and a class name DataConnection. in DataConnection has only property "S" as string. and why i cannot access to "S" directly by "string str = DataConnection.S" ? why need to create an instand of it first ?

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              As Henry has said, you can access it the way you are trying only if the S string is static and publicly available. Which I am guessing is not the case. ensure you DataConnection class is similar to the following...

              public class DataConnection
              {
              private string s = "default";//this is where the string value will be stored, assignment is not required
              public string S{//this is a property that you can access with external class to get/set the value of the private string s
              {
              get{return s;}
              set{s = value;}
              }
              }

              then in your main form you need to create an instance of your DataConnection class in which you can access the value of S, like so...

              DataConnection dc = new DataConnection();
              dc.S = "new value";
              MessageBox.Show(dc.S);

              you need to ensure that your DataConnection instance is located in an appropriate place depending on where you want to access the data from i.e. mulitply functions would mean you should set it at a class level

              Life goes very fast. Tomorrow, today is already yesterday.

              1 Reply Last reply
              0
              • X xingselex

                is C# has readonly keyworld ? and how it use in declare a property in Class ?

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                xingselex wrote:

                is C# has readonly keyworld ? and how it use in declare a property in Class

                1. Yes. 2. public/protected/private (delete as applicable) readonly string S = string.Empty;

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                X 1 Reply Last reply
                0
                • H Henry Minute

                  xingselex wrote:

                  is C# has readonly keyworld ? and how it use in declare a property in Class

                  1. Yes. 2. public/protected/private (delete as applicable) readonly string S = string.Empty;

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  X Offline
                  X Offline
                  xingselex
                  wrote on last edited by
                  #8

                  ok thank Henry Minute :)

                  X 1 Reply Last reply
                  0
                  • X xingselex

                    ok thank Henry Minute :)

                    X Offline
                    X Offline
                    xingselex
                    wrote on last edited by
                    #9

                    my code is like this bellow . and why it message that "the modifier 'readonly' is not valid for this item " . what wrong with it ? sorry for my question. i'm just start to learn about coding... using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace Stock { class DataConnection { private string _ConnectStr = ""; public readonly string ConnectStr { get { return _ConnectStr; } set { _ConnectStr = value; } } public DataConnection() { string str = "Data Source=sokheng;Initial Catalog=stock;User Id=sa;Password="; try { System .Data .SqlClient .SqlConnection CN = new System.Data.SqlClient.SqlConnection() ; CN.ConnectionString = str; CN.Open (); _ConnectStr = str; CN.Close (); } catch(Exception ex) { } } } }

                    L 1 Reply Last reply
                    0
                    • X xingselex

                      is C# has readonly keyworld ? and how it use in declare a property in Class ?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      If it's a property you don't even need the readonly keyword, you could make the getter public and the setter private (or protected)

                      1 Reply Last reply
                      0
                      • X xingselex

                        my code is like this bellow . and why it message that "the modifier 'readonly' is not valid for this item " . what wrong with it ? sorry for my question. i'm just start to learn about coding... using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace Stock { class DataConnection { private string _ConnectStr = ""; public readonly string ConnectStr { get { return _ConnectStr; } set { _ConnectStr = value; } } public DataConnection() { string str = "Data Source=sokheng;Initial Catalog=stock;User Id=sa;Password="; try { System .Data .SqlClient .SqlConnection CN = new System.Data.SqlClient.SqlConnection() ; CN.ConnectionString = str; CN.Open (); _ConnectStr = str; CN.Close (); } catch(Exception ex) { } } } }

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        You mixed "readonly" with a property, you could do this:

                        public string ConnectStr
                        {
                        get
                        {
                        return _ConnectStr;
                        }
                        private set
                        {
                        _ConnectStr = value;
                        }
                        }

                        X 1 Reply Last reply
                        0
                        • L Lost User

                          You mixed "readonly" with a property, you could do this:

                          public string ConnectStr
                          {
                          get
                          {
                          return _ConnectStr;
                          }
                          private set
                          {
                          _ConnectStr = value;
                          }
                          }

                          X Offline
                          X Offline
                          xingselex
                          wrote on last edited by
                          #12

                          why i cannot use "MessageBox.show" in my class ? i want to show when catch an error ...

                          OriginalGriffO 1 Reply Last reply
                          0
                          • X xingselex

                            why i cannot use "MessageBox.show" in my class ? i want to show when catch an error ...

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #13

                            Your name is Rajdeep.NET and I claim my £5!

                            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            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