How to write a class, where one of its attributes is an Array?
-
Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.
class Testclass
{
........private int\[\] \_number; public int number { get { return \_number\[\]; } set { \_number\[\] = value; } } ........ }
2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public
public int[] number;
Although I declare a new Object in the Form class (it should be a windows App)Testclass Myclass = new Testclass();
the programme crashesMyclass.number[i] = i + 2;
saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me... -
Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.
class Testclass
{
........private int\[\] \_number; public int number { get { return \_number\[\]; } set { \_number\[\] = value; } } ........ }
2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public
public int[] number;
Although I declare a new Object in the Form class (it should be a windows App)Testclass Myclass = new Testclass();
the programme crashesMyclass.number[i] = i + 2;
saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...There are a few things here. First of all, the return type from number would be int[] not int - the two are not comparable. Secondly, you don't return _number[], you return _number. Finally, why use an array at all? Arrays have the nasty habit of becoming complicated to manage - why not look into a generic List? So this becomes:
class TestClass
{
public TestClass()
{
Number = new List<int>();
}
public List<int> Number { get; set; }
}I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.
class Testclass
{
........private int\[\] \_number; public int number { get { return \_number\[\]; } set { \_number\[\] = value; } } ........ }
2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public
public int[] number;
Although I declare a new Object in the Form class (it should be a windows App)Testclass Myclass = new Testclass();
the programme crashesMyclass.number[i] = i + 2;
saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me... -
Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.
class Testclass
{
........private int\[\] \_number; public int number { get { return \_number\[\]; } set { \_number\[\] = value; } } ........ }
2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public
public int[] number;
Although I declare a new Object in the Form class (it should be a windows App)Testclass Myclass = new Testclass();
the programme crashesMyclass.number[i] = i + 2;
saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...This is all you need:
public class TestClass
{
public int[] Numbers { get; set; }
}However, I would probably use a List in the following form:
public class NumbersList : List<int>
{
// You don't have to put anything in the class if you don't want/need
// to. It's just to clean up the allocation and type references because
// typing "private NumbersList numbers; is cleaner than typing
// "private List<int> numbers;" (at least, that's the way I see it).
}...which would allow you then to do this:
public class TestClass
{
public NumbersList Numbers { get; set; }
}...as well as being much more flexible than a mere array.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
Hi there, I am willing to declare a class, where one of its attributes has to be an Array of integers. 1st question: How should I write the set and get methods, cause the following is not working.
class Testclass
{
........private int\[\] \_number; public int number { get { return \_number\[\]; } set { \_number\[\] = value; } } ........ }
2nd question: Not knowing how to deal with the above methods ani in order to advance into coding I declared the attribute as public
public int[] number;
Although I declare a new Object in the Form class (it should be a windows App)Testclass Myclass = new Testclass();
the programme crashesMyclass.number[i] = i + 2;
saying that System.NullReferenceException was unhandled and asking me to create a new object instance for Myclass.number[i] A bit confusing for me...Ditto what others have said regarding arrays. Personally I'd only use an int[] if it is being initialised once, and then will not have elements added or removed. That could be useful for certain specific applications. For example...
public class Simple { private int\[\] \_int = { 1, 2, 3, 4, 5 }; public int\[\] MyInt { get { return \_int; } } } private static void TestSimple() { Simple s = new Simple(); s.MyInt\[0\] += 1; }
...is fine. But as others have said if the size of the array is to be dynamic (items being added) I'd use say a List<int> probably.
-
Ditto what others have said regarding arrays. Personally I'd only use an int[] if it is being initialised once, and then will not have elements added or removed. That could be useful for certain specific applications. For example...
public class Simple { private int\[\] \_int = { 1, 2, 3, 4, 5 }; public int\[\] MyInt { get { return \_int; } } } private static void TestSimple() { Simple s = new Simple(); s.MyInt\[0\] += 1; }
...is fine. But as others have said if the size of the array is to be dynamic (items being added) I'd use say a List<int> probably.
-
There are a few things here. First of all, the return type from number would be int[] not int - the two are not comparable. Secondly, you don't return _number[], you return _number. Finally, why use an array at all? Arrays have the nasty habit of becoming complicated to manage - why not look into a generic List? So this becomes:
class TestClass
{
public TestClass()
{
Number = new List<int>();
}
public List<int> Number { get; set; }
}I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
Is this a different list than an ArrayList? http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=VS.100).aspx