.Count, not in namespace system.
-
In a previous post I was informed that I could count the number of elements in an array using. // elements is a previously declared array elements.Count however .Count isnt available I looked it up in the object viewer and it looks like its a part of the system namespace which I have included in my code. Anyone know why it might not be showing up? and also if elements.Count and elements.Length return the same value?
It is an array then use
Length
. If it any other type of collection then useCount
.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
It is an array then use
Length
. If it any other type of collection then useCount
.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
Colin Angus Mackay wrote:
It is an array then use Length. If it any other type of collection then use Count
Kinda sucks there is this "inconsistency". Yes, sure, logicaly array has lenght not count and collection doesn't have lenght but count, but... What about
NumberOfElements
or something for both.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
In a previous post I was informed that I could count the number of elements in an array using. // elements is a previously declared array elements.Count however .Count isnt available I looked it up in the object viewer and it looks like its a part of the system namespace which I have included in my code. Anyone know why it might not be showing up? and also if elements.Count and elements.Length return the same value?
Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;
-
Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;
jblouir wrote:
but im still stuck with trying to add an object to a global object array.
What is the problem? If fixed lenght of array is problem, use collection.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;
jblouir wrote:
im still stuck with trying to add an object to a global object array
You can't add objects to an array - They have a fixed length. You may wish to try an ArrayList. Also, global variables are rarely justified in an object oriented language. You may wish to have a rethink about your design.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
Well I was unstructed to use this... string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; elements is an array, when .Count didnt work I assumed it was because I missing a using namespace .Length appears to work but im still stuck with trying to add an object to a global object array. intIndex, strRoom,strDesc,strLook, i are all variables, descriptor being the object name RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.arrRoomData.SetValue(descriptor,i) i++;
Hi, some remarks on your code:
jblouir wrote:
string [] elements = line.split(new char [] { ',' } );
can be simplified to string [] elements = line.Split(','); because of the params keyword in one of the Split overloads
jblouir wrote:
if (elements.Count > 0)
I see no way elements.Count could be zero; if no comma's are present, everything will be returned as the first and only string in elements[]. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, some remarks on your code:
jblouir wrote:
string [] elements = line.split(new char [] { ',' } );
can be simplified to string [] elements = line.Split(','); because of the params keyword in one of the Split overloads
jblouir wrote:
if (elements.Count > 0)
I see no way elements.Count could be zero; if no comma's are present, everything will be returned as the first and only string in elements[]. :)
Luc Pattyn [My Articles] [Forum Guidelines]
Luc Pattyn wrote:
string [] elements = line.Split(',');
He's trying to use code that I wrote off the cuff in an earlier answer. I have found that the above doesn't work for me, I must be doing something wrong.
Luc Pattyn wrote:
I see no way elements.Count could be zero;
Good point, I was trying to illustrate the need to code defensively, and not assume an array length, but in this case, you're right.
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 )
-
jblouir wrote:
im still stuck with trying to add an object to a global object array
You can't add objects to an array - They have a fixed length. You may wish to try an ArrayList. Also, global variables are rarely justified in an object oriented language. You may wish to have a rethink about your design.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
Well I have a game loop in void Main, and I have a few methods, one being initialise which sits in the loop that repeats until input = exit. the loop needs the input variable and initialise needs the input variable they need to be the same, and the best way I found of doing that is using a global variable. I have been literally programming in C# for less than a week, I am doing things that work and learning as I go, if there is a better way to do it then I implement the new method. I had thought that I could make a method that would return input but couldnt work it out at the time. // Edit And yes, the internet is my only resource at the moment, when I get the money to spend ill buy a book on C#.
-
How can a property be part of the System namespace? Elements.Count won't ever return anything, so do yourself a favour and use the property that does exist, elements.Length
-
Forgive me if I am wrong but dont all the different classes such as array and int etc come from namespaces? Then .Count is derived from array which is part of system, ok so its a property, but if you dont have proper namespace it wont be there.
Count is a property, it isn't found in a namespace but on a class, and the class will be found in a namespace.