Loading ...

C#: Swap two integer variables value | CodeAsp.Net

C#: Swap two integer variables value

 /5
0 (0votes)

In this blog I will show you how to swap two integer variables. I will be using Interlocked.Exchange to do the same. Below is the sample code:

using System;
using System.Threading;

namespace SampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            int j = 2;

            Console.WriteLine("Before Swapping: i={0}, j={1}", i, j);

            Interlocked.Exchange(ref j, Interlocked.Exchange(ref i, j));

            Console.WriteLine("After Swapping: i={0}, j={1}", i, j);
            Console.ReadLine();
        }
    }
}

 

Output:

Comments (2)

   
SDSWarlord
I have a problem, not an expert on SQL so I would appreciate any helpI have a multiview, on the default view customer selects the product he would like to purchase. on clicking the order button, view 2 is activated.on this page there is a select query that runs to update the selected product and display it in a gridview, which works. However there is a portion on the page that has a number of labels. One of which gets the current logged in client, I would like a query to select from my SQL database the other clients details and then populate all the remaining labels, Name, Address, City, etc. I cannot get this to work. I have included the code from my .vb page The code runs by I come up with the following error: An expression of non-boolean type specified in a context where a condition is expected, near '_CustomerNumber'. I do not know if I am on the right track or totally going in the wrong direction. I would appreciate help with this as it is urgent. .vb Code as follows:Imports System.DataImports System.Data.SqlClientPartial Class _Default    Inherits System.Web.UI.Page    Protected Sub SubmitBtn1_Click(ByVal Sender As Object, ByVal E As System.EventArgs) Handles SubmitBtn1.Click        If RadioButtonMEEOptions1.SelectedValue > "" Then            MultiView1.ActiveViewIndex = 1            SelectMEEOrders.SelectParameters("txtOption").DefaultValue = RadioButtonMEEOptions1.SelectedItem.Value            SelectMEEOrders.SelectParameters("txtQuantity").DefaultValue = DropDownListMEEOptions1.SelectedItem.Value            Dim objCmd As SqlCommand            Dim objReader As SqlDataReader            Dim strSQL As String            Dim MyConn As SqlConnection = New SqlConnection()            MyConn.ConnectionString = "Data Source=VALHALLA; Initial Catalog=CompuSwiftData; User ID=CompuSwift; Password=CompuSwift;"            Dim CustomerNumber As String = txtCustomerNumber.Text            MyConn.Open()            strSQL = "SELECT 03_FirstName FROM Users WHERE 02_CustomerNumber = '" & Session("CustomerNumber") & "'"            objCmd = New SqlCommand(strSQL, MyConn)            Try                objReader = objCmd.ExecuteReader(CommandBehavior.Default)                While objReader.Read()                    With objReader                        txtName.Text = objReader("03_FirstName").ToString()                    End With                End While            Catch ex As Exception                lblMsg.Text = ex.Message            End Try        ElseIf RadioButtonMEEOptions1.SelectedValue = "" Then            lblMessage.Visible = True            lblMessage.Text = ("You did not select an product option, please select an option")        End If    End SubIf need be I will post the aspx page as well if you need it.   
5/27/2010
 · 
by
   
SDSWarlord
I have a problem and need assistance urgently. I am by no means an expert on SQL so I am not sure as to what I am doing wrong.I hope that I am going in the right direction with this!!I have a multiview, in the the default view the client selects his products that he would like to purchase. on click of the order button, view two of the multiview activates. This is then populated by a stored proc with the clients selections in a grid view. This works fine and the grid view updates correclty. However there is a section in the page where the client details must be updated based on who is logged into the site at the time. In this portion one label gets updated by code and displays whoi is logged in at the time, but based on this the other labels must be updated with the rest of the clients details, Address, phone, cell, etcHowever this portion does not work, I get the following error: An expression of non-boolean type specified in a context where a condition is expected, near '_CustomerNumber'.I have included the .vb code behind so that you can look at it and see what I am doing wrong..vb codeImports System.DataImports System.Data.SqlClientPartial Class _Default    Inherits System.Web.UI.Page    Protected Sub SubmitBtn1_Click(ByVal Sender As Object, ByVal E As System.EventArgs) Handles SubmitBtn1.Click        If RadioButtonMEEOptions1.SelectedValue > "" Then            MultiView1.ActiveViewIndex = 1            SelectMEEOrders.SelectParameters("txtOption").DefaultValue = RadioButtonMEEOptions1.SelectedItem.Value            SelectMEEOrders.SelectParameters("txtQuantity").DefaultValue = DropDownListMEEOptions1.SelectedItem.Value            Dim objCmd As SqlCommand            Dim objReader As SqlDataReader            Dim strSQL As String            Dim MyConn As SqlConnection = New SqlConnection()            MyConn.ConnectionString = "Data Source=VALHALLA; Initial Catalog=CompuSwiftData; User ID=CompuSwift; Password=CompuSwift;"            Dim CustomerNumber As String = txtCustomerNumber.Text            MyConn.Open()            strSQL = "SELECT 03_FirstName FROM Users WHERE 02_CustomerNumber = '" & Session("CustomerNumber") & "'"            objCmd = New SqlCommand(strSQL, MyConn)            Try                objReader = objCmd.ExecuteReader(CommandBehavior.Default)                While objReader.Read()                    With objReader                        txtName.Text = objReader("03_FirstName").ToString()                    End With                End While            Catch ex As Exception                lblMsg.Text = ex.Message            End Try        ElseIf RadioButtonMEEOptions1.SelectedValue = "" Then            lblMessage.Visible = True            lblMessage.Text = ("You did not select an product option, please select an option")        End If    End Sub   
5/27/2010
 · 
by

Top Posts