Getting a value from a constructor
-
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.
-
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.
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?
-
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.
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..
-
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..
jayantbramhankar wrote:
matrika is two dimesional
No it isn't.
modified on Friday, March 19, 2010 3:41 PM
-
jayantbramhankar wrote:
matrika is two dimesional
No it isn't.
modified on Friday, March 19, 2010 3:41 PM
int[,] matrika = new int[x, y];
It's not? Granted, there is no word "dimesional", but it seems 2D to me.
-
int[,] matrika = new int[x, y];
It's not? Granted, there is no word "dimesional", but it seems 2D to me.
Sorry, I didn't see the lowercase m.
-
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?
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!
-
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..
-
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!
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
-
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.
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() ;
-
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
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? -
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?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
-
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?Maybe you should google encapsulation, since I think that is what your trying to do.