Monday, July 28, 2008

Error: Cannot implicitly convert type 'Employees' to 'System.Collections.ArrayList'. An explicit conversion exists (are you missing a cast?)

Error: Cannot implicitly convert type 'Employees' to 'System.Collections.ArrayList'. An explicit conversion exists (are you missing a cast?)

What I intended to do is to add the Employee object to the WorkerList property through the set accessor. However, I have made a mistake here. The mistake was, I used a reference of Employee object as the argument to pass it to the set accessor (a special kind of method), which takes an implicit parameter called "value", and the type of this parameter (the "value") always matches the type of the field (or the property). That's why it's causing this error.

Error Code:

public class WorkHours
{
private ArrayList workerList = new ArrayList();

public ArrayList WorkerList
{
set
{
workerList.Add( value ); // what "value" really wants is a reference to a ArrayList object.
// or
workerList = value;
}
}
}

WorkHours workHours = new WorkHours();
Employee employee1 = new Employee();

workHours.WorkerList = employee1;


Correct Code:

public class WorkHours
{
private int minWorkers; // within this particular hour.
public int MinWorkers
{
get { return minWorkers; }
set { minWorkers = value; }
}

private ArrayList workerList = new ArrayList(); // within this particular hour.

public void setWorkerList(Employees employees)
{
workerList.Add(employees);
}

public ArrayList getWorkerList()
{
return this.workerList;
}
}

WorkHours workHours = new WorkHours();
workHours.setWorkerList( new Employees() );

Sunday, July 27, 2008

Reading the output of a process


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace test
{
public partial class Form1 : Form
{
Process process = new Process();


public Form1()
{
InitializeComponent();

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "C:/CSharp/SQLite/sqlite3.exe";
process.StartInfo.Arguments = "a.db3";
}

private void button1_Click(object sender, EventArgs e)
{


process.Start();
//process.BeginOutputReadLine();

process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
//process.BeginErrorReadLine();
process.BeginOutputReadLine();
}

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
//log.Error(e.Data);
}

void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
//log.Info(e.Data);
this.AddText(e.Data);

}

delegate void AddTextCallback(string text);

private void AddText(string text)
{
if (this.label1.InvokeRequired)
{
AddTextCallback d = new AddTextCallback(AddText);
this.Invoke(d, new object[] { text });
}
else
{
this.label1.Text += text + Environment.NewLine;

}
}


}
}

Thursday, July 24, 2008

在 Visual Studio 中,想要 改變 Development Settings (如 VB, C#, C++)

1. 在 Visual Studio 中,想要 改變 Development Settings (如 VB, C#, C++),到 Tools > Import and Export setting > Reset all setting

2. Windows > Reset Window Layout

Tuesday, July 22, 2008

SQLite 速記2


Dim sqlBugs As New SQLite.SQLiteConnection()
Dim dsBugs As New DataSet()
Dim daBugs As New SQLiteDataAdapter()
Dim cmdBugs As New SQLiteCommandBuilder()
Dim theDataGridView As New DataGridView()

sqlBugs = New SQLite.SQLiteConnection("Data Source=C:\vb08sbs\DannySQL\DannySQL\Data\aaa.db3;")
dsBugs = New DataSet("dataset1")

daBugs = New SQLiteDataAdapter("SELECT * FROM [foo]", sqlBugs)
cmdBugs = New SQLiteCommandBuilder

If sqlBugs.State <> ConnectionState.Open Then sqlBugs.Open()

cmdBugs.DataAdapter = daBugs

'daBugs.Fill(dsBugs)
daBugs.Fill(dsBugs)

'theDataGridView.DataSource = dsBugs.Tables(0).DefaultView

SQLite 速記

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataSet1.foo' table. You can move, or remove it, as needed.
'Me.FooTableAdapter.Fill(Me.DataSet1.foo)
Dim strCommand As String
Dim strConnect As String
Dim DataGrid1 As New DataGridView

strConnect = "Data Source=C:\vb08sbs\DannySQL\DannySQL\Data\aaa.db3"

Dim objConnection As SQLiteConnection = New SQLiteConnection(strConnect)
Dim objDS As DataSet = New DataSet
Dim objDA As SQLiteDataAdapter

Try

strCommand = "SELECT * FROM [foo] LIMIT 0,1"

objDA = New SQLiteDataAdapter(strCommand, objConnection)

'objDA.Fill(objDS)
objDA.Fill(Me.DataSet1.foo)

If objDS.Tables(0).Rows.Count > 0 Then

DataGrid1.DataSource = objDS.Tables(0).DefaultView

End If

Catch ex As Exception

'錯誤控管

Finally

'清除資源

If Not objDS Is Nothing Then objDS.Dispose()

End Try

End Sub

Sunday, July 20, 2008

connect VB .NET to SQLite

1. create new project, save all, and exit VS

2. Copy sqlite/bin folder to YourProject/bin

3. Run bin/Designer/install.exe , check the box, close

3. Open up the project again.

4. To add a custom provider to your application's config file (app.config).

click project > add new item > general tab > application configuration file

Add the following code to your app.config file:


<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>


5. Add new data source

click Data > Add new data Source > select Database > next > click New Connection > Select SQLite Database File > uncheck "Always use this selection" > click Continue

6. click "New" > select the path to store the database file > next

Tuesday, July 8, 2008

test


<?php
echo "Hello World";
?>