Thursday, June 27, 2013

Separator Line on Form [C#]

This example shows how to create a bevel line in Windows Forms. This line can be used as a visual separator of controls on a form.
Bevel line
To simulate the line in Windows Forms use a Label control. Set its Height to 2 pixels and BorderStyle to Fixed3D. Thats all, see the example.
[C#]
// separator bevel line
label1.AutoSize = false;
label1.Height = 2;
label1.BorderStyle = BorderStyle.Fixed3D;
 
source : http://www.csharp-examples.net/separator-line/ 

Wednesday, June 19, 2013

C# dataset insert to database and Table Value Parameters

Introduction

In this article, we explore the SQL server 2008 Table value parameters. Many articles have been published on this subject, some describe the SQL server side of things, others discuss how to access the table value functions from .NET.
I was unable to find one crisp article just describing a simple example of how to use Table value parameters using C# and SQL Server 2008 and after some research, I wrote this article so that other individuals may benefit from my findings.
Table value parameters are a simple mechanism to pass bulk of data from ADO.NET to SQL Server. They allow data to be available on the SQL Server as a temporary table.
This task in the past was done using XML in which the bulk data was modelled in a hierarchical structure and passed to the stored procedure. The stored procedure then converted back the structure into a temporary table to process the data in a relational manner.

Audience

The audience of this article is expected to be aware of ADO.NET, C# and SQL Server 2008.

Objective

To demonstrate an end to end example of table value parameters.

The Code

We will directly jump into the code for this article. We shall discuses each layer from the SQL Server Table to the C# code.

The Database Table

In this section, we describe the table schema for the sample problem. The table in this code will be a simple table with an identity column along with a nvarchar and integer column. The nvarchar column will not allow nulls and the integer will allow nulls. The schema creations script for the same is:
--This is the database table
CREATE TABLE dbo.SampleTable
(
Id int NOT NULL IDENTITY (1, 1),
SampleString nvarchar(64) NOT NULL,
SampleInt int NULL
) ON [PRIMARY]

The Table Value Parameter Type

To pass the data along as a table value parameter, a new database type has to be created. This type definition in essence describes what one row will look like and we will pass around a bunch of such rows. The script for the same is:
-- Create a table data type
CREATE TYPE [dbo].[SampleDataType] As Table
(
--This type has structure similar to the DB table 
SampleString Nvarchar(64) Not Null -- Having one String
, SampleInt Int -- and one int
)
One may observe that the structure of this type is similar to the table, however what is important is that we need to create the type definition most convenient to pass along the data which may mean de-normalization.

The Stored Procedure

The stored procedure is a rather simple piece of code which takes the SampleDataType type as input parameter. Inside the stored procedure, the parameter is available as a temporary table. This table however has to be readonly.
--This is the Stored Procedure
CREATE PROCEDURE [dbo].[SampleProcedure]
(
-- which accepts one table value parameter. 
-- It should be noted that the parameter is readonly
@Sample As [dbo].[SampleDataType] Readonly
)
AS

Begin
-- We simply insert values into the DB table from the parameter
-- The table value parameter can be used like a table with only read rights
Insert Into SampleTable(SampleString,SampleInt)
Select SampleString, SampleInt From @Sample
End

The DB Test Script

At this stage, we should quickly test our database for any errors using this script, which also describe the mechanism using which table value parameters may be passed along from one stored procedure to other.
-- This is the sample script to test the SP
-- An instance of the Table parameter type is created
Declare @SampelData As [dbo].[SampleDataType]
-- and then filled with the set of values
Insert Into @SampelData(SampleString, SampleInt) Values('1',1);
Insert Into @SampelData(SampleString, SampleInt) Values('2',null);
Insert Into @SampelData(SampleString, SampleInt) Values('3',3);
Select * From @SampelData
-- we then call the SP to store the values
Exec SampleProcedure @SampelData
Select * From SampleTable

The C# Code

In the C# code, we have to resolve two problems to use the required stored procedure.
  1. Create a data structure which is equivalent to the table value parameter - just the way a nvarchar is represented as string on the C# end.
  2. Pass this data to the Stored procedure.

Representing the Data

There are many mechanisms available to represent the data such as representing the data as a DataTable,IEnumerable, Linq object, Data reader, etc. In this article, we will focus on the DataTable. All we do is create aDataTable, define columns parallel to our Table data type and fill them up.
//To represent the table parameter in C#, we need to either 
//have a set of entities which are IEnumreable 
//or a data reader or a Data table.
//In this example we create a data table with same name as the type we have in the DB 
DataTable dataTable = new DataTable("SampleDataType"); 
//we create column names as per the type in DB 
dataTable.Columns.Add("SampleString", typeof(string)); 
dataTable.Columns.Add("SampleInt", typeof(Int32)); 
//and fill in some values 
dataTable.Rows.Add("99", 99); 
dataTable.Rows.Add("98", null); 
dataTable.Rows.Add("97", 99); 

Passing the Data

To pass the data, it is to be represented as a SqlParameter. The type of this parameter is Structured. The details are as shown in the code snippet. The other code to call the SP is trivial and may be seen in the given code.
SqlParameter parameter = new SqlParameter(); 
//The parameter for the SP must be of SqlDbType.Structured 
parameter.ParameterName="@Sample"; 
parameter.SqlDbType = System.Data.SqlDbType.Structured; 
parameter.Value = dataTable; 
command.Parameters.Add(parameter); 
Please note that the attached code is developed in Visual Studio 2010 + SQL Server 2008 however it will work with Visual Studio 2005 and SQL Server 2008.

source : http://www.codeproject.com/Articles/39161/C-and-Table-Value-Parameters


CREATE TYPE dbo.MyDataTable -- you can be more speciifc here
AS TABLE
(
  col1 INT,
  col2 DATETIME
  -- etc etc. The columns you have in your data table.
);
GO

CREATE PROCEDURE dbo.InsertMyDataTable
  @dt AS dbo.MyDataTable READONLY
AS
BEGIN
  SET NOCOUNT ON;

  INSERT dbo.RealTable(column list) SELECT column list FROM @dt;
END
GO
Now in your C# code:
DataTable tvp = new DataTable();
// define / populate DataTable

using (connectionObject)
{
    SqlCommand cmd = new SqlCommand("dbo.InsertMyDataTable", connectionObject);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter tvparam = cmd.Parameters.AddWithValue("@dt", tvp);
    tvparam.SqlDbType = SqlDbType.Structured;
    cmd.ExecuteNonQuery();
}
If you had given more specific details in your question, I would have given a more specific answer.
source : 

Visual Studio 2010, Build error, Build (web): Object reference not set to an instance of an object

Visual Studio 2010, Build error, Build (web): Object reference not set to an instance of an object

When upgrading a from previous version, Crystal report build provider will cause builder error, in ASP.NET 4 crystal reports are also pre-compiled using build provider for (.rpt), when compiler tries to pre-compile reports build in previous version, a build error "Object reference not set to an instance of an object" is thrown and build fails.

Possible work arround is to remove the build provider for (*.rpt) in your web.config:

There will be following construct under section:

<buildProviders>
<add extension=".rpt" type="CrystalDecisions.Web.Compilation.RptBuildProvider, CrystalDecisions.Web, Version=14.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
</buildProviders>

We can change this to:

<buildProviders>
<add extension=".rpt" type="CrystalDecisions.Web.Compilation.RptBuildProvider, CrystalDecisions.Web, Version=14.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<remove extension=".rpt"/>
</buildProviders>
What we have done is removed the build provider for (.rpt), in this way the crystal reports will remain as it is and no build error will show.

resource : http://software-works.blogspot.com/2010/05/visual-studio-2010-build-error-build.html

Sunday, June 16, 2013

Abstract, Interface Programming

āļ่āļ­āļ™āļŦāļ™้āļēāļ™ี้ āđ€āļĢāļēāļ็āđ„āļ”้āļĄีāļāļēāļĢāļžูāļ”āļ–ึāļ‡āđ€āļĢื่āļ­āļ‡āļ‚āļ­āOOP āļัāļ™āļĄāļēāļš้āļēāļ‡āđ€āļĨ็āļāļ™้āļ­āļĒ āļ‹ึ่āļ‡āļ็āļ—āļģāđƒāļŦ้āđ€āļĢāļēāđ„āļ”้āļĢู้āļ§่āļē āļ„ุāļ“āļĨัāļāļĐāļ“āļ°āļŠāļģāļ„ัāļāļ•่āļēāļ‡āđ†āļ‚āļ­āļ‡ OOP āļ—ี่āļˆāļģāđ€āļ›็āļ™āļ•้āļ­āļ‡āļĄีāļ™ั้āļ™ āļ็āļ›āļĢāļ°āļāļ­āļšāļ”้āļ§āļĒ Encapsulation, Inheritance āđāļĨāļ° Polymorphism āļ‹ึ่āļ‡āđāļ•่āļĨāļ°āļ­āļ‡āļ„์āļ›āļĢāļ°āļāļ­āļšāļ™ั้āļ™ āļ็āļ–ูāļāđāļš่āļ‡āļ­āļ­āļāđ€āļ›็āļ™āļ„ุāļ“āļĨัāļāļĐāļ“āļ°āļ‚āļ­āļ‡āđāļ•่āļĨāļ°āļ āļēāļĐāļēāļ­āļ­āļāđ„āļ›āļ•āļēāļĄāđāļ•่āļĨāļ°āđ„āļ§āļĒāļēāļāļĢāļ“์āļ‚āļ­āļ‡āļ āļēāļĐāļēāļ™ั้āļ™āđ† āđāļ•่āļ็āļĒัāļ‡āļĄีāļ§ิāļ˜ีāļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ—ี่āļ„āļĨ้āļēāļĒāđ†āļัāļ™āļ­āļĒู่

āļšāļ—āļ„āļ§āļēāļĄāļ™ี้ āļ‹ึ่āļ‡āļˆāļ°āļāļĨ่āļēāļ§āļ–ึāļ‡ Abstract Class āđāļĨāļ° Interface Programming āļ™ั้āļ™ āļˆāļ°āļ–ืāļ­āļ§่āļēāļ–ูāļāļˆัāļ”āļ­āļĒู่āđƒāļ™ Polymorphism āļŦāļĢืāļ­āļ็āļ„ืāļ­āļ„āļ§āļēāļĄāļŦāļĨāļēāļāļŦāļĨāļēāļĒāļ™ั่āļ™āđ€āļ­āļ‡ āļ‹ึ่āļ‡āļˆāļ°āļ­āļ˜ิāļšāļēāļĒāļ­āļ­āļāļĄāļēāļ”้āļ§āļĒāđ„āļ§āļĒāļēāļāļĢāļ“์āļ‚āļ­āļ‡āļ āļēāļĐāļē Java āđ€āļžāļĢāļēāļ°āđ€āļ›็āļ™āļ āļēāļĐāļēāļ—ี่āļ—āļģāļ„āļ§āļēāļĄāđ€āļ‚้āļēāđƒāļˆāđ„āļ”้āļ‡่āļēāļĒāļāļ§่āļē C++ āđƒāļ™āļŦāļĨāļēāļĒāđ†āļĨัāļāļĐāļ“āļ° āđāļĨāļ°āđ€āļ›็āļ™āļ āļēāļĐāļēāļ—ี่āļĄีāļœู้āđ€āļĢิ่āļĄāļ•้āļ™āđƒāļ™āļāļēāļĢāļĻึāļāļĐāļēāļāļēāļĢāđ‚āļ›āļĢāđāļāļĢāļĄāļĄิ่āļ‡ āļĻึāļāļĐāļēāļ­āļĒู่āļĄāļēāļāļžāļ­āļŠāļĄāļ„āļ§āļĢ

āļ่āļ­āļ™āļ­ื่āļ™ āđ€āļĢāļēāļ„āļ§āļĢāļˆāļ°āđ€āļ‚้āļēāđƒāļˆāļ„āļ§āļēāļĄāļŦāļĄāļēāļĒāļ‚āļ­āAbstract Class āđāļĨāļ° Interface Programming āđƒāļŦ้āļ”ีāļ่āļ­āļ™ āđ€āļžื่āļ­āļ—ี่āļˆāļ°āđ„āļ”้āļĻึāļāļĐāļēāđ„āļ”้āļ­āļĒ่āļēāļ‡āļ–ูāļāļ—āļēāļ‡

Abstract Class -> Class āļ—ี่āļ—āļģāļŦāļ™้āļēāļ—ี่āļĄāļēāđ€āļ›็āļ™ Super Class āļŠāļģāļŦāļĢัāļšāđ€āļžื่āļ­āđƒāļŦ้āļŠืāļšāļ—āļ­āļ”āđ‚āļ”āļĒāđ€āļ‰āļžāļēāļ° āđ‚āļ”āļĒāļˆāļ°āđ„āļĄ่āļŠāļēāļĄāļēāļĢāļ–āļ™āļģāļĄāļēāļŠāļĢ้āļēāļ‡āđ€āļ›็āļ™ Instance āđ€āļ­āļ‡āđ„āļ”้ āđ‚āļ”āļĒāļ„āļĨāļēāļŠāļĨูāļ āļˆāļ°āļ•้āļ­āļ‡āļŠืāļšāļ—āļ­āļ” āđāļĨāļ° āļ—āļģāļāļēāļĢ āđ€āļ‚ีāļĒāļ™āļāļēāļĢāļ—āļģāļ‡āļēāļ™āđƒāļŦāļĄ่(Override) āđƒāļŦ้ Abstract Method āļ—ั้āļ‡āļŦāļĄāļ”

Interface -> āđ€āļ›็āļ™āļ„ุāļ“āļŠāļĄāļšัāļ•ิāđ€āļžื่āļ­āđƒāļŦ้ Class āļ—āļģāļāļēāļĢ Implement (āļ„āļĨ้āļēāļĒāļāļēāļĢāļŠืāļšāļ—āļ­āļ”) āđƒāļŠ้āđ€āļžื่āļ­āđ€āļžิ่āļĄāļ„ุāļ“āļŠāļĄāļšัāļ•ิāđƒāļŦ้ Class āļ™ั้āļ™āđ† āđ‚āļ”āļĒāļˆāļ°āļ•้āļ­āļ‡āļ—āļģāļāļēāļĢ Override Method āļ•่āļēāļ‡āđ†āđƒāļ™ Interface āđƒāļŦ้āļŦāļĄāļ”

Abstract Class


āđ€āļĄื่āļ­āļ­่āļēāļ™āļĄāļēāļ–ึāļ‡āļ•āļ­āļ™āļ™ี้ āļ­āļēāļˆāļĄีāļœู้āļ­่āļēāļ™āļšāļēāļ‡āļ„āļ™āļŠāļ‡āļŠัāļĒāļ§่āļē Abstract Class āļ™ั้āļ™ āļĄีāļ„āļ§āļēāļĄāļˆāļģāđ€āļ›็āļ™āļ­āļĒ่āļēāļ‡āđ„āļĢ āđ€āļ™ื่āļ­āļ‡āļˆāļēāļāļŦāļēāļāđ€āļĢāļēāļˆāļ°āļŠāļĢ้āļēāļ‡ Class āļ‚ึ้āļ™āļĄāļē Class āļŦāļ™ึ่āļ‡āđāļĨ้āļ§ āļ—āļģāđ„āļĄāđ€āļĢāļēāļˆึāļ‡āļˆāļ°āļ•้āļ­āļ‡āđ„āļĄ่āđƒāļŦ้ Class āļ™ั้āļ™āđ† āļŠāļēāļĄāļēāļĢāļ–āļŠāļĢ้āļēāļ‡ Instance āļ‚ึ้āļ™āļĄāļēāļ”้āļ§āļĒ āļ™่āļēāļˆāļ°āļŠāļĢ้āļēāļ‡āļ‚ึ้āļ™āļĄāļēāđƒāļŦ้āđ€āļ›็āļ™ Class āļ›āļāļ•ิ āļ็āļĒัāļ‡āļŠāļēāļĄāļēāļĢāļ–āļ–ูāļāļŠืāļšāļ—āļ­āļ”āđ„āļ”้āļ­āļĒู่āļ”ี

āļ„āļģāļ•āļ­āļšāđƒāļ™āļŠ่āļ§āļ™āļ™ี้ āļŦāļēāļāļˆāļ°āļ­āļ˜ิāļšāļēāļĒāđāļĨ้āļ§ āļ„āļ‡āļ•้āļ­āļ‡āļ­āļ˜ิāļšāļēāļĒāļ”้āļ§āļĒāļāļēāļĢāļĒāļāļ•ัāļ§āļ­āļĒ่āļēāļ‡ āļˆāļ°āđ€āļ‚้āļēāđƒāļˆāļ‡่āļēāļĒāļ—ี่āļŠุāļ” āđ€āļĢิ่āļĄāļ”้āļ§āļĒāļāļēāļĢāđāļŠāļ”āļ‡āļ–ึāļ‡āđ‚āļ„āļĢāļ‡āļŠāļĢ้āļēāļ‡āļ‚āļ­āļ‡āļāļēāļĢāļŠืāļšāļ—āļ­āļ” āļ”ัāļ‡āļ‚้āļēāļ‡āļĨ่āļēāļ‡āļˆāļ°āļ”ีāļāļ§่āļē



āļˆāļēāļāļĢูāļ›āļ”้āļēāļ™āļšāļ™ āđ€āļ›็āļ™āļāļēāļĢāđāļŠāļ”āļ‡āļāļēāļĢāļŠืāļšāļ—āļ­āļ” āđ‚āļ”āļĒāļˆāļ°āļĄี Superclass āđ€āļ›็āļ™ Class āļŠื่āļ­ Shape(āļĢูāļ›āļĢ่āļēāļ‡) āļ‹ึ่āļ‡āļˆāļ°āļĄี Class āļ—ี่āļĄāļēāļŠืāļšāļ—āļ­āļ” āļ็āļ„ืāļ­ Circle(āļ§āļ‡āļāļĨāļĄ) Triangle(āļŠāļēāļĄāđ€āļŦāļĨี่āļĒāļĄ) āđāļĨāļ° Rectangle(āļŠี่āđ€āļŦāļĨี่āļĒāļĄ)

āļ—ีāļ™ี้้ āđ€āļĢāļēāļ•้āļ­āļ‡āļāļēāļĢāļ—ี่āļˆāļ°āļšัāļ‡āļ„ัāļšāđƒāļŦ้āđāļ•่āļĨāļ° Class āļ—ี่āļŠืāļšāļ—āļ­āļ”āļˆāļēāļ Shape āļĄีāļ„āļ§āļēāļĄāļŠāļēāļĄāļēāļĢāļ–āđƒāļ™āļāļēāļĢ "āļŦāļēāļžื้āļ™āļ—ี่" (calArea()) āļ‹ึ่āļ‡āđ€āļĢāļēāļŠāļēāļĄāļēāļĢāļ–āļ—āļģāđ„āļ”้ āđ‚āļ”āļĒāļāļģāļŦāļ™āļ”āđƒāļŦ้ Class Shape āļ™ั้āļ™ āļĄี Method calArea() āļ‚ึ้āļ™āļĄāļē āđāļĨāļ°āđ€āļĄื่āļ­ Class āđƒāļ”āđ† āļ—āļģāļāļēāļĢāļŠืāļšāļ—āļ­āļ” Class Shape āđ„āļ›āđāļĨ้āļ§ āļ็āļˆāļ°āļ–ูāļāļšัāļ‡āļ„ัāļšāđƒāļŦ้āļŠืāļšāļ—āļ­āļ” Method calArea() āđ„āļ›āļ”้āļ§āļĒ

āđāļ•่āđ€āļ™ื่āļ­āļ‡āļˆāļēāļ āļĢูāļ›āļĢ่āļēāļ‡(Shape) āļ™ั้āļ™ āđ€āļ›็āļ™āļŠิ่āļ‡āļ—ี่āļ–ูāļāļŠāļĄāļĄุāļ•ิāļ‚ึ้āļ™āļĄāļē āđ„āļĄ่āļĄีāļĢูāļ›āļĢ่āļēāļ‡āļ—ี่āđāļ™่āļ™āļ­āļ™ āļˆึāļ‡āđ„āļĄ่āļŠāļēāļĄāļēāļĢāļ–āđ€āļ‚ีāļĒāļ™āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ āļēāļĒāđƒāļ•้ Method calArea() āđ„āļ”้ āđ€āļĢāļēāļˆึāļ‡āļ•้āļ­āļ‡āļŠāļĢ้āļēāļ‡ calArea() āļ‚āļ­āļ‡ Shape āđƒāļŦ้āđ€āļ›็āļ™ Abstract Method āđ€āļžื่āļ­āļ—ี่āļˆāļ°āđ„āļ”้āđ„āļĄ่āļ•้āļ­āļ‡āļāļģāļŦāļ™āļ”āļāļēāļĢāļ—āļģāļ‡āļēāļ™āđƒāļŦ้ Method āļ™ี้

Class Shape
public abstract class Shape {
public abstract float calArea();
}

āđ€āļĄื่āļ­ Class āļ—ี่āđ€āļŦāļĨืāļ­āļ—ั้āļ‡ 3 āļ—āļģāļāļēāļĢāļŠืāļšāļ—āļ­āļ” Class Shape āđ„āļ›āđāļĨ้āļ§ āļ็āļˆāļģāđ€āļ›็āļ™āļ•้āļ­āļ‡āļ—āļģāļāļēāļĢ Override Abstract Method āļ‚āļ­āļ‡ Superclass āļ—ั้āļ‡āļŦāļĄāļ”


Class Circle
public class Circle extends Shape {
double radius;
public Circle(double radius)
{
this.radius = radius;
}

@Override
public double calArea() {
return Math.PI * Math.pow(radius, 2);
}
}



Class Rectangle
public class Rectangle extends Shape {
double lenght;
double height;


public Rectangle(double lenght, double height)
{
this.lenght = lenght;
this.height = height;
}

@Override
public double calArea() {
return lenght * height;
}
}


Class Triangle
public class Triangle extends Shape {
double base;
double height;


public Triangle(double base, double height)
{
this.base = base;
this.height = height;
}

@Override
public double calArea() {
return base * height * 0.5;
}
}

āđ€āļĢāļēāļ็āļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āđ€āļĢีāļĒāļāđƒāļŠ้āđƒāļ™ main() āđ„āļ”้āļ”ัāļ‡āļ™ี้

public class MainClass {
public static void main(String[] args) {
Circle c = new Circle(5);
Triangle t = new Triangle(3, 6);
Rectangle r = new Rectangle(10, 5);

System.out.println(c.calArea());
System.out.println(t.calArea());
System.out.println(r.calArea());
}
}


āļ‹ึ่āļ‡āļ็āđ€āļŦāļĄืāļ­āļ™āļ§่āļē āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđ€āļĢāļēāļˆāļ°āļŠāļĄāļšูāļĢāļ“์āđāļĨ้āļ§ āđāļ•่āļŦāļēāļāđ€āļĢāļēāļĨāļ­āļ‡āļ„ิāļ”āļ§่āļē āđ€āļĢāļēāļĄี Class āļ—ี่āļŠืāļšāļ—āļ­āļ”āļĄāļēāļˆāļēāļ Shape āļ‹ัāļāļĢ้āļ­āļĒ Class āļ‹ึ่āļ‡āļāļēāļĢāđ€āļĢีāļĒāļ calArea() āļ—ีāļĨāļ° Class āļ™ั้āļ™ āđ€āļ›็āļ™āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ—ี่āļĒāļēāļāļĄāļēāļ āļ–้āļēāđ€āļĢāļēāļˆāļ°āļ•้āļ­āļ‡āđ„āļĨ่āđ€āļ‚ีāļĒāļ™āđ‚āļ„้āļ”āđ„āļ›āļ—ีāļĨāļ° Class āđāļĨāļ°āļŦāļēāļāļĄีāļāļēāļĢāđ€āļ›āļĨี่āļĒāļ™āđāļ›āļĨāļ‡āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļšāļēāļ‡āļ­āļĒ่āļēāļ‡ āļ็āļ„āļ‡āļˆāļ°āļĒāļēāļāđƒāļ™āļāļēāļĢāđāļ้āđ„āļ‚āđ€āļŠ่āļ™āļัāļ™

āđ€āļ™ื่āļ­āļ‡āļˆāļēāļāđāļ•่āļĨāļ° Class āļŠืāļšāļ—āļ­āļ”āļĄāļēāļˆāļēāļ Class āđ€āļ”ีāļĒāļ§āļัāļ™ āđ€āļĢāļēāļˆึāļ‡āļŠāļēāļĄāļēāļĢāļ–āļ™āļģ Class āļ—ั้āļ‡ 3 āļŠāļ™ิāļ”āļ™ี้ āļŦāļĢืāļ­āļˆāļ°āļ‹ัāļ āļĢ้āļ­āļĒāļžัāļ™āļŠāļ™ิāļ” āļ—ี่āļŠืāļšāļ—āļ­āļ”āļĄāļēāļˆāļēāļ Class āļ™ี้ āļĄāļēāđ€āļ็āļšāđƒāļ™ Array āđ€āļ”ีāļĒāļ§āļัāļ™ āđāļĨāļ°āļ§āļ™āļĨูāļ›āļ—āļģāđ„āļ”้

public class MainClass {
public static void main(String[] args) {
Shape[] s = {new Circle(5), new Triangle(3, 6), new Rectangle(10, 5)};
for(int i = 0; i < s.length; i++)
System.out.println(s[i].calArea());
}
}

āļ§ิāļ˜ีāļ™ี้ āļˆāļ°āļĄีāļ›āļĢāļ°āđ‚āļĒāļŠāļ™์āļĄāļēāļ āđ€āļĄื่āļ­āđ€āļĢāļēāļ•้āļ­āļ‡āđƒāļŠ้ Class āļ—ี่āļŦāļĨāļēāļāļŦāļĨāļēāļĒāļŠāļ™ิāļ” āđāļ•่āļ็āļ–ืāļ­āļ§่āļēāđ€āļ›็āļ™āļŠāļ™ิāļ”āđ€āļ”ีāļĒāļ§āļัāļ™ (Subclass is a Superclass) āļ‹ึ่āļ‡āļ—āļģāđƒāļŦ้āđ€āļĢāļēāđ„āļĄ่āļ•้āļ­āļ‡āđ€āļ‚ีāļĒāļ™āļāļēāļĢāļ„āļ§āļšāļ„ุāļĄāļŦāļĨāļēāļĒāđ†āļ„āļĢั้āļ‡ āļัāļšāļāļēāļĢāļ—āļģāļ‡āļēāļ™āļŠāļ™ิāļ”āđ€āļ”ีāļĒāļ§āļัāļ™

āļ™āļ­āļāļˆāļēāļāļ™ี้ āđ€āļĢāļēāļĒัāļ‡āļŠāļēāļĄāļēāļĢāļ–āļ™āļģāļ§ิāļ˜ีāļ™ี้ āļĄāļēāđƒāļŠ้āļัāļšāļāļēāļĢāļŠ่āļ‡āļœ่āļēāļ™ Parameter āļ—ี่āđ€āļĢāļēāļ•้āļ­āļ‡āļāļēāļĢāļ„āļ§āļšāļ„ุāļĄāļ‚āļ­āļšāđ€āļ‚āļ•āļ‚āļ­āļ‡āļŠāļ™ิāļ”āļ‚āļ­āArgument āđ„āļ”้āļ­ีāļāļ”้āļ§āļĒ



public class MainClass {
private static void printShape(Shape shape)
{
System.out.println(shape.calArea());
}

public static void main(String[] args) {
Circle c = new Circle(5);
Triangle t = new Triangle(3, 6);
Rectangle r = new Rectangle(10, 5);

printShape(c);
printShape(t);
printShape(r);
}
}


Interface


āđ€āļĢื่āļ­āļ‡āļ‚āļ­āļ‡ Interface āļ™ั้āļ™ āļŦāļēāļāļˆāļ°āļĻึāļāļĐāļē āļ„āļ§āļĢāļˆāļ°āļ—āļģāļāļēāļĢāļĻึāļāļĐāļēāđ€āļĢื่āļ­āļ‡āļ‚āļ­āļ‡ Abstract Class āđƒāļŦ้āđ€āļ‚้āļēāđƒāļˆāđ€āļŠีāļĒāļ่āļ­āļ™ āđ€āļžāļĢāļēāļ° Interface āļ™ั้āļ™ āđāļĄ้āđāļ™āļ§āļ„ิāļ”āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļˆāļ°āļ•่āļēāļ‡āļัāļ™ āđāļ•่āļāļēāļĢāļ™āļģāļĄāļēāđƒāļŠ้āļ™ั้āļ™ āļ„āļĨ้āļēāļĒāļัāļ™āļĄāļēāļ

āđ€āļĄื่āļ­āļžูāļ”āļ–ึāļ‡ Interface āđāļĨ้āļ§ āđ€āļĢāļēāļ็āļĄัāļāļˆāļ°āļ™ึāļāļ–ึāļ‡ āļ„ุāļ“āļŠāļĄāļšัāļ•ิāļ•่āļēāļ‡āđ† āļ—ี่āļ™āļģāļĄāļēāđ€āļžิ่āļĄāđƒāļŦ้āļัāļš Class āđ€āļŠ่āļ™āļŠāļĄāļĄุāļ•ิāļ§่āļē āđ€āļĢāļēāļĄี Interface 2 āļ•ัāļ§ āļŠื่āļ­āļ§่āļē IFlyAble(āļšิāļ™āđ„āļ”้) āļัāļš IRunAble(āļ§ิ่āļ‡āđ„āļ”้) āđ‚āļ”āļĒāđ€āļĢāļēāļāļģāļŦāļ™āļ”āđƒāļŦ้ Interface āļ—ั้āļ‡ 2 āļĄีāļĨัāļāļĐāļ“āļ°āļ”ัāļ‡āļ™ี้

public interface IFlyAble {
public void Fly();
}



public interface IRunAble {
public void Run();
}

āđāļĨāļ°āđ€āļĢāļēāļ็āļĄี Class āļ‚ึ้āļ™āļĄāļēāļ­ีāļ 3 Class āļ„ืāļ­ Human(āļ„āļ™), Bird(āļ™āļ) āđāļĨāļ° Plane(āđ€āļ„āļĢื่āļ­āļ‡āļšิāļ™) āđ‚āļ”āļĒ Human āļ™ั้āļ™ āļŠāļēāļĄāļēāļĢāļ–āļ§ิ่āļ‡āđ„āļ”้ Bird āļŠāļēāļĄāļēāļĢāļ–āļšิāļ™āđ„āļ”้ āđāļĨāļ° Plane āļ—āļģāđ„āļ”้āļ—ั้āļ‡āļ§ิ่āļ‡āđāļĨāļ°āļšิāļ™

āļ‹ึ่āļ‡āđ€āļĄื่āļ­āđ€āļĢāļēāđ€āļ­āļēāļ—ั้āļ‡ 3 Class āļĄāļēāđ€āļ‚ีāļĒāļ™āđ€āļ›็āļ™ Code āđāļĨ้āļ§ āđ€āļĢāļēāļ็āļ•้āļ­āļ‡āļ—āļģāļāļēāļĢ Override Method āļ—ั้āļ‡āļŦāļĄāļ”āļ‚āļ­āļ‡ Interface āļ—ี่āđāļ•่āļĨāļ° Class āļ—āļģāļāļēāļĢ Implement āļĄāļē

public class Human implements IRunAble {
@Override
public void Run() {
System.out.println("Human Run");
}
}

public class Bird implements IFlyAble{
@Override
public void Fly() {
System.out.println("Bird Fly");
}
}

public class Plane implements IFlyAble, IRunAble{
@Override
public void Run() {
System.out.println("Plane Run");
}


@Override
public void Fly() {
System.out.println("Plane Fly");
}
}

āļ—ีāļ™ี้ āđ€āļĢāļēāļˆāļ°āļĨāļ­āļ‡āļŠāļĢ้āļēāļ‡ Method āļ‚ึ้āļ™āļĄāļē āļŠื่āļ­āļ§่āļē goFly() āļัāļš goRun() āđ‚āļ”āļĒāļ—ี่āļāļģāļŦāļ™āļ”āļ§่āļē Argument āļ—ี่āļ–ูāļāļŠ่āļ‡āļĄāļēāļ™ั้āļ™ āļŦāļēāļāļŠ่āļ‡āđ€āļ‚้āļē goFly() āļ็āļˆāļ°āļ•้āļ­āļ‡āļšิāļ™āđ„āļ”้ (implement IFlyAble) āđāļĨāļ° āļŦāļēāļāļŠ่āļ‡āđ€āļ‚้āļē goRun() āļ็āļˆāļ°āļ•้āļ­āļ‡āļ§ิ่āļ‡āđ„āļ”้ (implement IRunAble) āđ€āļŠ่āļ™āļัāļ™

private static void goFly(IFlyAble flyAble)
{
flyAble.Fly();
}

private static void goRun(IRunAble runAble)
{
runAble.Run();
}
āđ€āļĢāļēāļ็āļŠāļēāļĄāļēāļĢāļ–āļ™āļģāļĄāļēāđ€āļĢีāļĒāļāđƒāļŠ้āđƒāļ™ main āđ„āļ”้āļ”ัāļ‡āļ™ี้

public static void main(String[] args) {
Human h = new Human();
Bird b = new Bird();
Plane p = new Plane();

goRun(h);
goFly(b);
goRun(p);
goFly(p);
}
āđāļ•่āđ€āļĢāļēāđ„āļĄ่āļŠāļēāļĄāļēāļĢāļ–āļŠั่āļ‡āļ„āļ™āđ„āļ›āļšิāļ™āđ„āļ”้ āđ€āļžāļĢāļēāļ°āļ„āļ™āļšิāļ™āđ„āļĄ่āđ„āļ”้(āđ„āļĄ่āđ„āļ”้ implement IFlyAble) āļāļēāļĢāđ€āļĢีāļĒāļ Code āļĨัāļāļĐāļ“āļ°āļ™ี้āļˆึāļ‡āļ—āļģāđ„āļĄ่āđ„āļ”้

goFly(h);

āļ›āļĢāļ°āđ‚āļĒāļŠāļ™์āļ‚āļ­āļ‡āļāļēāļĢāđƒāļŠ้ Interface āļ™ั้āļ™ āđ€āļĢāļēāļĄัāļāļˆāļ°āđƒāļŠ้āđ€āļĄื่āļ­āļœู้āļŠāļĢ้āļēāļ‡ Method āļ•้āļ­āļ‡āļāļēāļĢāļāļģāļŦāļ™āļ”āļ§่āļē Class āđƒāļ”āđ† āļ—ี่āļˆāļ°āļ–ูāļāļŠ่āļ‡āļĄāļēāđ€āļ›็āļ™ Argument āļˆāļ°āļ•้āļ­āļ‡āļĄีāļ„ุāļ“āļŠāļĄāļšัāļ•ิāļ•āļēāļĄāļ—ี่āļāļģāļŦāļ™āļ”āđ„āļ§้ āļŦāļĢืāļ­āļ็āļ„ืāļ­āļāļēāļĢ Implement Interface āļ—ี่āļāļģāļŦāļ™āļ”āđ„āļ§้āļ™ั่āļ™āđ€āļ­āļ‡ āđ€āļžāļĢāļēāļ°āđƒāļ™āļ„āļ§āļēāļĄāđ€āļ›็āļ™āļˆāļĢิāļ‡ āļœู้āļ—ี่āļŠāļĢ้āļēāļ‡ Method āļัāļšāļœู้āļ—ี่āļŠāļĢ้āļēāļ‡ Class āļ­āļēāļˆāđ€āļ›็āļ™āļ„āļ™āļĨāļ°āļ„āļ™āļัāļ™ āđāļĨāļ°āļ­āļēāļˆāđ„āļĄ่āđ€āļ„āļĒāļ„ุāļĒāļัāļ™āļŦāļĢืāļ­āđ€āļˆāļ­āļัāļ™āđ€āļĨāļĒāļ‹ัāļāļ„āļĢั้āļ‡āļ็āđ„āļ”้ āļˆึāļ‡āđ„āļĄ่āļ­āļēāļˆāļŠื่āļ­āļŠāļēāļĢāđƒāļŦ้ Class āļ—ี่āļŠ่āļ‡āđ€āļ‚้āļēāļĄāļē āļĄีāļ„ุāļ“āļŠāļĄāļšัāļ•ิāđ€āļžีāļĒāļ‡āļžāļ­āļ•่āļ­āļāļēāļĢāļ™āļģāđ„āļ›āđƒāļŠ้ āļ‰āļ°āļ™ั้āļ™ āļœู้āļŠāļĢ้āļēāļ‡ Method āļˆึāļ‡āļ—āļģāļāļēāļĢāļŠāļĢ้āļēāļ‡ Interface āļ‚ึ้āļ™āļĄāļē āđāļĨāļ°āļāļģāļŦāļ™āļ”āļ§่āļē āļŦāļēāļāđƒāļ„āļĢāļ็āļ•āļēāļĄ āļ•้āļ­āļ‡āļāļēāļĢāļ—ี่āļˆāļ°āļ™āļģ Class āđƒāļ”āđ†āļŠ่āļ‡āļĄāļēāđ€āļ›็āļ™ Argument āđāļĨ้āļ§ āļ็āļ•้āļ­āļ‡āļ—āļģāļāļēāļĢ implement interface āļ™ั้āļ™āđ†āļ”้āļ§āļĒ

āļŦāļĨัāļ‡āļˆāļēāļāļ—ี่āđ„āļ”้āļžูāļ”āļ–ึāļ‡āļ—ั้āļ‡ Abstract Class āđāļĨāļ° Interface āļĄāļēāļ­āļĒ่āļēāļ‡āļĒāļēāļ§āļ™āļēāļ™ āļ็āļ„ิāļ”āļ§่āļēāļ—ุāļāļ„āļ™āļ„āļ‡āđ„āļ”้āļ­āļ°āđ„āļĢāđ„āļ›āđ„āļĄ่āļĄāļēāļāļ็āļ™้āļ­āļĒāļš้āļēāļ‡ āļ‚āļ­āđƒāļŦ้āļ—ุāļāļ„āļ™āļĨāļ­āļ‡āļ™āļģāļ§ิāļ˜ีāļāļēāļĢāļ™ี้ āđ„āļ›āđƒāļŠ้āđƒāļ™āļāļēāļĢāđ€āļ‚ีāļĒāļ™āļš้āļēāļ‡ āđ€āļžāļĢāļēāļ°āļ„āļ‡āļŠ่āļ§āļĒāļĨāļ”āđ€āļ§āļĨāļēāđƒāļ™āļāļēāļĢāļ—āļģāļ‡āļēāļ™ āđ„āļĄ่āļĄāļēāļ āļ็āļ™้āļ­āļĒ :)

Wednesday, June 12, 2013

Problem during ClickOnce deployment.

I just finished figuring out a problem I was having with a ClickOnce deployment for a project I've been working on.

This project has a reference to ADODB.dll -- a Primary Interop Reference provided by Microsoft in the .NET Framework SDK. Unfortunately, that PIA isn't provided in the .NET Framework redistributable. Also, unfortunately, setting the adodb.dll to copy-local during the project-build process didn't help during ClickOnce deployment -- the ADODB.dll was specified as needing to be installed into the GAC before my app would install.

I did some searching around, and stumbled upon this post as the MSDN ClickOnce forums:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=323832&SiteID=1

You can find my response there, but if you're averse to clicking, read-on.

Basically, the problem boils down to the default ClickOnce publishing behavior for the ADODB PIA. For whatever reason, it marks the ADODB PIA as a pre-requisite in the applications .manifest file. This means that ClickOnce requires the ADODB PIA to be installed into the GAC before it will allow my app to be installed. Unfortunately, none of the bootstrapper pre-reqs installed the PIA into the GAC, (making one of those pre-req bootstrapper installers is a possible solution, and I mention it in the reply, but it's not the easiest solution.)

What fixes is the problem is modifying the 'Publish Status' of the ADODB.dll
in the project's ClickOnce publishing settings. You change ADODB.dll's publish-status from the default "Include (Auto)" to "Include".

Yes. 

Change it from one setting to another setting that appears to be the exact samesetting.
Re-publish. The applications .manifest now specifies ADODB.dll to 'install' -- which will cause ClickOnce to copy it to the install folder, instead of requiring it be installed into the GAC.


Here's a couple screen shots for ya:

Bring up your Project Properties Page, and go to the Publish tab. Click the "Application Files..." button:

In the dialog that pops up, set ADODB.dll's Publish Status to Include:

source : http://yoopergeek.blogspot.com/2006/03/problem-during-clickonce-deployment.html