-->

Monday, March 9, 2015

Coupling in borland C language

Coupling measures the strength of all relationships between functional units. It is the measure of the interdependence of one module to that of another. The program should have low coupling. Low coupling minimize the cause of errors in other modules. The errors in the other modules are caused because of the change in one module.

Definition: Coupling can be defined as the degree of interdependence between two or more modules. The reduction in coupling reduces the complexity of the program. While designing a program, make each module as independent as possible eliminating unnecessary relationships.

The following figure shows the types of coupling that exists in programming:
Types of Coupling:
1. No direct coupling
2. Normal coupling
    2.1   Data coupling
    2.2   Stamp coupling
    2.3   Control coupling
3. Common coupling
4. Content coupling

No direct coupling: These are the independent modules of the program. They are not really components of a single program.

Normal coupling: Two modules, X and Y, are normally coupled if X calls Y, Y returns to X and all information passed between them is by parameters in the call.

Data coupling: Two modules are said to be data coupled if they communicate by passing parameters. This is the most common type of coupling. Try to keep the parameters as minimum as possible.

Stamp coupling: Two modules are said to be stamp coupled if they communicate through a passed data structure that contains more information than necessary for them to perform their operations. A complete piece of data is passed between the modules.

Control coupling: Two modules are said to be control coupled if they communicate with the help of at least one "control flag". The control flag controls internal logic of the module.

Common coupling: Two modules are said to be common coupled if both of them share the same global data area. This type of coupling is really undesirable. Problem or error in one module can affect the other modules. Even it is difficult to identify the affected modules.

Content coupled: Two modules are content coupled if one module changes a statement in another, one module references or alters data contained inside another module or one module branches into another module.

The cohesion exists is within the modules where as the coupling exists between modules within the program. The law of program development is:

"Minimize the COUPLING and Maximize the COHESION"

Pointer introduction in C language

Introduction

Pointer is one of the important feature available in C language. Almost all the program in the software industry are written only using pointer, But many people think that pointer concept is difficult. The correct understanding and use of pointer is very much required for successful C programming. Pointers are one of the strongest and also one of the most dangerous features(if not used properly) available in C. Due to its important and dangerous feature, the concept of pointer is dealt in detail. Now , let us understand the concept of pointers.

Pointer concept 

Definition  : The basic data types in C languages are int, float, char , double , and void. Pointer is a special data type which is derived from these basic data types. So, pointer is called derived data type. The pointer takes the values from 0 to 65535 (Memory address) if the size of the RAM is 64K. The pointers are always associated with the following three concepts:

Pointer concept

  • Pointer Constants
  • Pointer Values 
  • Pointer Variables 

Linear Search in C language

Searching

Before writing the algorithm or program for searching , let us see, “ What is searching? What are the searching techniques?”
Definition : More often we will be working with the large amount of data. It may be necessary to determine whether a particular item is present in the large amount of data or not. This process of finding a particular item in the large amount of data is called searching. The two important and simple searching techniques are linear search and binary search.

SQL Video Channel : Download all SQL Video


Linear search (Sequential search)
“What is the linear search?”
Definition: Linear search is a simple searching techniques in which we search for a given key item in the list in linear order(Sequential order) i.e. one after the other. The item to be searched is often called key item. The linear search is also called sequential search.

For example , if key=10 and the list is 20,10,40,25 after searching we say that key is present . If key =100 after searching we say that key is not present.
“How to search for an item in a list of elements?” The procedure is as follows.

Linear/Sequencial Search in C


Procedure: Assume 10 is the item to be searched in the list of items 50,40,30,60,10. Observe from above figure that, 10 has to be compared with a[0], a[1], a[2], a[3], a[4].
But, once the value of I is greater than or equal to 5, it is an indication that item is not present and display the message “Unsuccessful Search”.

Note: In general the terminal condition in the for loop i<5 can be replaced by i<n.
You are already familiar with the algorithm and flowchart of linear search . Now, the C program for linear search is shown below:

#include<stdio.h>
#include<conio.h>
main()
{
int i,n,key,a[20];
printf("Enter the value of n.");
scanf("%d",&n);
printf("Enter n values:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter item to search:");
scanf("%d",&key);
/* Search the key in array */
for(i=o;i<n;i++)
{
if(a[i]==key)
{
printf("item found");
exit(0);
}
}
printf("Not found");
}

Output
Linear Search in C

Linear Search in C

Advantage of linear search

  • Very simple approach
  • Works well for small array
  • Used to search when the elements are not sorted (not in any order)
Disadvantage of linear search

  • Less efficient if the array size is large
  • If the elements are already sorted, linear search is not efficient.

Note : When the elements are not sorted and size is very less, linear search is used . if the elements are sorted , a better method or technique binary search can be used.

Binary Search in C
Selection Search in C

Code of Image Captcha in windows form c#

Image captcha is used for human verification. Actually some bots machine is used for creating back links to the blog/website. If you want to stop that machine, create human verification captcha. There are two types, first one is string based and another one is numbered based captcha. In this example, i will implement number based captcha.
Learn, how to design numbered based captcha. Actually, we will write some randomly generated number on image. After that we compare that number with the text box.

Video Contain Full code implementation details:

Copy this code and paste in the code file:

  int number = 0;
        private void createcaptcha()
        {
            Random r1 = new Random();
            number = r1.Next(10, 1000);
            Image img = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            var font = new Font("TimesNewRoman", 25, FontStyle.Bold, GraphicsUnit.Pixel);
            var graphics = Graphics.FromImage(img);
            graphics.DrawString(number.ToString(), font, Brushes.Red, new Point(0, 0));
            this.pictureBox1.Image = img;
     
        }

   private void refresh_Click(object sender, EventArgs e)
        {
            createcaptcha();

        }

private void verify_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == number.ToString())
            {
                MessageBox.Show("match");
            }
            else
            {
                MessageBox.Show("not match");
            }
        }

Here,

  1. Bitmap class is used for picking width and height parameter from the picture box.
  2. Using Font class, set the font style, font family and graphics unit that is pixel.
  3. Using Graphics class draw string on the picture, i have used randomly generated number.
  4. Now, assign the image to the picture box.

Code Generate the following output

Code of Image Captcha in windows form c#

Saturday, March 7, 2015

How to change Background image as a slider in windows forms application c#

Change background look attractive and different. If you want to change background with image also you want to put image slider in background, read this article carefully. Before put some images in background must to add some images in resource folder. Learn How to add images in resources folder: follow the mentioned path which is given below

Expand properties -->Resources-->Add Resource-->Add existing file

Now, set the Background image of the form through code file, add line of code before InitializeComponent() method.

   public Form1()
        {
            this.BackgroundImage = Properties.Resources._007;

            InitializeComponent();
 
        }

Now, Take a timer a control with Tick event also set the timer interval. After every interval image should be change. Now, copy the following code and paste in the code file:

Class constructor code:


 public Form1()
        {
            this.BackgroundImage = Properties.Resources.banner;
            InitializeComponent();
            Timer tm = new Timer();
            tm.Interval = 1000;
           tm.Tick += new EventHandler(changeimage);
            tm.Start();
        }

Definition of the function


  private void changeimage(object sender, EventArgs e)
        {
            List<Bitmap> b1 = new List<Bitmap>();
            b1.Add(Properties.Resources._007);
            b1.Add(Properties.Resources.banner);
            b1.Add(Properties.Resources.aspnet_validator);
            var timeget = DateTime.Now.Second % b1.Count;
            this.BackgroundImage = b1[timeget];
        }
Here,

  1. Timer control with 1 sec interval.
  2. After 1 sec timer control raise tick event handler code.
  3. List with image type.
  4. Add some images in the list.
  5. Find the remainder by the list of item on every second.
If you want to see the full code with output -- see this video

Wednesday, March 4, 2015

edmx with linq query to save and retrieve image to database in windows form c#

In my previous article we have already seen this article(Insert and retrieved) using ado.net in asp.net, On that time we  used nvarchar(size) datatype to store the image in database. Today, i am talking about image data type. Actually image data type store image in binary format. Also i will use edmx file or you can say entity framework to store and retrieve image from database table. First to create a database table in sql server management studio. Now, your table script look like:


USE [ht]
GO

/****** Object:  Table [dbo].[tblPicture]    Script Date: 4/3/2015 6:28:16 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblPicture](
[id] [int] IDENTITY(1,1) NOT NULL,
[pic] [image] NULL,
 CONSTRAINT [PK_tblPicture] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

Now, script generate the following design:


database table in management studio with image data type

Now, come to visual studio and create a new windows form project. Add two button control in the form, one for add image into database and another for retrieving image from table. Now add edmx file in the solution. After that you can write code in button click event handler block.

Code for insert image into database table:


  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opendlg = new OpenFileDialog();
            if(opendlg.ShowDialog()==DialogResult.OK)
            {
                Image img = Image.FromFile(opendlg.FileName);
                MemoryStream ms = new MemoryStream();
                img.Save(ms, img.RawFormat);
                htEntities1 ht = new htEntities1();
                ht.tblPictures.Add(new tblPicture() { pic = ms.ToArray() });
                ht.SaveChanges();
                MessageBox.Show("Load Sucess");

            }
        }
Here,

  1. First to open file dialog box and then save image into MemoryStream class.
  2. Now, you can save the MemoryStream object into database table with the help of context class(htEntities1) and model class(tblPicture).

Code for retrieving image from database table:

 private void button2_Click(object sender, EventArgs e)
        {
            htEntities1 ht = new htEntities1();
            var item = ht.tblPictures.Where(a => a.id == 1004).SingleOrDefault();
            byte[] arr = item.pic;
            MemoryStream ms = new MemoryStream(arr);
            pictureBox1.Image = Image.FromStream(ms);
         

        }
Here,

  1. Retrieve record by using linq query with lambda expression.
  2. Retrieved record insert into byte type array.
  3. Now, array pass into MemoryStream class constructor as a parameter.
  4. Now, Bind the PictureBox with Stream class.

Tuesday, March 3, 2015

How to bind label and text box control using edmx file in windows form c#

In previous article we have already learn about bind label or text box using ado.net. Today, i will discuss about edmx file. If you want to bind label or text box in windows form, edmx file is the best solution for this type of problem. There are following steps to bind the label or text box:

I am giving you a whole code video for user friendly: 

  1. First to add some label and text boxes in the form Like:
How to bind label and text box control using edmx file in windows form c#

  1.  Now, add edmx file in the solution
  2. Now, copy this code and paste in the button click event handler block.

 private void button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(idtxt.Text);
            Student_DBEntities dc = new Student_DBEntities();
            var getrecord = dc.Student_record.Where(a => a.Id == id).SingleOrDefault();

            Studentidtxt.Text = getrecord.Student_Id;
            studentnametxt.Text = getrecord.Student_Name;
            fathernametxt.Text = getrecord.Father_Name;
            MotherNametxt.Text = getrecord.Mother_Name;
            Addresstxt.Text = getrecord.Address;

        }
Here,
  1. Student_DBEntities is a data context name, Create a object of that class, Access public property(Student_record -Screen Shot mentioned in the link) by using instance name.
  2. Access single record with the help of where clause with lambda expression also use SingleOrDefault( ) method.
  3. By using getrecord variable you can access table fields or you can say model class fields. 
  4. Get each fields which is required by you, returning result assign in Text boxes.
© Copyright 2013 Computer Programming | All Right Reserved