C#: Access modifiers for classes

This entry is part 17 of 71 in the series C#

In the earlier examples, we have used public and private to declare displayName() method.

There are basically 4 access modifiers in C#, public, private, protected and internal.

When we declare a function as public, access to it will not be restricted.

Declared AccessibilityMeaning
publicAccess is not restricted.
protectedAccess is limited to the containing class or types derived from the containing class.
internalAccess is limited to the current assembly.
protected internalAccess is limited to the current assembly or types derived from the containing class.
privateAccess is limited to the containing type.

Note that access modifiers are not applicable to namespaces.

Internal classes are accessible only within files in the same assembly.

For example, the code below will not produce an error since it will be assembled in one source or cs file.

using System;

namespace Hello
{
    class Friend
    {
        internal void displayName(String name)
        {
            Console.WriteLine("Hello, what is your name?");
            Console.WriteLine("Your name is " + name);
            Console.ReadLine();
        }

        static void Main()
        {
            string name = "John";

            Friend friend = new Friend();           
            friend.displayName(name);
        }
    }
}

If we have one source file Assembly1.cs during the first assembly and we produce another source file Assembly2.cs during a second assembly, the first file contains an internal base class, BaseClass. In the second file, an attempt to instantiate BaseClass will produce an error.

For more information about internal member functions or types, you can also refer to msdn here.

variable

C#: namespace and class

This entry is part 10 of 71 in the series C#

Using the earlier simple program, we go through the components of a C# program.

using System;

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}

A namespace is a collection of classes.

Here in our simple program, there are actually 2 namespaces namely, System and ConsoleHelloApplication.

For the namespace ConsoleHelloApplication, we have only one class known as Program.

For the namespace System, we have used Console class to print out a line.

In an object-oriented program, we commonly say that Program class is a member of namespace ConsoleHelloApplication.

We can also say that the Main function is a member of Program class.

There is one more class here with 2 member functions.  Are you able to name the 2 member functions?

Well, the class is Console and the 2 member functions are WriteLine() and ReadLine().  The 2 member functions are also called the methods of Console class.

jQuery Selectors: Class and ID selectors

This entry is part 4 of 33 in the series jQuery Selectors

When we were learning jQuery we have gone through some examples very similar to below.

Here we show how we can select elements based on class and ID.

The ID used here is #hide-button and the class is .red.

<!DOCTYPE html>
<html>
<head>
    <script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
    <script>
        $(function () {
            $('#hide-button').click(function () {
                $('.red').hide();
            });
        });
    </script>
</head>
<body>
<h1 id='title' class='bold red'>The Headline</h1>

<div id='container'>
    Class and id selector.
    <p id='description' class='red'>The headline and this p will be hidden.</p>

    <p id='note' class='green'>This happens because of their class attribute's value.</p>
</div>
<button id='hide-button'>Hide elements!</button>
</body>
</html>

CSS3 Selectors: Class selectors

This entry is part 5 of 40 in the series CSS3 Selectors

We can apply a class to a particular html element using <span> tag.

If there are other html elements with similar class, they will be affected too.

<!DOCTYPE html>
<html>
<head lang='en'>
    <meta charset='UTF-8'>
    <title>Codecrawl.com</title>
    <style>
        .city {
            font-family: sans-serif;
            color: purple;
            font-weight: bold;
        }
    </style>
</head>
<body>

<h2>Taylor Swift</h2>

<p> Taylor Swift moved to <span class='city'>Nashville</span>, Tennessee, at the age of 14 to pursue a career in country
    music.</p>

</body>
</html>

Javascript: Mouse event

This entry is part 26 of 27 in the series Javascript Tutorial

Mouse event is probably the most common event used in Javascript or web programming.

<!DOCTYPE html>
<html>
<head>
    <script>
        function mouseOver() {
            document.getElementsByClassName("lineClass")[0].style.color = "#C0D9FE";
            document.getElementsByClassName("lineClass")[1].style.color = "#C0D9FE";
            document.getElementsByClassName("lineClass")[2].style.color = "#C0D9FE";
        }
        function mouseOut() {
            document.getElementsByClassName("lineClass")[0].style.color = "silver";
            document.getElementsByClassName("lineClass")[1].style.color = "silver";
            document.getElementsByClassName("lineClass")[2].style.color = "silver";
        }
    </script>
</head>
<body>
<div class="lineClass">This is line 1</div>
<div class="lineClass">This is line 2</div>
<div class="lineClass">This is line 3</div>
<h1 onmouseover="mouseOver();" onmouseout="mouseOut();">Mouse over this text</h1>

</body>
</html>

JS Fiddle

We have bind 2 events to a single h1 element.  When the mouse is over the line, mouseOver function is called changing the lineClass related lines to a different color.

Look at how lineClass has turned the 3 div lines into an array with 3 items in the array.  They are accessed with [0], [1] and [2].

You can also pass the lineClass into a variable.  This will make the code look cleaner.

<!DOCTYPE html>
<html>
<head>
    <script>
        function mouseOver() {
            var lineC = document.getElementsByClassName("lineClass")
            lineC[0].style.color = "#C0D9FE";
            lineC[1].style.color = "#C0D9FE";
            lineC[2].style.color = "#C0D9FE";
        }
        function mouseOut() {
            var lineC = document.getElementsByClassName("lineClass")
            lineC[0].style.color = "red";
            lineC[1].style.color = "red";
            lineC[2].style.color = "red";
        }
    </script>
</head>
<body>
<div class="lineClass">This is line 1</div>
<div class="lineClass">This is line 2</div>
<div class="lineClass">This is line 3</div>
<h1 onmouseover="mouseOver();" onmouseout="mouseOut();">Mouse over this text</h1>

</body>
</html>

JS Fiddle – simplified

In the above simplified JS Fiddle, I have passed the lineClass into a variable lineC as shown in line 2 below.

function mouseOver() {
    var lineC = document.getElementsByClassName("lineClass")
    lineC[0].style.color = "#C0D9FE";
    lineC[1].style.color = "#C0D9FE";
    lineC[2].style.color = "#C0D9FE";
}