Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, 18 September 2008

VS2005 XML Comments not shown

XML Method comments are not being displayed by intellisense for methods of classes other than those in the same project.

Any referenced DLL must have the XML file which contains the comments in the same directory as the DLL itself.

Tuesday, 13 May 2008

Rhino Mocks - Partial mock not calling base method

Had a problem where I was creating a partial mock then calling a method of the partial mock which was returning 0 (zero).

The base method was not being called even though no expectations had been setup.

The reason for this was that the ReplayAll (and VerifyAll) methods of Rhino Mocks was not being called.

Original code:

            _interestBiz = _mocks.PartialMock<InterestBiz>();
int baseDay = _interestBiz.GetBaseDaysFromAccount(account);
Assert.AreEqual(365, baseDay, "Incorrect base days");


Fixed code:



            _interestBiz = _mocks.PartialMock<InterestBiz>();
using (_mocks.Record())
{

}

using (_mocks.Playback())
{
int baseDay = _interestBiz.GetBaseDaysFromAccount(account);
Assert.AreEqual(365, baseDay, "Incorrect base days");



 



So even though no expectations were set Rhino Mocks needs to no this.

Thursday, 1 May 2008

Good Rhino.Mocks introduction / tutorial

Good Rhino.Mocks introduction / tutorial here

C# Threading : Workers processing list

Using ThreadSafeQueue class as list of items to process.

Class used as a worker, takes list as parameter, creates a thread to run in.

public class MyWorker
{
private Thread _myThread;
public Thread WorkerThread
{
get { return _myThread; }
set { _myThread = value; }
}

ThreadSafeQueue<int> _listOfNumbers;
public string NumbersProcess = string.Empty;
int total=0;

public MyWorker(ThreadSafeQueue<int> listOfNumbers)
{
_listOfNumbers = listOfNumbers;
_myThread = new Thread(new ThreadStart(this.DoProcess));
}

public void DoProcess()
{
int _localInt=0;
do
{
if (_listOfNumbers.TryDequeue(out _localInt))
{
total = total + _localInt;
NumbersProcess = NumbersProcess + _localInt.ToString() + ", ";
}
} while ((!_listOfNumbers.NoMoreItems) ||
(_listOfNumbers.NoMoreItems) && (_listOfNumbers.Count > 0));
}
}



 



Calling process creates 5 workers then populates list, then waits for workers to finish processing:



//Queue to hold numbers to process
ThreadSafeQueue<int> listOfInts = new ThreadSafeQueue<int>();

//5 worker objects (threads)
MyWorker[] workers = new MyWorker[5];
for (int i = 0; i < workers.Length; i++)
{
workers[i] = new MyWorker(listOfInts);
workers[i].WorkerThread.Start();
}

//Populate list to process
for (int i = 0; i < 1000; i++)
{
listOfInts.Enqueue(i);
}

//Flag that no more items will be added to queue
listOfInts.NoMoreItems = true;

//Wait for workers to finish then display details
foreach (MyWorker worker in workers)
{
worker.WorkerThread.Join(); //Wait for worker to finish processing.
tbxDetail.Text=tbxDetail.Text+string.Format("Numbers processsed {0} \n",worker.NumbersProcess);
}
tbxDetail.Text = tbxDetail.Text + string.Format("\n\n");

Tuesday, 29 April 2008

C# Constructors

See full discussion here

Base

public myClass() : base()

Overload

class myClass
{
public myClass()
{
//No parameters
}

public myClass(string param1) : this()
{
//No parameters will be called before me!
}
}

Thursday, 3 April 2008

Bind Dictionary to WinForm ComboBox

To bind a dictionary to a combo box:

 

            cmbTable.DisplayMember = "Key";
cmbTable.ValueMember = "Value";
cmbTable.DataSource = new BindingSource(myDictionary, null);

Friday, 11 May 2007

C# Date encoding / parse

DateTime.Parse requires a Culture to be set to use a specific format.
If the format is known then a better alternative is to use new DateTime(year,month,day).

C# Windows dock overlap

Where panels etc are 'docked' in a windows form they can sometimes overlap (eg Left dock overlaps the Fill dock panel). To overcome this the overlapping panel must be 'Sent to Back'

Tuesday, 6 March 2007

Error on DataBind() of DropDownList:, invalid SelectedValue

Getting error:

has a SelectedValue which is invalid because it does not exist in the list of item


The SelectedValue before the databind is set to "" (i.e. blank string)
The error occurs because the data being bound does not include a value of "" (blank string).

To fix it:
  1. Set AppendDataBoundItems=true for control
  2. Clear the list prior to databind; .Items.Clear();
  3. Add a blank item: .Items.Add(new ListItem("", ""));
  4. Call .DataBind();

Tuesday, 27 February 2007

Configuration - unknown class

Tripped me up a couple of times, the System.Configuration must be manually added as a reference to a project otherwise the Configuration class is unknown.
For some reason I always assume this reference is added by default then spend a while wondering why VS claims Configuration class is not part of System.Configuration!

Thursday, 22 February 2007

C# Enum, enumerated types

C# Enumerated type info

Declaration:
enum MyType
{
One=1,
Two=2
};



Assignment:

int myVar = (int) MyType.One;
MyType myVar2 = MyType.Two;


Getting name of value:
string enumName = MyType.One.ToString();


Getting all values as string:
string[] enumNames = Enum.GetNames(typeof(MyType));


Getting an Enum value from a string:
MyType myVar = (MyType)Enum.Parse(typeof(MyType),"One",true)

Wednesday, 21 February 2007

C# Lazy or full boolean evaluation?

C# allows for both full and lazy evaluation. For full evaluation use the single operators for And/Or (|/&), for lazy evaluation use the double operators for And/Or (&amp;amp;amp;&/||).
e.g.
Full Evaluation
if ((sName==null) | (sName="~empty~"))
...do something

will cause an exception if sName is null as the second comparions (="~empty~") will be performed.

Lazy Evaluation
if ((sName==null) || (sName="~empty~"))
...do something

will not cause an exception if sName is null because as the rest of the expression is an OR the result will be true irrespective of the rest of the expression, and so the rest of the expression is ignored.

Thursday, 15 February 2007

C# Generics typecasting

Error being received:

cannot implicity convert type ilist to list

The error occurs when calling a function that returns an IList.
e.g.

IList myFunction();
....
List myVar = new List;
myVar=myFunction();


the error occurs on the assignment of myVar=myFunction.
The local var myVar should be declared as IList.
In the above instance the new List is not required because myFunction creates the instance.
Correct code:

IList myFunction();
....
IList myVar;
myVar=myFunction();