Tuesday, 23 June 2015

Difference Between Const, ReadOnly and Static ReadOnly in C#

These are very common keywords and are quite confusing. So today we will discuss these keywords and try to understand them.
  1. Const: Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default a const is static and we cannot change the value of a const variable throughout the entire program.

    Csharp-Const-ReadOnly-and-StaticReadOnly1.jpg

    Here I have created a class named Variables and defined all three variables, so first let's play with const.

    Csharp-Const-ReadOnly-and-StaticReadOnly2.jpg

    Here I tried to de-initialize the const variable, it gaves me an error like "A const field requires a value to be provided".  Ok now I initialize a value for this variable and try to change it further in the class.

    Csharp-Const-ReadOnly-and-StaticReadOnly3.jpg

    Here I have created a static constructor, default constructor, parameterized constructor and a Simple Method. I tried to change the value of the const variable everywhere but once I assign the value, I am unable to change it again since when I do it gives me a compile time error as you can see in the snapshot above.

    Now let's on to the readonly keyword.
     
  2. Readonly: Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even a method. Let's see:

    Csharp-Const-ReadOnly-and-StaticReadOnly4.jpg

    Here first I try to initialize the value in the static constructor. It gives me an error. Which you can see above.
    Now I try to change the value in a method, see what happened:

    Csharp-Const-ReadOnly-and-StaticReadOnly5.jpg
     
    Here, it is also giving an error that you can only assign a value either through a variable or a constructor.
    Now try to change the value in the default constructor.

    Csharp-Const-ReadOnly-and-StaticReadOnly6.jpg
     
    Now in the snapshot above you can see it's built successfully without an error, warning or messages. Let's check if there is a runtime error. OK.

    Csharp-Const-ReadOnly-and-StaticReadOnly7.jpg
     
    Now here we can see that there is not a runtime error and the value was assigned successfully to the Readonly variable.
    Now one gotcha is, now that you have assigned the value, can you change this value again ??? 
    Let's try to change the value again.

    Csharp-Const-ReadOnly-and-StaticReadOnly8.jpg

     
    Here I created  a parameterized constructor and created a new object, and passing a value as "Hello Frend'z" and as I built it, it gave me the result "Build Succeeded".  Now let's move ahead and check for a runtime error:

    Csharp-Const-ReadOnly-and-StaticReadOnly9.jpg
     
    See guys. There is no runtime error !!  And the value can be changed again and again through a constructor.
     
    Now move ahead to Static Readonly variables.
     
  3. Static ReadOnly: A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime. Let's understand it practically.

    Csharp-Const-ReadOnly-and-StaticReadOnly10.jpg

    Now in the preceding you can see that I used two variables, one is not assigned and another is assigned, and the static constructor.  Now in the static constructor you can see that the unassigned variable is being assigned and the assigned value is being changed. And there is no compile time error. Further I try to again change this variable's value.  See what happened:

    Csharp-Const-ReadOnly-and-StaticReadOnly11.jpg
     
    As you can see in the above, I created Default, Parameterized Constructor and Method and tried to change the value again here. But I am getting a compile time error for all.

Wednesday, 20 May 2015

Create Oracle Database link query

CREATE DATABASE LINK <LINKNAME> CONNECT TO <DATABASENAME> IDENTIFIED BY <DATABASEPASSWORD> USING 'localhost';

Thursday, 4 December 2014

File upload javaScript extesion,file size validation

HTML tag:

<input type="file" id="file" name="file" onchange="return checkFileExtension(this);" />JavaScript Function:

function checkFileExtension(elem) {
var filePath = elem.value;
if (filePath.indexOf('.') == -1)
return false;
var TmpImgSize = Math.round(parseInt(elem.files[0].size) / 1024);
if (parseInt(TmpImgSize) > 2000) {
alert('The Size of selected file is more that 2000 KB. Please select the file less than or equal to 2000KB');
elem.value = '';
return false;
}
var fileExt = elem.files[0].name.split('.');
if (fileExt.length > 2) {
fileExt = fileExt[1];
alert('Please ensure that file should be in single extension');
elem.value = '';
return false;
}
var validExtensions = new Array();
var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
validExtensions[0] = 'jpg';
validExtensions[1] = 'jpeg';
validExtensions[2] = 'bmp';
validExtensions[3] = 'png';
validExtensions[4] = 'gif';
validExtensions[5] = 'tif';
validExtensions[6] = 'tiff';
validExtensions[7] = 'txt';
validExtensions[8] = 'doc';
validExtensions[9] = 'xls';
validExtensions[10] = 'pdf';
validExtensions[11] = 'docx';
validExtensions[12] = 'xlsx';
for (var i = 0; i < validExtensions.length; i++) {
if (ext == validExtensions[i])
return true;
}
alert('The file extension should be jpg/jpeg/bmp/png/gif/tif/tiff/txt/doc/xls/pdf/docx');
elem.value = '';
//alert('The file extension ' + ext.toUpperCase() + ' is not allowed!');
return false;
}

Saturday, 26 July 2014

select duplicate records in oracle

select column_name, count(column_name)
from table
group by column_name
having count (column_name) > 1;

Friday, 4 July 2014

Append comma in string end C#

 string char1 = "";
            for (int i = 0; i < ricmodel.TypeofCharacteristics.Length; i++)
            {
                if (string.IsNullOrEmpty(char1))
                {
                    char1 = ricmodel.TypeofCharacteristics[i];
                }
                else
                {
                    char1 = char1 + "," + ricmodel.TypeofCharacteristics[i];
                }
            }

Wednesday, 2 July 2014

Oracle Shut Downing Steps

> sqlplus /nolog

SQL> connect / as sysdba

Connected.

SQL> shutdown abort

ORACLE Instance shut down.

SQL> startup nomount

ORACLE Instance started

SQL> alter database mount;

SQL> alter database open;
Which still gave the error:

ORA-00600: internal error code, arguments: [kcratr1_lostwrt]

I then found the advice to do the following:

SQL> startup mount

ORACLE Instance started

SQL> recover database

Media recovery complete

SQL> alter database open

Database altered

Oracle database import and export query

Oracle database dump file import query:
=========================================================
C:\>IMP FMS/FMS FILE=C:\users\nadamuni\desktop\fms.dmp FULL=Y

Oracle database dump file export query:
=========================================================
C:\>EXP FMS/FMS FILE=C:\users\nadamuni\desktop\fms.dmp