Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about arrays

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
How can I create an array that I am not sure how many items are going into it.
Code:
 string[] myArray = new string[1];
1 item can go into the array
How can I declare an array if I am not sure how big I should make it.

Thank you,
Timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
OK, I am trying to put an object into an arraylist and I am not sure how to do it. Here is what I have (it dosnt work)
Code:
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Foo f = new Foo();
            f.lName = "Jones";
            f.fName = "Tom";

            RtnArrayList rtn = new RtnArrayList();
            ArrayList ar1 =  rtn.ArryList(f);
            WhatArrLst show = new WhatArrLst();
            show.ShowMe();

        }
    }
    class Foo
    {
        public int id;
        public string fName;
        public string lName;
        
        public void RtnFoo(int gID, string gFname, string gLname)
        {
            this.fName  = gFname;
            this.lName  = gLname;
            this.id     = gID;
        }
        
    }
    class RtnArrayList
    {
        ArrayList al = new ArrayList();
        public object ArryList(object obj)
        {
            this.al = obj;
            return obj;
        }
    }
    class WhatArrLst
    {
        public void ShowMe(ArrayList alst)
        {
            Console.WriteLine(alst[0].lName);
        }
    }
}
I am trying to create a collection of objects. Is there an easier way to do this?

Thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
I would just use the List<Object> someobject = new List<Object>();
so then you could do a someobject.Add(something);
Then you can do a foreach loop to iterate through it.
 
Don't use Arraylist. That is not the same as a List<object>.

There are a lot of resources on the web for this. Fortunate for you, I just went through this so it is fresh in my mind. The first thing you need to do is create the list class. Here's the code I'm currently working with. Note there is one overload (two classes with the same name and different parameters):

public class Contexts
{
public string contextType;
public object startDate;
public object endDate;
public string name;
public string heading;
public int period;

public Contexts(string contextType, object endDate, string name, string heading, int period)
{
this.contextType = contextType;
this.endDate = endDate;
this.name = name;
this.heading = heading;
this.period = period;
}

public Contexts(string contextType, object startDate, object endDate, string name, string heading, int period)
{
this.contextType = contextType;
this.startDate = startDate;
this.endDate = endDate;
this.name = name;
this.heading = heading;
this.period = period;
}
}


Note that 'startDate' and 'endDate' are objects. The cool thing about lists is that you can put ANYTHING in them.

Here is the method that uses the list. The 'ListContexts' method has an output type of List<Contexts>:

List<Contexts> lst = ListContexts(dtsInstance, aConnection);

Here is the ListContexts method:

public static List<Contexts> ListContexts(XmlDocument dtsInstance, OleDbConnection aConnection)
{
List<Contexts> ctx = new List<Contexts>();
XmlNodeList contextList = dtsInstance.GetElementsByTagName("xbrli:context");
foreach (XmlElement contextElm in contextList)
{
string contextID = contextElm.GetAttribute("id");
XmlElement xbrliEntity = contextElm["xbrli:entity"];
XmlElement xbrliPeriod = contextElm["xbrli:period"];

XmlElement CIK = xbrliEntity["xbrli:identifier"];
XmlElement instant = xbrliPeriod["xbrli:instant"];
XmlElement startDate = xbrliPeriod["xbrli:startDate"];
XmlElement endDate = xbrliPeriod["xbrli:endDate"];

string cikValue = CIK.InnerText;
string startDateValue = "Null";
string endDateValue = "Null";
string instantValue = "Null";
string periodValue = "";
string heading = "heading";

DateTime startDateDate = new DateTime();
DateTime endDateDate = new DateTime();
DateTime instantDate = new DateTime();

DateTime dt;
//List<Contexts> ctx = new List<Contexts>();

if (instant != null)
{
instantValue = instant.InnerText;
periodValue = "instance";
dt = DateTime.ParseExact(instantValue, "yyyy-MM-dd", null);
instantValue = "'" + dt.ToShortDateString() + "'";
instantDate = dt;
heading = instantDate.ToString("MM/dd/yyyy");
ctx.Add(new Contexts(periodValue,instantDate,contextID,heading,100));
}
else
{
startDateValue = startDate.InnerText;
dt = DateTime.ParseExact(startDateValue, "yyyy-MM-dd", null);
startDateValue = "'" + dt.ToShortDateString() + "'";
startDateDate = dt;
endDateValue = endDate.InnerText;
dt = DateTime.ParseExact(endDateValue, "yyyy-MM-dd", null);
endDateValue = "'" + dt.ToShortDateString() + "'";
endDateDate = dt;
TimeSpan span = endDateDate - startDateDate;
string monthDuration = Math.Round((double)span.Days / 30).ToString();
heading = monthDuration + " Months Ending " + endDateDate.ToString("MM/dd/yyyy");
periodValue = "duration";
ctx.Add(new Contexts(periodValue,startDateDate,endDateDate,contextID, heading, Convert.ToInt16(monthDuration)));
}


}
//return ctx.Sort(delegate (ctx c1, ctx c2));
return ctx;

}

It might look like there's a lot going on here, but there isn't really. Once you set them up, lists can be very powerful.

 
No one seems to have answered the original question, just advised on alternatives to Array!

Whilst not reflecting on the advantages or otherwise of the alternatives suggested, to answer the original question ...
Code:
string[] myArray
...
...
...
...
Array.Resize(myArray, newsize)

 
I would assume the reason that most people didn't offer that as a suggestion is that from a coding standpoint, it doesn't offer the best option for most people/situations. This would especially be true if you entered a loop adding say 100 or more objects.

What you stated would probably work for the OP and does answer the question.

If a user asks for X, and what they really want is Y, do you just blindly give them X if it may not meet their needs even though that's what they asked for? As a consultant, I find it best to ask what they are attempting to do to find the best response.
 
arrays have a defined size. if you don't know the size required then List<> is your best option. you could go this route.
1. define default array size
2. populate array
3. when more than half the slots are full
3.1 create new array which is double the length of the current array
3.2 copy the items from the original array to the newer, larger array
3.3 overwrite the original array with the new array.
4. once completed find the empty array slots and compress the array into another array with just the right amount of elements.

but why would you want to do this. simply use a List<> and add what's needed.

BTW List<object> is pointless. if you want a generic list of objects use a Dicitonary, ArrayList, or Hashtable. List<> is meant to restrict the collection to a type without casting. if you need to hold a variety of types use polymorphism (it what OOP does). with a base class/interface each concrete implementation can do it's own thing.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top