The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed.
you can not use the out paramter to pass a value to the method. With ref you can.
Example:
class OutExample
{
static void SplitName(string fullName, out string firstName, out string lastName)
{
// NOTE: firstName and lastName have not been assigned to yet. Their values cannot be used.
int spaceIndex = fullName.IndexOf(' ');
firstName = fullName.Substring(0, spaceIndex).Trim();
lastName = fullName.Substring(spaceIndex).Trim();
}
static void Main()
{
string fullName = "Yuan Sha";
string firstName;
string lastName;
// NOTE: firstName and lastName have not been assigned yet. Their values may not be used.
SplitName(fullName, out firstName, out lastName);
// NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.
System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName);
}
}
//------------------------------
class RefExample
{
static object FindNext(object value, object[] data, ref int index)
{
while (index < data.Length)
{
if (data[index].Equals(value))
{
return data[index];
}
index += 1;
}
return null;
}
static void Main()
{
object[] data = new object[] {1,2,3,4,2,3,4,5,3};
int index = 0;
// NOTE: must assign to index before passing it as a ref parameter
while (FindNext(3, data, ref index) != null)
{
// NOTE: that FindNext may have modified the value of index
System.Console.WriteLine("Found at index {0}", index);
index += 1;
}
System.Console.WriteLine("Done Find");
}
}
Search This Blog
2009-09-07
difference between Constant And Readonly
What is the difference between constant and readonly?
'const':
Can't be static.
Value is evaluated at compile time.
Initiailized at declaration only.
'readonly':
Can be either instance-level or static.
Value is evaluated at run time.
Can be initialized in declaration or by code in the constructor.
You can't use the static keyword with a const because it would be redundant. You can think of const as a subset of static.
Consts are static, not instance members.
public class Constant
{
public const int C = 12345;
}
------------
public class ReadOnly
{
private readonly int r = 1; //variable initialization upon declaration is possible for readonly variables
public ReadOnly()
{
r = 2; //assignment to a readonly variable is possible in the constructor
}
public int R
{
get { return r; }
set { r = value; } //this line won't compile because r is declared a readonly
}
}
'const':
Can't be static.
Value is evaluated at compile time.
Initiailized at declaration only.
'readonly':
Can be either instance-level or static.
Value is evaluated at run time.
Can be initialized in declaration or by code in the constructor.
You can't use the static keyword with a const because it would be redundant. You can think of const as a subset of static.
Consts are static, not instance members.
public class Constant
{
public const int C = 12345;
}
------------
public class ReadOnly
{
private readonly int r = 1; //variable initialization upon declaration is possible for readonly variables
public ReadOnly()
{
r = 2; //assignment to a readonly variable is possible in the constructor
}
public int R
{
get { return r; }
set { r = value; } //this line won't compile because r is declared a readonly
}
}
2009-08-27
Find missing and duplicate number from a sequence of numbers in SQL table
Sometimes it is important to know which values in a sequence are missing, either to find unused values so they can be used, or to find “holes” in the data. In this article I’ll show you how to find missing values.
CREATE TABLE [dbo].[test](
[custid] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[custid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
//---------------------------------------
Insert the following values in the custid.
custid
-------
1
2
3
4
6
7
8
9
10
15
16
17
18
19
20
To Find missing no the SQL query should be:
select m.custid +1 as start
from test as m
left outer join test as r on m.custid = r.custid - 1
where r.custid is null
Output-
Start
---------
5
11
21
Description:first replicate the table as m and r,then its finding the null value from r(where r.custid is null) and joining with the previous value of null i.e r.custid-1 with the m table.
first it finds 4 as 5 is not in the r table and join 4 with the row of m where m.custid contains 4,similarly for 10 and 20.then it returns m.custid+1.
To find the range of missing values:
select start, stop from (
select m.custid + 1 as start,
(select min(custid) - 1 from test as x where x.custid > m.custid) as stop
from test as m
left outer join test as r on m.custid = r.custid - 1
where r.custid is null
) as x
where stop is not null;
Start Stop
------------
5 5
11 14
you can rewrite this query (without subqueries) as follows.
select l.custid + 1 as start, min(fr.custid) - 1 as stop
from test as l
left outer join test as r on l.custid = r.custid - 1
left outer join test as fr on l.custid <> m.custid) as stop
from test as m
left outer join test as r on ascii(m.custid) = ascii(r.custid) - 1
where r.custid is null
) as x
where stop <> '';
How to find duplicate record in sql server:
select id, count(*) from sequence
group by id
having count(*) > 1;
Reference:http://www.xaprb.com/blog/2005/12/06/find-missing-numbers-in-a-sequence-with-sql/
CREATE TABLE [dbo].[test](
[custid] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[custid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
//---------------------------------------
Insert the following values in the custid.
custid
-------
1
2
3
4
6
7
8
9
10
15
16
17
18
19
20
To Find missing no the SQL query should be:
select m.custid +1 as start
from test as m
left outer join test as r on m.custid = r.custid - 1
where r.custid is null
Output-
Start
---------
5
11
21
Description:first replicate the table as m and r,then its finding the null value from r(where r.custid is null) and joining with the previous value of null i.e r.custid-1 with the m table.
first it finds 4 as 5 is not in the r table and join 4 with the row of m where m.custid contains 4,similarly for 10 and 20.then it returns m.custid+1.
To find the range of missing values:
select start, stop from (
select m.custid + 1 as start,
(select min(custid) - 1 from test as x where x.custid > m.custid) as stop
from test as m
left outer join test as r on m.custid = r.custid - 1
where r.custid is null
) as x
where stop is not null;
Start Stop
------------
5 5
11 14
you can rewrite this query (without subqueries) as follows.
select l.custid + 1 as start, min(fr.custid) - 1 as stop
from test as l
left outer join test as r on l.custid = r.custid - 1
left outer join test as fr on l.custid <> m.custid) as stop
from test as m
left outer join test as r on ascii(m.custid) = ascii(r.custid) - 1
where r.custid is null
) as x
where stop <> '';
How to find duplicate record in sql server:
select id, count(*) from sequence
group by id
having count(*) > 1;
Reference:http://www.xaprb.com/blog/2005/12/06/find-missing-numbers-in-a-sequence-with-sql/
2009-08-14
GridView with Fixed Header and Freeze Column
Code Snippent:
protected void GridLoad()
{
/*
//For DB Connection and all other operation in Grid
SqlConnection sqlCon = new SqlConnection();
DPE dpe = new DPE();
sqlCon = dpe.DatabaseConnection();
SqlCommand cmd1 = new SqlCommand("MDOPEDS.OUTDATESRESULTS", sqlCon);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
adp.Fill(ds);
grvOutdatesData.DataSource = ds.Tables[0];
grvOutdatesData.DataBind();
Session["OutdateDataset"] = ds.Tables[0];
int colcount = ds.Tables[0].Columns.Count - 1;
int rowcount = ds.Tables[0].Rows.Count - 1;
hdn1.Value = rowcount.ToString();
for (int d = 0; d <= rowcount; d++)
{
TextBox txt = new TextBox();
txt.ID = "txtOutdates" + d;
txt.Text = grvOutdatesData.Rows[d].Cells[colcount].Text.Trim();
txt.Width = 40;
txt.Height = 12;
txt.Attributes.Add("runat='server' class", "inputTextBox1");
grvOutdatesData.Rows[d].Cells[colcount].Controls.Add(txt);
}
*/
//Freezing rows and column
for (int c = 1; c <= colcount; c++)
{
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("Width", "45px");
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("writing-mode", "tb-rl");//for vertical align of header
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("filter", "flipv fliph");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("position", "relative");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("TOP", "expression(this.offsetParent.scrollTop)");
}
grvOutdatesData.HeaderRow.Cells[0].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[1].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[2].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[3].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
foreach (GridViewRow grv in grvOutdatesData.Rows)
{
grv.Cells[0].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[0].Attributes.CssStyle.Add("position", "relative");
grv.Cells[0].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[1].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[1].Attributes.CssStyle.Add("position", "relative");
grv.Cells[1].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[2].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[2].Attributes.CssStyle.Add("position", "relative");
grv.Cells[2].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[3].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[3].Attributes.CssStyle.Add("position", "relative");
grv.Cells[3].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
for (int b = 0; b < colcount; b++)
{
if (grv.Cells[b].Text == " ")
{
grv.Cells[b].Attributes.CssStyle.Add("BACKGROUND-COLOR", "#f19c76");
}
}
}
}
Output:
protected void GridLoad()
{
/*
//For DB Connection and all other operation in Grid
SqlConnection sqlCon = new SqlConnection();
DPE dpe = new DPE();
sqlCon = dpe.DatabaseConnection();
SqlCommand cmd1 = new SqlCommand("MDOPEDS.OUTDATESRESULTS", sqlCon);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
adp.Fill(ds);
grvOutdatesData.DataSource = ds.Tables[0];
grvOutdatesData.DataBind();
Session["OutdateDataset"] = ds.Tables[0];
int colcount = ds.Tables[0].Columns.Count - 1;
int rowcount = ds.Tables[0].Rows.Count - 1;
hdn1.Value = rowcount.ToString();
for (int d = 0; d <= rowcount; d++)
{
TextBox txt = new TextBox();
txt.ID = "txtOutdates" + d;
txt.Text = grvOutdatesData.Rows[d].Cells[colcount].Text.Trim();
txt.Width = 40;
txt.Height = 12;
txt.Attributes.Add("runat='server' class", "inputTextBox1");
grvOutdatesData.Rows[d].Cells[colcount].Controls.Add(txt);
}
*/
//Freezing rows and column
for (int c = 1; c <= colcount; c++)
{
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("Width", "45px");
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("writing-mode", "tb-rl");//for vertical align of header
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("filter", "flipv fliph");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("position", "relative");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("TOP", "expression(this.offsetParent.scrollTop)");
}
grvOutdatesData.HeaderRow.Cells[0].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[1].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[2].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[3].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
foreach (GridViewRow grv in grvOutdatesData.Rows)
{
grv.Cells[0].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[0].Attributes.CssStyle.Add("position", "relative");
grv.Cells[0].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[1].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[1].Attributes.CssStyle.Add("position", "relative");
grv.Cells[1].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[2].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[2].Attributes.CssStyle.Add("position", "relative");
grv.Cells[2].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[3].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[3].Attributes.CssStyle.Add("position", "relative");
grv.Cells[3].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
for (int b = 0; b < colcount; b++)
{
if (grv.Cells[b].Text == " ")
{
grv.Cells[b].Attributes.CssStyle.Add("BACKGROUND-COLOR", "#f19c76");
}
}
}
}
Output:
Subscribe to:
Posts (Atom)
