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. Getting a value from a constructor

Getting a value from a constructor

Scheduled Pinned Locked Moved C#
questiondata-structures
13 Posts 6 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.
  • A Offline
    A Offline
    av7254
    wrote on last edited by
    #1

    Hello, I created an object "polzisce" from a class called Matrika(this is class that creates two dimensional array) and it's constructor takes two parameters. Those two parameters are actualy X an Y axis and they are not fixed size. public class Matrika { //constructor public Matrika(int x, int y) { int[,] matrika = new int[x, y]; } } static void Main(string[] args) { Matrika polzisce = new Matrika(5, 5); } The question is: Is object "polzisce" actualy two dimensional array after it is created? How can i get that two-dimensional array "matrika" from constructor? Thanks! Alen.

    P J 3 Replies Last reply
    0
    • A av7254

      Hello, I created an object "polzisce" from a class called Matrika(this is class that creates two dimensional array) and it's constructor takes two parameters. Those two parameters are actualy X an Y axis and they are not fixed size. public class Matrika { //constructor public Matrika(int x, int y) { int[,] matrika = new int[x, y]; } } static void Main(string[] args) { Matrika polzisce = new Matrika(5, 5); } The question is: Is object "polzisce" actualy two dimensional array after it is created? How can i get that two-dimensional array "matrika" from constructor? Thanks! Alen.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      av7254 wrote:

      Is object "polzisce" actualy two dimensional array after it is created?

      No, it HAS_A two-dimensional array.

      av7254 wrote:

      How can i get that two-dimensional array "matrika" from constructor?

      If that's what you want, then why create Matrika at all? On the other hand, you could write a converter for it. But why not simply write a static method that takes the two parameters and creates the array?

      A 1 Reply Last reply
      0
      • A av7254

        Hello, I created an object "polzisce" from a class called Matrika(this is class that creates two dimensional array) and it's constructor takes two parameters. Those two parameters are actualy X an Y axis and they are not fixed size. public class Matrika { //constructor public Matrika(int x, int y) { int[,] matrika = new int[x, y]; } } static void Main(string[] args) { Matrika polzisce = new Matrika(5, 5); } The question is: Is object "polzisce" actualy two dimensional array after it is created? How can i get that two-dimensional array "matrika" from constructor? Thanks! Alen.

        J Offline
        J Offline
        jayantbramhankar
        wrote on last edited by
        #3

        polzisce isn't two dimensional array.. matrika is two dimesional, and if you want to access it make it member of class and write a method..

        P A 2 Replies Last reply
        0
        • J jayantbramhankar

          polzisce isn't two dimensional array.. matrika is two dimesional, and if you want to access it make it member of class and write a method..

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          jayantbramhankar wrote:

          matrika is two dimesional

          No it isn't.

          modified on Friday, March 19, 2010 3:41 PM

          A 1 Reply Last reply
          0
          • P PIEBALDconsult

            jayantbramhankar wrote:

            matrika is two dimesional

            No it isn't.

            modified on Friday, March 19, 2010 3:41 PM

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #5

            int[,] matrika = new int[x, y];

            It's not? Granted, there is no word "dimesional", but it seems 2D to me.

            [Forum Guidelines]

            P 1 Reply Last reply
            0
            • A AspDotNetDev

              int[,] matrika = new int[x, y];

              It's not? Granted, there is no word "dimesional", but it seems 2D to me.

              [Forum Guidelines]

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

              Sorry, I didn't see the lowercase m.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                av7254 wrote:

                Is object "polzisce" actualy two dimensional array after it is created?

                No, it HAS_A two-dimensional array.

                av7254 wrote:

                How can i get that two-dimensional array "matrika" from constructor?

                If that's what you want, then why create Matrika at all? On the other hand, you could write a converter for it. But why not simply write a static method that takes the two parameters and creates the array?

                A Offline
                A Offline
                av7254
                wrote on last edited by
                #7

                your way is simplier but, i want to do it like this. all i want is to get the x and y value from constructor. Use cases: Matrika polzisce = new Matrika(10,20); Matrika polzisce = new Matrika(5,2); maybe: Matrika polzisce = new Matrika(1000,1000); in this case: Matrika polzisce = new Matrika(5,5); Constructor runes first when object is created and contains varibles x=5 and y=5(actualy limits of array). I need those two to create two-dimensional array. I don't know how to access those varibles in the constructor, that i can create 2d array and that object "polzisce" will contain 2d array 5x5 Tnx for all other posts! I appreciate!

                OriginalGriffO 1 Reply Last reply
                0
                • J jayantbramhankar

                  polzisce isn't two dimensional array.. matrika is two dimesional, and if you want to access it make it member of class and write a method..

                  A Offline
                  A Offline
                  av7254
                  wrote on last edited by
                  #8

                  So, how do i make it member of class, that will conatain x=5 and y=5 in this case. "polzisce" has to be object wich contains a 2d array wich is specified with this Matrika polzisce = new Matrika(5,5); Tnx for your answers :)

                  1 Reply Last reply
                  0
                  • A av7254

                    your way is simplier but, i want to do it like this. all i want is to get the x and y value from constructor. Use cases: Matrika polzisce = new Matrika(10,20); Matrika polzisce = new Matrika(5,2); maybe: Matrika polzisce = new Matrika(1000,1000); in this case: Matrika polzisce = new Matrika(5,5); Constructor runes first when object is created and contains varibles x=5 and y=5(actualy limits of array). I need those two to create two-dimensional array. I don't know how to access those varibles in the constructor, that i can create 2d array and that object "polzisce" will contain 2d array 5x5 Tnx for all other posts! I appreciate!

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

                    Not exactly a difficult thing to do: it is pretty common: Using your original code:

                    public class Matrika
                    {
                    public int[,] arrayOfInts;
                    public Matrika(int x, int y)
                    {
                    arrayOfInts = new int[x, y];
                    }
                    }

                    static void Main(string[] args)
                    {
                    Matrika polzisce = new Matrika(5, 5);
                    for (int x = 0; x < 5; x++)
                    {
                    for (int y = 0; y < 5; y++)
                    {
                    Console.WriteLine(polzisce.arrayOfInts[x, y]);
                    }
                    }
                    }

                    Note that it is not considered good practice to declare the array as public: I did this only to simplify this example. Note also that the ints are not initialised, so they will all be zero.

                    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                    "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

                    A 1 Reply Last reply
                    0
                    • A av7254

                      Hello, I created an object "polzisce" from a class called Matrika(this is class that creates two dimensional array) and it's constructor takes two parameters. Those two parameters are actualy X an Y axis and they are not fixed size. public class Matrika { //constructor public Matrika(int x, int y) { int[,] matrika = new int[x, y]; } } static void Main(string[] args) { Matrika polzisce = new Matrika(5, 5); } The question is: Is object "polzisce" actualy two dimensional array after it is created? How can i get that two-dimensional array "matrika" from constructor? Thanks! Alen.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      I'm still not sure what you want, but how about this:

                      public class Matrika
                      {
                      private readonly int x ;
                      private readonly int y ;

                      public Matrika
                      (
                          int X
                      ,
                          int Y
                      )
                      {
                          this.x = X ;
                          this.y = Y ;
                      
                          return ;
                      }
                      
                      public int\[,\]
                      GetArray
                      (
                      )
                      {
                          return ( new int \[ this.x , this.y \] ) ;
                      }
                      

                      }

                      Matrika m = new Matrika ( 5 , 5 ) ;

                      int[,] polzisce = m.GetArray() ;

                      1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Not exactly a difficult thing to do: it is pretty common: Using your original code:

                        public class Matrika
                        {
                        public int[,] arrayOfInts;
                        public Matrika(int x, int y)
                        {
                        arrayOfInts = new int[x, y];
                        }
                        }

                        static void Main(string[] args)
                        {
                        Matrika polzisce = new Matrika(5, 5);
                        for (int x = 0; x < 5; x++)
                        {
                        for (int y = 0; y < 5; y++)
                        {
                        Console.WriteLine(polzisce.arrayOfInts[x, y]);
                        }
                        }
                        }

                        Note that it is not considered good practice to declare the array as public: I did this only to simplify this example. Note also that the ints are not initialised, so they will all be zero.

                        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                        A Offline
                        A Offline
                        av7254
                        wrote on last edited by
                        #11

                        Thanks! i have solved this thing it is realy preety simple :) public class Matrika { public int mat_x; public int mat_y; public Array matrika; public Matrika(int x, int y) { //i needed to save separated x and y and create 2D array mat_x = x; mat_y = y; matrika = Array.CreateInstance(typeof(int), x, y); { But, why did you say that public atributes are no ok?

                        OriginalGriffO P 2 Replies Last reply
                        0
                        • A av7254

                          Thanks! i have solved this thing it is realy preety simple :) public class Matrika { public int mat_x; public int mat_y; public Array matrika; public Matrika(int x, int y) { //i needed to save separated x and y and create 2D array mat_x = x; mat_y = y; matrika = Array.CreateInstance(typeof(int), x, y); { But, why did you say that public atributes are no ok?

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

                          av7254 wrote:

                          why did you say that public atributes are no ok

                          This may take a little while to explain: Assume you have a class: MyClass which contains a string and an int.

                          public class MyClass
                          {
                          public string UserName;
                          public int UserID;
                          ....
                          }

                          You write your class, you test it, you are happy it works. So happy, you use it to handle all the user identity work in your entire app! Then the boss comes along, and says he wants the user name to be in two parts in the database: First name, and second name. Oh, and the userID is not an int, it's going to be a GUID. How many classes do you have to change to implement this? How much code to you have to change, and test, and document? If instead you had written the class as:

                          public class MyClass
                          {
                          private string userName;
                          public string UserName
                          {
                          get { return userName; }
                          set { userName = value; }
                          }
                          private int userID;
                          public int UserID
                          {
                          get { return userID; }
                          set { userID = value; }
                          }
                          }

                          When your dumb boss comes along with his changes, how much rework is there to do? Only the one class, because you can change the internals of MyClass without affecting the outside world:

                          public class MyClass
                          {
                          private string firstName;
                          private string lastName;
                          public string UserName
                          {
                          get { return firstName + " " + lastName; }
                          set
                          {
                          string[] names = value.Split(' ');
                          if (names.Length != 2)
                          {
                          throw new ApplicationException("Name must have first and last components");
                          }
                          firstName = names[0];
                          lastName = names[1];
                          }
                          }
                          private GUID userID;
                          public int UserID
                          {
                          get { return userID.GetHash(); }
                          set { userID = GetGUID(value); }
                          }
                          }

                          This is one of the cornerstones of OOP - encapsulation. Never expose your internals more than you have to! When you are starting off, it seems like a lot of fussing about over nothing! But it very quickly becomes second nature, and does give real benefits - almost from day one. If nothing else, it forces you to think about how your class will be used, and what you want to expose to the outside world. Very often, this affects the internal design in a good way, by the realization that a small chan

                          "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
                          • A av7254

                            Thanks! i have solved this thing it is realy preety simple :) public class Matrika { public int mat_x; public int mat_y; public Array matrika; public Matrika(int x, int y) { //i needed to save separated x and y and create 2D array mat_x = x; mat_y = y; matrika = Array.CreateInstance(typeof(int), x, y); { But, why did you say that public atributes are no ok?

                            P Offline
                            P Offline
                            Phillip Donegan
                            wrote on last edited by
                            #13

                            Maybe you should google encapsulation, since I think that is what your trying to do.

                            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