mobile ads

Friday, 11 October 2013

Can we define a Private Constructor

Yes, In static classes.
A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class. For example:
class NLog
{
    // Private Constructor:
    private NLog() { }

    public static double e = System.Math.E;
}
The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you don't use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.
Private constructors are used to prevent the creation of instances of a class when there are no instance fields or methods, such as the math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the entire class static.

What is Abstract Class and Interface? Why we use them and at What scenario we use?

AbstractClass: Abtract class cannot be instantiated, it can only be inherited.

When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

For example a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.


Interface: Interface is like contract between the base class and interface used. If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.

For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.

 If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.

For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .


Use an abstract class

  • When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)

  • Use an abstract class to define a common base class for a family of types.

  • Use an abstract class to provide default behavior.

  • Subclass only a base class in a hierarchy to which the class logically belongs.

 Use an interface

  • When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.

  • Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.

  • Use an interface to design a polymorphic hierarchy for value types.

  • Use an interface when an immutable contract is really intended.

  • A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

DataGridView using DataReader

Solution 1
DataReader will read row by row so you make looping to read all records if you want to display all record or any element from record , in other hand dataGridview need to take all data of your records in one package, so the solution is to make centralize store by make class that contains properties and set value for each properties when you read the value of each record and store each object from class that you create in ArrayList so the ArrayList will represent your store.

1.Create a class that contains properties
public class MyDetails
    {
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
           } 
        }
 
2. Create ArrayList to represent data


ArrayList sequence = new ArrayList();
 
3. Retreive data

SqlConnection sqlCon = null;
            try
            {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = "Your Connection String";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = sqlCon;
                cmd.CommandText = "SELECT * FROM StudentInfo";
                sqlCon.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MyDetails m = new MyDetails();
                    m.Id = (int)reader[0];
                    m.Name = reader[1].ToString();
                    m.Age = (int)reader[2];
                    sequence.Add(m);
                }
                dataGridView1.DataSource = sequence;
            }
            finally
            {
                sqlCon.Close();
            } 
 

Solution 2

SqlConnection con = new SqlConnection("Connection String");
con.Open();

SqlCommand com = new SqlCommand("SQL Query", con);
SqlDataReader read = com.ExecuteReader();
DataSet ds = new DataSet();
DataTable dt = new DataTable("Table1");
ds.Tables.Add(dt);
ds.Load(read, LoadOption.PreserveChanges, ds.Tables[0]);

gridControl1.DataSource = ds.Tables[0];
 

NavigationCommands Memers 2

NavigationCommands.FirstPage

FirstPage property gets the value that represents the First Page Command
KeyGesture   -  N/A
UI Text         -  First Page

This command indicates the intention to go to the first page.
DocumentViewer, FlowDocumentReader, FlowDocumentScrollViewer, and FlowDocumentPageViewer implement support for responding to the FirstPage command
Example

<MenuItem
  Command="NavigationCommands.FirstPage"
  CommandTarget="{Binding ElementName=flowDocumentPageViewer}" />
...

<FlowDocumentPageViewer Name="flowDocumentPageViewer">
  <FlowDocument>
    
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed vulputate,
 lacus non sagittis pharetra, diam dolor dictum tellus, et hendrerit odio risus 
    </Paragraph>
...
  </FlowDocument>
</FlowDocumentPageViewer>

NavigationCommands.NextPage

NextPage property gets the value that represents the Next Page Command
KeyGesture   -  N/A
UI Text         -  Next Page

This command indicates the intention to go to the next page.
DocumentViewer, FlowDocumentReader, FlowDocumentScrollViewer, and FlowDocumentPageViewer implement support for responding to the NextPage command
Example

<MenuItem
  Command="NavigationCommands.NextPage"
  CommandTarget="{Binding ElementName=flowDocumentPageViewer}" />
...

<FlowDocumentPageViewer Name="flowDocumentPageViewer">
  <FlowDocument>
    
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed vulputate,
 lacus non sagittis pharetra, diam dolor dictum tellus, et hendrerit odio risus 
    </Paragraph>
...
  </FlowDocument>
</FlowDocumentPageViewer>

NavigationCommands.LastPage

LastPage property gets the value that represents the Last Page Command
KeyGesture   -  N/A
UI Text         -  Last Page

This command indicates the intention to go to the lastpage.
DocumentViewer, FlowDocumentReader, FlowDocumentScrollViewer, and FlowDocumentPageViewer implement support for responding to the LastPage command
Example

<MenuItem
  Command="NavigationCommands.LastPage"
  CommandTarget="{Binding ElementName=flowDocumentPageViewer}" />
...

<FlowDocumentPageViewer Name="flowDocumentPageViewer">
  <FlowDocument>
    
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed vulputate,
 lacus non sagittis pharetra, diam dolor dictum tellus, et hendrerit odio risus 
    </Paragraph>
...
  </FlowDocument>
</FlowDocumentPageViewer>


NavigationCommands.PreviousPage

PreviousPage property gets the value that represents the PreviousPage Command
KeyGesture   -  N/A
UI Text         -  Previous Page

This command indicates the intention to go to the previouspage.
DocumentViewer, FlowDocumentReader, FlowDocumentScrollViewer, and FlowDocumentPageViewer implement support for responding to the PreviousPage command
Example

<MenuItem
  Command="NavigationCommands.PreviousPage"
  CommandTarget="{Binding ElementName=flowDocumentPageViewer}" />
...

<FlowDocumentPageViewer Name="flowDocumentPageViewer">
  <FlowDocument>
    
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed vulputate,
 lacus non sagittis pharetra, diam dolor dictum tellus, et hendrerit odio risus 
    </Paragraph>
...
  </FlowDocument>
</FlowDocumentPageViewer>

NavigationCommands.NavigateJournal

NavigateJournal property gets the value that represents the NavigateJournal Command
KeyGesture   -  N/A
UI Text         -  Navigate Journal

This command indicates the intention to navigate the journal.
Frame and NavigationWindow implement support for responding to the NavigateJournal command
Example
<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Window.Resources>
    
    <JournalEntryUnifiedViewConverter x:Key="JournalEntryUnifiedViewConverter" />
      
    <DataTemplate x:Key="journalMenuItemTemplate">
      <TextBlock>
        <TextBlock Text="{Binding (JournalEntryUnifiedViewConverter.JournalEntryPosition)}" />
        <TextBlock FontWeight="Bold" FontStyle="Italic">
          <TextBlock Margin="5,0,0,0">(</TextBlock>
          <TextBlock Text="{Binding JournalEntry.Name}"/>
          <TextBlock Margin="5,0,0,0">)</TextBlock>
        </TextBlock>
      </TextBlock>
    </DataTemplate>
      
    <Style x:Key="journalMenuItemContainerStyle">
      <Setter Property="MenuItem.Command" Value="NavigationCommands.NavigateJournal" />
      <Setter Property="MenuItem.CommandTarget" Value="{Binding ElementName=frame}" />
      <Setter Property="MenuItem.CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}}" />
    </Style>
  </Window.Resources>
...

<MenuItem
  Header="Navigate Journal"
  ItemTemplate="{StaticResource journalMenuItemTemplate}"
  ItemContainerStyle="{StaticResource journalMenuItemContainerStyle}" >
    
  <MenuItem.ItemsSource>
    <MultiBinding Converter="{StaticResource JournalEntryUnifiedViewConverter}" >
      <Binding ElementName="frame" Path="BackStack"/>
      <Binding ElementName="frame" Path="ForwardStack"/>
    </MultiBinding>
  </MenuItem.ItemsSource>
</MenuItem>
...

<Frame Name="frame" NavigationUIVisibility="Hidden" Source="Page1.xaml" />
...
</Window>

NavigationCommands.Refresh

Refresh property gets the value that represents the Refresh Command

KeyGesture   -  F5
UI Text         -   Refresh

This command indicates the intention to refresh the current page.
Frame and NavigationWindow implement support for responding to the Refresh commandExample

<MenuItem
  Command="NavigationCommands.Refresh"
  CommandTarget="{Binding ElementName=frame}" />
...

<Frame Name="frame" NavigationUIVisibility="Hidden" Source="Page1.xaml" />
 
NavigationCommands.Search


Search property gets the value that represents the Search Command
KeyGesture   -  F3
UI Text         -   Refresh

This command indicates the intention to search.
There is no implementation for responding to the Search command on any given WPF class. As such, you need to provide an appropriate implementation, which is shown in the example.
Example
XAML


<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.Search">

  <Window.CommandBindings>
    <CommandBinding
      Command="NavigationCommands.Search"
      CanExecute="navigationCommandSearch_CanExecute"
      Executed="navigationCommandSearch_Executed" />
  </Window.CommandBindings>
...

<MenuItem Command="NavigationCommands.Search" />
...
<FlowDocumentPageViewer Name="flowDocumentPageViewer">
  <FlowDocument>
    
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed vulputate,
 lacus non sagittis pharetra, diam dolor dictum tellus, et hendrerit odio risus
 nec erat. Nam sollicitudin imperdiet mi. Sed rutrum. Morbi vel nunc. Donec 
    </Paragraph>
...
  </FlowDocument>
</FlowDocumentPageViewer>
...
</Window>
 
 C#
using System.Windows; // Window 
using System.Windows.Input; // CanExecuteRoutedEventArgs, ExecutedRoutedEventArgs 

namespace SDKSample
{
    public partial class Search : Window
    {
        public Search()
        {
            InitializeComponent();
        }

        void navigationCommandSearch_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            // Can search of there is a document
            e.CanExecute = (this.flowDocumentPageViewer.Document != null);
        }

        void navigationCommandSearch_Executed(object target, ExecutedRoutedEventArgs e)
        {
            // Implement custom Search handling code
        }
    }
}
NavigationCommands.Zoom


Zoom property gets the value that represents the Zoom Command
KeyGesture   - N/A
UI Text         -   Zoom

This command indicates the intention to set the zoom level.
There is no implementation for responding to the Zoom command on any given WPF class. As such, you need to provide an appropriate implementation, which is shown in the example.
Example
<FlowDocumentPageViewer Name="flowDocumentPageViewer" MinZoom="50" MaxZoom="200">

  
  <FlowDocumentPageViewer.ContextMenu>
    <ContextMenu>
      <ContextMenu.CommandBindings>
        <CommandBinding
          Command="NavigationCommands.Zoom"
          CanExecute="navigationCommandZoom_CanExecute"
          Executed="navigationCommandZoom_Executed" />
      </ContextMenu.CommandBindings>
      <MenuItem Header="50%" Command="NavigationCommands.Zoom" CommandParameter="50" />
      <MenuItem Header="100%" Command="NavigationCommands.Zoom" CommandParameter="100" />
      <MenuItem Header="150%" Command="NavigationCommands.Zoom" CommandParameter="150" />
      <MenuItem Header="200%" Command="NavigationCommands.Zoom" CommandParameter="200" />
    </ContextMenu>
  </FlowDocumentPageViewer.ContextMenu>

  <FlowDocument>
    
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
 Sed vulputate, lacus non sagittis pharetra, diam dolor dictum tellus, 
et hendrerit odio risus nec erat. Nam sollicitudin imperdiet mi. Sed rutrum.
    </Paragraph>
...
  </FlowDocument>
</FlowDocumentPageViewer>
 
NavigationCommands.Favorites 

Favorites property gets the value that represents the Favorites Command
KeyGesture   -  CTRL+I
UI Text         -  Favorites

This command indicates the intention to manage and navigate to favorites.
Example

<MenuItem Command="NavigationCommands.Favorites">
  <MenuItem.CommandBindings>
    
    <CommandBinding
      Command="NavigationCommands.Favorites"
      CanExecute="navigationCommandFavorites_CanExecute"
      Executed="navigationCommandFavorites_Executed" />
  </MenuItem.CommandBindings>
</MenuItem>
...
<Frame Name="frame" NavigationUIVisibility="Hidden" Source="Page1.xaml" />