Thursday, 11 September 2014

Default Scope of Class In C# Dot.net?





Hi All,  
Today i am going to explain the default scope of class  in C# .  
default scope of  class is internal, 
Class can  be public either internal , it can never be Private or protected  and protected Internal.  
In order to understand ,create the console Application.



















Go to Object Browser (Ctrl+Alt+j) here we will see the default scope of class.






















   Class can be Public also, but it can never be protected  , Private and  protected internal.
















So let me clarify "Default scope of class is internal" class can be public and internal but it can never be a protected  , private and protected  internal.

Happy coding...  
 

Access modifiers in C#

Today i am going to explain what is access modifiers in C#.


One of the most  favorite question of interviewer.  
What is modifiers?
Different types of modifiers  and different between them  ?

Access modifiers
Access modifiers are keywords used to specify the declared accessibility of a member or a type.
In C# there are 5 different types of Access Modifiers.

Public
-No restrictions to access
-The type or member can be accessed by any other code in the same assembly or another assembly that references it.

protected

-Access is limited to within the class definition and any class that inherits from the class.
-The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal
-Access is limited exclusively to classes defined within the current project assembly.
-The type or member can be accessed only by code in the same class or struct.

private
-Access is limited to within the class definition; This is the default access modifier type if none is formally specified.
-The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
protected internal member enjoy dual effect.it is accessible anywhere in the project as well as if that class is inherit one other class and that is in other project then only it can be accessible to other project also.

Go to live Example here.

Point to be notice  here
we can access these member objpublic objinternal and  objprotectedinternal in program class we can not access the private member outside the class and we can also not  access the protected member with derived class. In Order to access the protected member with derived class we have to create the object of derived  class.
I have create the object of Program class.that object can access the protected  member. because protected definition saying "It can be access with his derived class"
many of developer got confused in protected modifiers just because we have to create  object of derived  class.

private modifiers  saying "The type or member can be accessed only by code in the same class or struct."

Please find the code below.
class Fristclass
    {
        public int objpublic;
        private int objprivate;
        protected int objprotected;
        internal int objinternal;
        protected internal int objprotectedinternal;

        void show()
        {
            Console.WriteLine(objprivate); // private can access inside  same class or struct
        }
    }
    class Program:Fristclass
    {
        static void Main(string[] args)
        {
            Fristclass objclass = new Fristclass();
            Console.WriteLine(objclass.objinternal); // it can be current project assembly and same class and struct also
            Console.WriteLine(objclass.objprotectedinternal); // one other class and that is in other project then only it can be accessible to other project also.
            Console.WriteLine(objclass.objpublic); // no limit of access 



            // for protected and
            Program programclassobj = new Program();
            Console.WriteLine(programclassobj.objprotected);  // It can be access with his drived class
            Console.WriteLine(programclassobj.objprotectedinternal); // one other class and that is in other project then only it can be accessible to other project also
        }
    }

Happy coding....

Friday, 5 September 2014

Mobile number validation by using jquery

Hi Guys ,

In this post we are going to learn how to validate mobile number by using jquery 

Below is the jquery mobile, validation regular expression, mobile validation jquery regular expression which is for checking mobile digit number.



$(document).ready(function() {
    $('#textboxid').blur(function(e) {
        if (validateMobile('textboxid')) {
           alert('valid');
        }
        else {
            alert('invalid');
        }
    });
});

function validateMobile(txtPhone) {
    var a = document.getElementById(txtPhone).value;
    var filter = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;
    if (filter.test(a)) {
        return true;
    }
    else {
        return false;
    }
}

How to set maximum characters in textbox

Hi Guys,

In this post,  we are going to learn how to set  limit the maximum number of characters in textbox and we count the number of characters entered in textbox .




Add this jquery code in your page

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
   
    function CountCharacters(txtMsg, CharLength, indicator) {
        chars = txtMsg.value.length;
        document.getElementById('lblcountEnter').innerHTML = chars;
        document.getElementById(indicator).innerHTML = CharLength - chars;
        if (chars > CharLength) {
            txtMsg.value = txtMsg.value.substring(0, CharLength);
        }
    }
</script>


And also paste this html code in your page 


<div>
    Number of Characters Left:
    <label id="lblcount"<br />
    Number of Characters You Have Enter:
    <label id="lblcountEnter">140</label><br />
   
    <textarea id="mytextbox" rows="5" cols="25" onkeyup="CountCharacters(this,139,'lblcount');"></textarea>
</div>


We can also set maxlength in textbox also like .

<input type="text" name="usrname" maxlength="10">



Happy coding



Thursday, 4 September 2014

set an unlimited length for maxJsonLength in web.config

Hi Guys, 
you can resolve this issue  "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Parameter name: input " 
 by using below code ,

JavaScriptSerializer serializer = new JavaScriptSerializer();
 serializer.MaxJsonLength = Int32.MaxValue;   //Or any size you want to use, basically int maxValue is 2GB, you shouldn't need this big json string to deserialize, else you are doing it wrong.
 GoogleContacts profile = serializer.Deserialize<GoogleContacts>(json);


Either You can set the MaxJsonLength property on your web.config:



<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 
 
 
 
If you are using MVC 4 then you can try this solution 
 
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonResult()
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior,
        MaxJsonLength = Int32.MaxValue
    };
} 
 
 
happy coding....