Thursday 5 November 2015

How to Dynamically create Multiple Tabpages with Gridview in Each Tab on Button click


Below is the code how to Dynamically create Multiple Tabpages with Gridview in Each Tab.



private void btn_new_Click(object sender, EventArgs e)
        {
       

//For creating the Headers such as name ,values,class


            DataGridViewTextBoxColumn Name = new DataGridViewTextBoxColumn();
            Name .HeaderText = "Name";
            Name .Width = 150;

 DataGridViewTextBoxColumn values= new DataGridViewTextBoxColumn();
            values.HeaderText = "values";
            values.Width = 150;

 DataGridViewTextBoxColumn class= new DataGridViewTextBoxColumn();
            class.HeaderText = "Class";
            class.Width = 150;

 DataGridViewTextBoxColumn  Time = new DataGridViewTextBoxColumn();
            Time  .HeaderText = "Time";
            Time .Width = 150;

 DataGridViewTextBoxColumn A = new DataGridViewTextBoxColumn();
            A .HeaderText = "A";
            A .Width = 150;


 DataGridViewTextBoxColumn  B = new DataGridViewTextBoxColumn();
            B .HeaderText = "B";
            B .Width = 150;


//now add tab page in main tab.


          TabPage tabpage = new TabPage(class_new_tab.new_tab_name_returned);
            tabpage.BackColor = Color.White;
            tabpage.Name = class_new_tab.new_tab_name_returned;
            tabpage.Width = 200;
            tabpage.Height = 20;

           Tab.Controls.Add(tabpage);


// now add gridview ,this gridview you have to add in tab page..


             DataGridView dgv = new DataGridView();
            dgv.Dock = DockStyle.Fill;
            dgv.BackgroundColor = Color.White;
            tabpage.Controls.Add(dgv);
            dgv.Columns.Add(Name);
            dgv.Columns.Add(values);
            dgv.Columns.Add(class);
            dgv.Columns.Add(Time);
           dgv.Columns.Add(A);
            dgv.Columns.Add(B);
            dgv.RowHeadersVisible = false;//this will hide header from gridview
}










           

Difference between Structure and Union?




STRUCTURE

UNION

1.The keyword  struct is used to define a structure
1. The keyword union is used to define a union.
2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes.
2. When a variable is associated with a union, the compiler allocates the  memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
3. Each member within a structure is assigned unique storage area of location.
3. Memory allocated is shared by individual members of union.
4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
5 Altering the value of a member will not affect other members of the structure.
5. Altering the value of any of the member will alter other member values.
6. Individual member can be accessed at a time
6. Only one member can be accessed at a time.
7. Several members of a structure can initialize at once.
7. Only the first member of a union can be initialized.

Wednesday 29 April 2015

What is name hiding in C++?


Let us understand name Hiding concept with an example:


#include<iostream>
#include<conio.h>
using namespace std;

class A
{
public:

    void fun()
    {
    }
    void fun1()
    {
    }
};

class B:public A
{
public:
    void fun()
    {
    }
    void fun1(int x)
    {
    }
};

void main()
{
    B obj;
    obj.fun();
    obj.fun1();
}


in the above program it looks like the call of function fun1() in the main should run fine


  • But, in reality that method call results in an error which will say something like “error: no matching function for call to "error C2660: 'B::fun1'"
  • Why would it return that error? The Class B clearly derives from the Class A so it looks like the Class B object in the main function should have access to the fun1() function declared inside the Class A in addition to the fun(int) that accepts an integer  and is defined inside the Class B.



The reason the function call above results in an error is because of a property of C++ called name hiding


Let understand more about Name Hiding by taking another example:

#include<iostream>
#include<conio.h>
using namespace std;

class A
{
public:

    void fun1(string s)
    {
    }
};

class B:public A
{
public:
    void fun1(int x)
    {
    }
};

void main()
{
    string s;
    B obj;
    obj.fun1(s);

}

once again it look life there will be no issue in function fun1() calls from main function.
But when you try to compile your code it will throw an error " C2664: 'B::fun1' : cannot convert parameter 1 from 'std::string' to 'int'"


So, what should be done here?

Functions in derived classes with the same name as the functions in the parent classes, but that do not override the parent classes’ function are considered to be bad practice because it can result in errors just like the one above. So, having a function like fun1()  above is considered to be bad practice. This is technically not method overloading either, because overloading functions is done within the same class – not in an inheritance hierarchy.


The reason it’s bad practice is because it leads to ambiguity, and it’s much easier to just give the functions different names instead.

Solution to Name Hiding Problem

 The problem of name hiding can be solved ,All we have to do is implement the using keyword. let us understand by help of an example:


#include<iostream>
#include<conio.h>
using namespace std;

class A
{
public:

    void fun1(string s)
    {
    }
};

class B:public A
{
public:
    void fun1(int x)
    {
    }
    using A::fun1;
};

void main()
{
    string s;
    B obj;
    obj.fun1(s);

}



When we redeclare someFunction in the scope of Class B (with the “using” keyword), it makes both versions of that function (from both Class A and Claas B) visible in just the Classs B

Tuesday 28 April 2015

Functions Pointer in C++


  • Function Pointer are similar to Pointers , Only difference is instead of pointing to a variables , function pointer points to Functions.

Let us go through one example for better understanding:

  • int Arr[5]
As we know Arr  is constant pointer to an 5 element array , in order to retrieve value we will deference the pointer either by Arr[index] or *Arr, the corresponding Array values are returned.


  • int fun();
If you guessed that fun is actually a constant pointer to a function, you are correct. When a function is called (via the () operator), the function pointer is deferenced, and execution branches to the function.


Similar way is possible to declare a non-constant pointer to a function, syntax for writing a non -constant pointer to a function is :
  •  int (*fun)(int);
Parenthesis are necessary for precedence reason else it will be interpreted as pointer to a integer instead of pointer to a function.


fun is a pointer to a function that takes one integer parameter and return an integer , it will point to any function that matches its Signature.

example:

#include<iostream>
#include<conio.h>
using namespace std;

int fun(int a)
{  
    a++;  
    return a;
}

int main()
{
    int (*Pfun)(int);
    Pfun = fun;
    int b = Pfun(10);
    cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
    return 0;

}

Note: the signature (parameters and return value) of the function pointer must match the signature of the function. lets see few examples for better understanding.

// function prototypes
int foo();
double goo();
int hoo(int nX);

// function pointer assignments
int (*pFcn1)() = foo;           // okay
int (*pFcn2)() = goo;          // wrong -- return types don't match!
double (*pFcn3)() = goo;   // okay
pFcn1 = hoo;                      // wrong -- pFcn1 has no parameters, but hoo() does
int (*pFcn3)(int) = hoo;    // okay


few Examples of Pointer to function:

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;


//Example 1: simple

If a function pointer needs to point to functions that take parameters, the data-types of these parameters can simply be separated by commas in the function pointer declaration. Take for example the following:

int fun(int a , int b)
{    
    return a+b;
}

int main()
{
    int (*Pfun)(int, int );
    Pfun = fun;
    int a= Pfun(10,12);

    cout<<"\n value returned from pointer to a function is:-"<<a<<"\n";
    return 0;

}

//Example 2: 
if you want that caller function have flexibility to control how operation need to be done by simply reusing old snippet of code in-spite of writing two separated functions.


like in below code snippet function fun is being used both for add and multiply in-spite of writing separate fun function for add and multiply.

int fun(int a,int (*operation)(int , int))
{    
    
    return a+operation(10,5);   
     
}

int add(int a, int b)
{
    return a+b;
}
int multiply(int a, int b)
{
    return a*b;
}

int main()
{
     int x = fun(10,add);
     int y = fun(12,multiply);
    //int b = Pfun(10);
      
     cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
     cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
    return 0;

}


//Example 3:

We can also create arrays of function pointers. These aren't as scary as they sound, and they're simply created much like you would with an array of "normal" pointers, by specifying a number of elements in square brackets next to the pointer name. If we wanted a function pointer array which contained pointers to both functions "add" and "multiply" in elements of index 0 and 1, we could use the following:


int add(int a, int b)
{
    return a+b;
}
int multiply(int a, int b)
{
    return a*b;
}

int main()
{
     int (*fun[2])(int , int);
     fun[0]= add;
     fun[1]= multiply;
     int x = fun[0](10,12);
     int y =fun[1](1,3);
      
     cout<<"\n value returned from pointer to a function is:-"<<x<<"\n";
     cout<<"\n value returned from pointer to a function is:-"<<y<<"\n";
    return 0;


}







Thursday 27 February 2014

Main() Method and its purpose?


The Main method is the entry point of a C# console application or windows application.When the application is started, the Main method is the first method that is invoked.

A main method is static because it is available to run when your program starts and as it is the entry point of the program it runs without creating an instance of the class.


  • Static  members are scoped to the class level (rather than the object level) and can thus be invoked without the need to first create a new class instance. In other words, static functions exist before a class is instantiated so static is applied to the main entry point (Main method).


  • By Default Main() is Private because the reason behind this other application can not invoke the entry point (Main() method) of another. However please note that we can declare the Main() method as public.
  • Return value of Main() method is void (indicates no return value) by default. We can change it to one of the following possible signatures. 
  • We can use int return value of the main method to check whether there is an error occurred in the main method. For ex if main method executes successfully then we will return 0 (this is the default value return by the main method even if it defined as void). If main method executes unsuccessfully then we will return -1.

The parameter of the Main method is a String array that represents the command-line arguments.

For Example:  if I had a program (MyApp.exe) like this:

class Program
{
  static void Main(string[] args)
  {
    foreach (var arg in args)
    {
      Console.WriteLine(arg);
    }
  }
}

That I started at the command line like this:

 MyApp.exe Arg1 Arg2 Arg3

The Main method would be passed an array that contained three strings: "Arg1", "Arg2", "Arg3".

If you need to pass an argument that contains a space then wrap it in quotes. For example:

MyApp.exe "Arg 1" "Arg 2" "Arg 3"

Command line arguments commonly get used when you need to pass information to your application at run time.
 
For example if you were writing a program that copies a file from one location to another you would probably pass the two locations as command line arguments.

 For example:
Copy.exe C:\file1.txt C:\file2.txt


If you have more than one class that has a Main method, then you must compile your program with the /main compiler option to specify which Main method to use as the entry point. (Refer Below Image)



To set this compiler option in the Visual Studio development environment
  • Open the project's Properties page.
  • Click the Application property page.
  • Modify the Start up object property.



Thursday 23 January 2014

What's the difference between struct and class in C# .Net ?

In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.
The general difference is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.
A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.
A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.
This has one benefit, to begin with:
*value type*s always contains a value
reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment
Internally, *reference type*s are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:
copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places
When you declare variables or fields, here's how the two types differ:

variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.
Classes can be nothing - the reference can point to a null.
Structs are the actual value - they can be empty but never null. For this reason structs always have a default constructor with no parameters - they need a 'starting value.


For Example:

struct MyStruct 
{
    string MyProperty { get; set; }
}

void ChangeMyStruct(MyStruct input) 
   input.MyProperty = "new value";
}

...

// Create value type
MyStruct testStruct = new MyStruct { MyProperty = "initial value" }; 

ChangeMyStruct(testStruct);

// Value of testStruct.MyProperty is still "initial value"
//  the method changed a new copy of the structure.



*********For a class this would be different*******

class MyClass 
{
    string MyProperty { get; set; }
}

void ChangeMyClass(MyClass input) 
   input.MyProperty = "new value";
}

...

// Create reference type
MyClass testClass = new MyClass { MyProperty = "initial value" };

ChangeMyClass(testClass);

// Value of testClass.MyProperty is now "new value" 
// - the method changed the instance passed.