henry pf
henry pf

Reputation: 310

c# function not updating parameter

This is a basic c# application but I am quite rusty. I am just going to start with showing you my code

using System;
using System.Data.SqlClient;
using System.Text;


namespace DatabaseAdder
{
    class Program
    {
        static void Main(string[] args)
        {
               int RUId = 0;
                    int QuestionId = 0;
                    DateTime Date = DateTime.Now;
                    string QuestionWhenAnswered ;
                    string QuestionResponse;
                    int Accepted;
                    string AssignedWorkStation;
                    string CompleteToken;
            try
            {for (int i = 0; i < 300; i++) {

                   QuestionId ++;
                   QuestionIncrementInSetsOfTwelve(QuestionId);

                    Console.WriteLine(i  );
                    Console.WriteLine( QuestionId);

                    Random rand = new Random();

                // Build connection string
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "localhost";   // update me
                builder.UserID = "sa";              // update me
                builder.Password = "Mypassword123";      // update me
                builder.InitialCatalog = "CDA";

                // Connect to SQL
                Console.Write("Connecting to SQL Server ... ");
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {

                    connection.Open();
                    var sql = "INSERT INTO QuestionResponses(RUId, QuestionId,Date,QuestionWhenAnswered,QuestionResponse,Accepted,AssignedWorkStation,CompleteToken)" +
                        " VALUES(@RUId, @QuestionId,@Date,@QuestionWhenAnswered,@QuestionResponse,@Accepted,@AssignedWorkStation,@CompleteToken)";
                    using (var cmd = new SqlCommand(sql, connection))
                    {
                        cmd.Parameters.AddWithValue("@RUId", "1" );
                        cmd.Parameters.AddWithValue("@QuestionId",  "1");
                        cmd.Parameters.AddWithValue("@Date", DateTime.Now);
                        cmd.Parameters.AddWithValue("@QuestionWhenAnswered", "sam");
                        cmd.Parameters.AddWithValue("@QuestionResponse", "sam");
                        cmd.Parameters.AddWithValue("@Accepted", "1");
                        cmd.Parameters.AddWithValue("@AssignedWorkStation", "sam"); 
                        cmd.Parameters.AddWithValue("@CompleteToken", "sam");

                        cmd.ExecuteNonQuery();
                    }
                    }
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("All done. Press any key to finish...");
            Console.ReadKey(true);
        }

      static int  QuestionIncrementInSetsOfTwelve(int questionId)
        {
            if(questionId < 12)
            {
                questionId = 0;
            }
            else
            {
            }
            return questionId;
        }
    }
}

The questionincrementinsetsoftwelve is not changing the value when it is called even when I have debugged and can watch that its value is over 12 but it is still not setting this back to 0.

I understand that there is probably something very small I am overlooking so be easy on my ego.

Upvotes: 0

Views: 1763

Answers (2)

Joshua Schlichting
Joshua Schlichting

Reputation: 3450

You're passing QuestionIncrementInSetsOfTwelve() a value. It's not updating the parameter, it's taking it in, doing "stuff," and returning a new value.

If you want to alter the parameter passed in, use the ref keyword. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

Example from microsoft:

void Method(ref int refArgument)
{
    refArgument = refArgument + 44;
}

int number = 1;
Method(ref number);
Console.WriteLine(number);
// Output: 45

Notice how this returns nothing? It updates the argument passed in, because it was passed by ref.

Upvotes: 2

TheBatman
TheBatman

Reputation: 770

This bit

QuestionId ++;
QuestionIncrementInSetsOfTwelve(QuestionId);

Should be

QuestionId ++;
QuestionId = QuestionIncrementInSetsOfTwelve(QuestionId);

integers are passed by value, not by reference. So a new integer that has the same value as QuestionId is being passed to the method, and that new integer is being returned from it. You have to assign the result. The behavior you are looking for can be achieved by using the ref keyword but isn't needed so long as we assign the result.

Upvotes: 8

Related Questions