static private void findMissing(int[] intArray, int n)
{
bool[] bitArray = new bool[n];
if (intArray == null)
throw new Exception("findMissing - intArray is null");
for (int i = 0; i < n; i++)
bitArray[i] = false;
foreach (int x in intArray)
{
if (x < 0 || x >= n)
throw new Exception("findMissing - x is out of range - x=" + x);
bitArray[x] = true;
}
for (int i = 0; i < n; i++)
{
//Console.WriteLine("i=" + i + " bit=" + bitArray[i].ToString());
if (!bitArray[i])
Console.WriteLine("Missing element at i=" + i);
}
}
static private void testFindMissing()
{
int[] intArray = new int[]{ 1, 2, 4, 7, 3, 5, 6, 0, 9, 10 };
findMissing(intArray, 11);
Console.ReadLine();
}
Tuesday, March 29, 2011
amazon question find min diff in a list of int
static private void minDiff(int[] intArray)
{
if (intArray == null)
throw new Exception("minDiff - intArray is null");
var query = intArray.OrderBy(x1 => x1);
List intList = query.ToList();
if (intList.Count <= 1)
throw new Exception("minDiff - must have at least 2 elements in list");
int minDiff = int.MaxValue;
int x = -1;
int y = -1;
for (int i = 1; i < intList.Count - 1; i++)
{
if (intList[i] - intList[i - 1] < minDiff)
{
x = intList[i];
y = intList[i - 1];
minDiff = x - y;
}
}
Console.WriteLine("x=" + x + " y=" + y + " minDiff=" + minDiff);
Console.ReadLine();
}
static private void testMinDiff()
{
int[] intArray = new int[] { 3, 9, 6, 17, 33, 1, 16 };
minDiff(intArray);
}
{
if (intArray == null)
throw new Exception("minDiff - intArray is null");
var query = intArray.OrderBy(x1 => x1);
List
if (intList.Count <= 1)
throw new Exception("minDiff - must have at least 2 elements in list");
int minDiff = int.MaxValue;
int x = -1;
int y = -1;
for (int i = 1; i < intList.Count - 1; i++)
{
if (intList[i] - intList[i - 1] < minDiff)
{
x = intList[i];
y = intList[i - 1];
minDiff = x - y;
}
}
Console.WriteLine("x=" + x + " y=" + y + " minDiff=" + minDiff);
Console.ReadLine();
}
static private void testMinDiff()
{
int[] intArray = new int[] { 3, 9, 6, 17, 33, 1, 16 };
minDiff(intArray);
}
Wednesday, March 23, 2011
Nesting Functions vs Intermediate Variables
Unless you really love looking at the call stack, create intermediate variables and un-nest your function. The compiler will compile away these variables anyways, so no loss in efficiency. They will be handy for debugging and make the code clearer.
Tuesday, March 22, 2011
Delete a Project from Team Foundation Server
Answer: go to the Visual Studio 2010 command prompt and use “TfsDeleteProject.exe“.
Example:
TfsDeleteProject /collection:http://larry-pc:8080/tfs “Fret Project“
You can get the url, if you've forgotten it, by going to
Team -> Connect to Team Foundation Servers -> Servers ..
Example:
TfsDeleteProject /collection:http://larry-pc:8080/tfs “Fret Project“
You can get the url, if you've forgotten it, by going to
Team -> Connect to Team Foundation Servers -> Servers ..
Forcing DataGridView to Refresh when changed
This works - save DataSource, save scroll position, restore datasource, restore scroll position, e.g.
private void forceRefresh()
{
int x = dgvMusicalScaleList.FirstDisplayedScrollingRowIndex;
dgvMusicalScaleList.DataSource = null;
dgvMusicalScaleList.DataSource = musicalScaleList;
dgvMusicalScaleList.FirstDisplayedScrollingRowIndex = x;
}
private void forceRefresh()
{
int x = dgvMusicalScaleList.FirstDisplayedScrollingRowIndex;
dgvMusicalScaleList.DataSource = null;
dgvMusicalScaleList.DataSource = musicalScaleList;
dgvMusicalScaleList.FirstDisplayedScrollingRowIndex = x;
}
Monday, March 21, 2011
You forgot to make it public, dummy!
Microsoft has a convoluted way of saying this;
Error 1 Inconsistent accessibility: property type 'System.Collections.Generic.List' is less accessible than property 'Frets.MusicalScale.notes' J:\Users\Larry\Documents\Visual Studio 2008\Projects\Frets\Frets\Classes\MusicalScale.cs 44 41 Frets
Error 1 Inconsistent accessibility: property type 'System.Collections.Generic.List
Saturday, March 19, 2011
2-valued boolean variable
What do you name a variable that is a boolean but can have 2 meaningful values out of N?
Example: x can have values "red" or "blue".
Calling it bRed, or isRed, is meaningful if he value is true , but doesn't convey any info about the "false" case.
YOu could implement it as an int, or if you are picky, as an enum. But this means more lines of code to declare the enum, etc.
I thought of "redOrBlue", but logically speaking, the value is always true :)
What do you think of "redNotBlue"? Any better ideas?
Example: x can have values "red" or "blue".
Calling it bRed, or isRed, is meaningful if he value is true , but doesn't convey any info about the "false" case.
YOu could implement it as an int, or if you are picky, as an enum. But this means more lines of code to declare the enum, etc.
I thought of "redOrBlue", but logically speaking, the value is always true :)
What do you think of "redNotBlue"? Any better ideas?
DataGridView not Updating when DataSource modified
I had this problem. I managed to solve it as follows:
If you don't save and restore the FirstDisplayedScrollingRowIndex, then the user loses his/her scroll position.
other keywords - c#, msdn, .NET, scrolling
int x = dgvMetronomeSpeeds.FirstDisplayedScrollingRowIndex;
dgvMetronomeSpeeds.DataSource = null;
m_metronomeSpeeds.Add(new Integer(0));
dgvMetronomeSpeeds.DataSource = m_metronomeSpeeds;
dgvMetronomeSpeeds.FirstDisplayedScrollingRowIndex = x;
If you don't save and restore the FirstDisplayedScrollingRowIndex, then the user loses his/her scroll position.
other keywords - c#, msdn, .NET, scrolling
Friday, March 18, 2011
Never take notice of an error that you don't know how to handle
This is in general a good principle, but in specific cases it is a good idea to intercept it. The special case is when the error occurs at a very low level (i.e. a null pointer exception) and thus there is not enough context information to make sense of the error.
When debugging, it isn't an issue because you have access to the call stack and can figure out the context from there. If, however, the error is generated for a user, and the user provides the information to support, then there won't be enough information to figure out the error, at least not easily.
In this case (or if you are a developer and are anticipating this case happening in the future, where the user might be QA) it is often helpful to catch the error, add some context information, and rethrow the exception. Or, you could check for the error that might cause the exception, and throw an exceptioin yourself instead of calling the routine that might generate the exception.
For example, usinng a variable
Class1 c = complicatedCreationMethod();
....
c.method1();
Now, method1 will fail if c is null. If there is any possibility that complicatedCreationMethod() might return null, you may want to check for this and throw an exception rather than simply letting the null pointer exception percolate to the top of the error handlers.
When debugging, it isn't an issue because you have access to the call stack and can figure out the context from there. If, however, the error is generated for a user, and the user provides the information to support, then there won't be enough information to figure out the error, at least not easily.
In this case (or if you are a developer and are anticipating this case happening in the future, where the user might be QA) it is often helpful to catch the error, add some context information, and rethrow the exception. Or, you could check for the error that might cause the exception, and throw an exceptioin yourself instead of calling the routine that might generate the exception.
For example, usinng a variable
Class1 c = complicatedCreationMethod();
....
c.method1();
Now, method1 will fail if c is null. If there is any possibility that complicatedCreationMethod() might return null, you may want to check for this and throw an exception rather than simply letting the null pointer exception percolate to the top of the error handlers.
Wednesday, March 16, 2011
Tracking down bugs
This is basic, but anyways this is how I do it.
Put a breakpoint before the error occurs (your best guess). Step OVER code (not going into any code) until you run into the exception. Then, set the breakpoint at that procedure call, remove the previous breakpoint, step into that part, and repeat above until you find the bug.
Eventually you end up with a single line of code where the error occurs, that has no function calls. Simplify the call by removing any nested procedure calls and serializing them. E.g. instead of f1(f2(f3)) do x = f3; y = f2(x); z = f1(y). This makes it easier to step and watch the variables.
Put a breakpoint before the error occurs (your best guess). Step OVER code (not going into any code) until you run into the exception. Then, set the breakpoint at that procedure call, remove the previous breakpoint, step into that part, and repeat above until you find the bug.
Eventually you end up with a single line of code where the error occurs, that has no function calls. Simplify the call by removing any nested procedure calls and serializing them. E.g. instead of f1(f2(f3)) do x = f3; y = f2(x); z = f1(y). This makes it easier to step and watch the variables.
Monday, March 14, 2011
Data Design or Procedure First?
It's often a difficult decision whether to design the data representation or the algorithm first.
In a big team, designing the data representation first means that all team members can work on their individual functional components that operate on the same data representation.
However, the data representation may not be the right one for the algorithms, or not as simple or optimal as it could be. To determine the best data representation, you need to design the algorithm first.
When programming individually, I find it is best to figure out the simplest data representation (even if hard-coded, no IO) required to develop the algorithm, get that working, then modify the data representation to fit additional requirements (i.e. persistence, modifiability, etc). There's no point designing a lot of functionality (i.e. communication, database, etc.) if you don't have a good representation.
In a team, you can do this by prototyping critical algorithms first, then designing the data representation, then passing out functional component work.
In a big team, designing the data representation first means that all team members can work on their individual functional components that operate on the same data representation.
However, the data representation may not be the right one for the algorithms, or not as simple or optimal as it could be. To determine the best data representation, you need to design the algorithm first.
When programming individually, I find it is best to figure out the simplest data representation (even if hard-coded, no IO) required to develop the algorithm, get that working, then modify the data representation to fit additional requirements (i.e. persistence, modifiability, etc). There's no point designing a lot of functionality (i.e. communication, database, etc.) if you don't have a good representation.
In a team, you can do this by prototyping critical algorithms first, then designing the data representation, then passing out functional component work.
Xml Serialization and Deserialization of Complex Objects/Lists
http://msdn.microsoft.com/en-us/library/ms950721.aspx
what isn't really made clear here is that you have to pass into the XMLSerializer constructor all the types that will come in the nested types of the objects. you can do this e.g.
public class Class1
{
public static Type[] extraTypes = new Type[] { typeof(Class2), typeof(Class3) }
private list1 List = new List();
public List
{
get ..
set ..
}
... same for Class3 ..
}
Then you need to create the xml serializer as XmlSerializer(typeof(Class1), Class1.extraTypes)
what isn't really made clear here is that you have to pass into the XMLSerializer constructor all the types that will come in the nested types of the objects. you can do this e.g.
public class Class1
{
public static Type[] extraTypes = new Type[] { typeof(Class2), typeof(Class3) }
private list1 List
public List
{
get ..
set ..
}
... same for Class3 ..
}
Then you need to create the xml serializer as XmlSerializer(typeof(Class1), Class1.extraTypes)
Friday, March 11, 2011
Changing SoundPlayer volume
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/42b46e40-4d4a-48f8-8681-9b0167cfe781
Subscribe to:
Posts (Atom)