Thursday, November 14, 2013

C# multiple value return from single function

There are a number of ways to return two object from one function. You can class or structure and another one as out parameter. Alternatively, you can use generic type with two members. Such type, in particular, it Tuple:

#I am using Tuple to returns 2 datetime value(fromDate , toDate )
  public Tuple GetDates()
   {

          DateTime fromDate = DateTime.Now;
          DateTime toDate = DateTime.Now;
          int year = Convert.ToInt32(this.ddlQYear.SelectedItem.Value);

            if (Convert.ToInt32(ddlQ.SelectedItem.Value) > 0)
            {
                int month = Convert.ToInt32(ddlQ.SelectedItem.Value);
                fromDate = new DateTime(year, month, 1);
                if (month == 12)
                    toDate = new DateTime(year + 1, 1, 1);
                else
                    toDate = new DateTime(year, month + 1, 1);
            }
            return new Tuple(fromDate, toDate);
   }
===========Call this function bellow way==============================
            
DateTime fromDate = GetDates().Item1;
DateTime toDate = GetDates().Item2;


#Another way use "out" keyword:

 public static int GetDates(out DateTime FDate, out DateTime TDate)
   {
            DateTime fromDate = DateTime.Now;
            DateTime toDate = DateTime.Now;

           // Calculate your date

            FDate=fromDate ;
            TDate=toDate
     

         return 1;
   }
Hope that this helps!!

Wednesday, November 13, 2013

Swap column values in sql server 2008

Suppose we have a table named USER with 3 columns: USERID, USERName, USERAddress.

              USERID: int 
USERName: varchar(100)
USERAddress: varchar(100)


Swap with the temporary variable:
-----------------------------------------------


DECLARE @temp AS varchar(10)
 
UPDATE USER SET @temp=USERAddress, USERAddress=USERName, USERName=@temp
 


Hope freshers will find it helpful :)

Thursday, November 7, 2013

C# “is inaccessible due to its protection level” [Solved]

Some time its happen when your modifier are private or protected but need to access anywhere that time just your function need to be protected to public.

I had happened same problem when i write code radio button "OnCheckedChanged" event. I have solved that way to change modifier protected to public.


 public void ShareTransferSelection(object sender, EventArgs e)
    {
      // Code here
    }


Happy Programming!!