-2

I have the following method which I show below:

public static string loadLista(string pStrOp)
{
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WXYZ"].ConnectionString);
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select distinct RTRIM(LTRIM(c.str_val)) as Id , RTRIM(LTRIM(c.str_val)) as Value " +
                                                                                "from dbo.toc a " +
                                                                                "inner join dbo.propval c on c.tocid = a.tocid and c.PROP_ID = 698 " +
                                                                                "where a.pset_id = 114 and c.str_val is not null " +
                                                                                "order by 2 asc;", conn);


    var items = new List<Parametro>();


    try
    {
        conn.Open();

        using (var dr = cmd.ExecuteReader())
        {
            if (dr.HasRows)
            {

                 int fc = dr.FieldCount;
                 var colums = new Dictionary<int, string>();
                 for (int i = 0; i < fc; i++)
                   colums.Add(i, dr.GetName(i));

                object[] values = new object[fc];

                while (dr.Read())
                {

                dr.GetValues(values); //Get All Values
                Parametro item = Activator.CreateInstance<Parametro>();
                var props = item.GetType().GetProperties();

                foreach (var p in props)
                {
                    foreach (var col in colums)
                    {
                        if (p.Name != col.Value) continue;
                        var value = values[col.Key];
                        if (value.GetType() != typeof(DBNull))
                        {
                            p.SetValue(item, value, null);
                        }   
                    }

                }

                items.Add(item);

                }

            }
        }
    }
    catch (Exception ex)
    {
        return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("");
    }

    return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(items);
}

As you can see, I use a SqlCommand to make a query, then store it in a DataReader to process the data and finally save it in a list. The method works perfectly, the question is how can I replace the use of Lists in this method, to use arrays instead, since I have a limitation that prevents me from using Lists. If you could give me an applied example, it would be very helpful. Thanks in advance for the help provided.

  • 2
    You can always just call `ToArray` on the list - the problem with arrays is you can't easily change their size, but you can easily add items to a list. If you explain why you can't use list perhaps a different solution can be found. – D Stanley Feb 28 '18 at 02:33
  • 1
    I would also comment that swallowing any exception and just returning an empty result is not helpful. You have no idea what the exception was or what caused it to know what to fix. – D Stanley Feb 28 '18 at 02:34
  • @DStanley the problem is that I can not use Lists, for example when defining var items = new List (); I have an error in the code, this method should work in a solution that is already implemented, the problem that does not recognize the lists should be for an assembly in the solution, but adding that assembly for the lists to work can have an impact on other functionalities That is why, in the first instance, what was proposed was to replace the use of lists by the use of arrangements. – ASP.NEET Feb 28 '18 at 02:45
  • I'm not following what the problem is. `List` is in mscorlib, so there should be no additional assembly to add. – D Stanley Feb 28 '18 at 02:58
  • @DStanley - they clearly don't know what they're talking about. OP I think you need to read a book on programming. There is a fundamental lack of understanding, its probably really annoying you - just pick up a book or do some .Net tutorials on Lists and Arrays. – Jeremy Thompson Feb 28 '18 at 03:02

1 Answers1

0

I don't quite understand why you cant use List<T> and you should figure that out first, however for academic purposes

Array.Resize Method (T[], Int32)

Changes the number of elements of a one-dimensional array to the specified new size.

Remarks

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.array must be a one-dimensional array.

If array is null, this method creates a new array with the specified size.

So instead of using a List<T>

Parametro[] items;

...

Array.Resize(items, (items?.Length ?? 0) +1 ) // resize
items[items.Length-1] = item; // add item to the last index

Note, this is fairly inefficient, List<T> actually works with a capacity that is set to a factor of 2 of whats needed when it has to internally increase the array.

Disclaimer : Not tested, there may be typos

InteXX
  • 6,135
  • 6
  • 43
  • 80
TheGeneral
  • 79,002
  • 9
  • 103
  • 141