Move my parameters inside my array list with my string text

Airizzo

New member
Joined
Aug 21, 2019
Messages
2
Programming Experience
1-3
C#:
public static IEnumerable Briefing(string Msg, string Time)
{
    Msg = "Hello all".ToString();
    Time = "9:00am".ToString();

    ArrayList resultCollection = new ArrayList();
    resultCollection.Add(Msg);
    resultCollection.Add(Time);
    return resultCollection.ToArray();
}

That is my code at this moment. I need the "Hello All" and "9:00am" to be moved inside my array list with the parameters already in there. I have no idea how to add it though. Any help would be appreciated. I also wanted to say, it's not my choice to use arraylist, but I'm out ranked by what I want to use so I gotta use it.
 
Last edited by a moderator:
Firstly, there's no reason to call ToString on a literal String. It's not going to get any more stringy than it already is.

As for the issue, I don't really understand the problem. You obviously do know how to add items to an ArrayList because you're already doing it. Are you saying that you want to add them to an existent one rather than a new one? If so then simply get rid of the line that creates a new ArrayList and refer to an existent one in the subsequent lines instead. That may be via a member variable or you could add a parameter of type ArrayList to your method and pass it in when you call it.

If that's not what you mean then you're going to have to provide a clearer explanation.
 
I need the "Hello All" and "9:00am" to be moved inside my array list with the parameters already in there.
This question was asked and removed from SO for lack of clarity just like above. Remnants of what the OP wanted can be read in Google's Description of the SO topic it cached. But I'll answer it anyhow. What our OP is looking for is a way to add SQL parameters to an array list. Our OP said they can't use any other IEnumerable object, and that's likely because this is homework which they're likely late to submit. However I'll answer it for future readers, as I've got some time to kill. ;) If it where me, this is how I would do it using a list.
C#:
        private void Button1_Click(object sender, EventArgs e)
        {
            SqlParameter parameterA = new SqlParameter("@paramPlaceholderA", "paramValueA");
            SqlParameter parameterB = new SqlParameter("@paramPlaceholderB", "paramValueB");
            List<SqlParameter> listOfParams = new List<SqlParameter>
            {
                parameterA, parameterB
            };
            listOfParams.ForEach(delegate (SqlParameter thisParam)
            {
                Console.WriteLine($"The value of, {thisParam.ParameterName} is {thisParam.Value}");
            });
        }
Using an array list, it would look like this, but the functionality to avail of iterating from the array list like we can a list<T>.ForEach is not functionality offered by an array list.
Output:
The value of, @paramPlaceholderA is paramValueA
The value of, @paramPlaceholderB is paramValueB
Output is the same for both.
C#:
        private void Button1_Click(object sender, EventArgs e)
        {
            SqlParameter parameterA = new SqlParameter("@paramPlaceholderA", "paramValueA");
            SqlParameter parameterB = new SqlParameter("@paramPlaceholderB", "paramValueB");
            ArrayList listOfParams = new ArrayList()
            {
                parameterA, parameterB
            };
            foreach (SqlParameter thisParam in listOfParams)
            {
                Console.WriteLine($"The value of, {thisParam.ParameterName} is {thisParam.Value}");
            };
        }
In the case of our OP's code, we would make some minor changes and call our new function which will now return an IEnumerable collection of parameters. Comments are inline to help you understand what i did.
C#:
        private void Button1_Click(object sender, EventArgs e)
        {
            /* Call your method like this and return an enumerable object */
            IEnumerable enumerable = Briefing("Hello all", "9:00am");
            /* You can then iterate the collection and do what you want with your parameters */
            foreach (SqlParameter thisParam in enumerable)
            {
                Console.WriteLine($"The value of, {thisParam.ParameterName} is {thisParam.Value}");
            }
        }
        public IEnumerable Briefing(string Msg, string Time)
        {
            /* Build your parameters */
            SqlParameter parameterA = new SqlParameter("@hello", Msg);
            SqlParameter parameterB = new SqlParameter("@time", Time);
            /* Create a ArrayList and add your parameters to it */
            ArrayList listOfParams = new ArrayList()
            {
                parameterA, parameterB
            };
            /* Return your enumerable object of parameters */
            return listOfParams;
        }
Output:
The value of, @hello is Hello all
The value of, @time is 9:00am
Now your SQL Command just needs the correct statement using the correct parameters and you can execute your commands. Don't forget to add your parameters on iteration to your command before executing.
 
Last edited:
Perhaps I am missing something. Why even use the intermediate ArrayList when the return type of Briefing() is an IEnumerable? Why not simply have:
C#:
public IEnumerable Briefing(string Msg, string Time)
{
    yield return new SqlParameter("@hello", Msg);
    yield return new SqlParameter("@time", Time);
}
 
Ah. Then do this:
C#:
public static IEnumerable<SqlParameter> BriefingForSmartPeople(string Msg, string Time)
{
    yield return new SqlParameter("@hello", Msg);
    yield return new SqlParameter("@time", Time);
}

public static IEnumerable BriefingForMyDumbTeacher(string Msg, string Time)
    => new ArrayList(BriefingForSmartPeople(Msg, Time).ToArray());

?
 
I wonder if the creation of the array list was to try to make the code more legible. Here's my various attempts to try to see which one is the most legible.
C#:
public static class SqlParametersExtensions
{
    public static IEnumerable<SqlParameter> ToSqlParameters(this IDictionary<string, object> parameters)
        => parameters.Select(kvp => new SqlParameter(kvp.Key, kvp.Value));
}

static void LegibleCodeWithDictionary()
{
    string query = "INSERT INTO Briefings (Msg, Time) VALUES (@msg, @time)";
    var parameters = new Dictionary<string, object>()
    {
        ["@msg"] = "hello, world",
        ["@time"] = DateTime.Now.AddDays(1)
    };

    using (var connection = new SqlConnection("myconn"))
    using (var command = new SqlCommand(query, connection))
    {
        command.Parameters.AddRange(parameters.ToSqlParameters().ToList());
        connection.Open();
        command.ExecuteNonQuery();
    }
}

static void LegibleCodeWithExplicitParameters()
{
    using (var connection = new SqlConnection("myconn"))
    using (var command = new SqlCommand("INSERT INTO Briefings (Msg, Time) VALUES (@msg, @time)", connection))
    {
        command.Parameters.AddWithValue("@msg", "hello, world");
        command.Parameters.AddWithValue("@time", DateTime.Now.AddDays(1));
        connection.Open();
        command.ExecuteNonQuery();
    }
}

public static IEnumerable<SqlParameter> BriefingForSmartPeople(string Msg, DateTime Time)
{
    yield return new SqlParameter("@msg", Msg);
    yield return new SqlParameter("@time", Time);
}

static void LegibleCodeWithSmartBriefingMethod()
{
    using (var connection = new SqlConnection("myconn"))
    using (var command = new SqlCommand("INSERT INTO Briefings (Msg, Time) VALUES (@msg, @time)", connection))
    {
        foreach (var parameter in BriefingForSmartPeople("hello, world", DateTime.Now.AddDays(1)))
            command.Parameters.Add(parameter);
        connection.Open();
        command.ExecuteNonQuery();
    }
}

public static IEnumerable BriefingForMyDumbTeacher(string Msg, DateTime Time)
{
    return new ArrayList()
    {
        new SqlParameter("@msg", Msg),
        new SqlParameter("@time", Time)
    };
}

static void LegibleCodeWithDumbBriefingMethod()
{
    using (var connection = new SqlConnection("myconn"))
    using (var command = new SqlCommand("INSERT INTO Briefings (Msg, Time) VALUES (@msg, @time)", connection))
    {
        foreach (var parameter in BriefingForMyDumbTeacher("hello, world", DateTime.Now.AddDays(1)))
            command.Parameters.Add(parameter);
        connection.Open();
        command.ExecuteNonQuery();
    }
}
 
Back
Top Bottom