Returning an object of my values from a method call

stormist
stormist used Ask the Experts™
on
I have a method that needs to return a series of data that looks something like

1, namehere
2, anothernamehere
1, anothername here
etc

(int, string)

An array would be perfect but as far as I can tell the parts of an array have to be of the same type? A regular string array won't work because the first number can repeat many times. How would i go about returning these values?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what you want to do is encapsulate (int, string) in an object and then return an arraylist<yourObject>
ie your object would look like this
class myObject
{
      private string mString;
      private int mInt;
      public myObject(int i,string s)
      {
           mString = s;
           mInt = i;
      }
      public int MyInt
      {
            get
            {
                  return mInt;
            }
            set
            {
                  mInt = value;
            }
      }

      public string MyString
      {
            get
            {
                  return mString;
            }
            set
            {
                  mString = value;
            }
      }
}

Commented:
instead of a class, use a struct (structure), it is a bit more lightweight than a class, but still powerful enough to hold properties.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

i would reccomend using a class anyway, its better programming practice and they will compile to the same thing. Classes provide stronger options for extention and typematching

Commented:
yeah, but when they're not needed, why use something that is more powerful, especially if he's going to have tons of them in an ArrayList?
because its better practise, just like you should include a culture whenever you convert a date to a string even though it might not be 'needed', the only advantage of using a struct is that its quicker to write and the actual typing time isnt really a big deal in codeing....

Commented:
I'm reading something that says it might have a smaller footprint for these types of things. Actually it hinges on whether or not each one will be unique, or if there may be possible duplicates. If there are possible duplicates, then a struct is the way to go, it is smaller in memory.
Commented:
Here is some reading info on classes vs structs. I believe if structs are used, and stored IN a class (ArrayList), that it would be pretty good performance wise...because each time a struct is created, it is entered into the ArrayList (on the heap), and then it can be destroyed, so you'd never run out of room on the stack.

http://blog.devstone.com/aaron/archive/2004/06/27/205.aspx

Author

Commented:
In this case the values are already set within one class, and called by a 2nd class. I tried returning an array list with a structure but was having trouble with it. Do I have to redefine the structure in the calling class? if class one has the values like follows

 struct Entry
        {
            public int Number;
            public string Name;          
        }

        private ArrayList Data = new ArrayList();

then within my method:
Entry NameData;
NameData.Number = 1;
NameData.Name = "test";
Data.Add(NameData);
NameData.Number = 2;
NameData.Name = "test2";
Data.Add(NameData);
NameData.Number = 1;
NameData.Name = "test3";
Data.Add(NameData);


can you please show me the proper code to retrieve the values at the calling class? Assume the method name is GetData. So i declare an arraylist locally within the 2nd class, assign that local arraylist the value of what the method returns.. Then what?

Thanks for both your help
foreach(Entry e in GetData())
{
         //do stuff with your entry
}
might be better if you go
ArratList<Entry> ents = GetData();
foreach(Entry e in ents)
{
         //do stuff with your entry
}

Author

Commented:
Does entry have to be declared in receiving class for this to work?
you can probably get at it by going using yourNamespace.Yourclass.Entry;
but personally i would have put it in a class and a seperate file and just linked it with using yourNamespace.Entry; which is one of the bonuses of using classes not structs

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial