Thursday, June 30, 2011

C# Application Settings

Application Settings are a pretty cool feature and very simple to use with Visual Studio 2010, so first of all simply, what are they?

Application Settings allow you to abstract the details of storing data between program sessions.  Basically what this means is, you can add program settings and save their values so that the next time the program is run the setting will be remembered.  A similar feature is adding and using resources, but we'll be modifying our Application Settings ;)

Step 1.
Create a new Windows Forms Application.


Step 2.
Open the Settings file in the solution explorer pane.

Step 3.
Add a boolean setting, we will be using it in the next step.  At this step I should probably point out that we will want to be working with a "User" scope setting, as it we can modify it's value.  Application scope settings are more appropriate for program strings, like error messages or text blocks and other static items.  If you prefer the long version, please see the MSDN link at the bottom of this post.

Step 4.
Now then, the real magic.  Add a CheckBox control to the form, and view it's properties.  Go ahead and link the Toggle setting to "checked" as shown in the picture.


Step 5.
You may have already noticed if you have built and ran your application that your settings are not yet preserved, but why!?  Truth be told you must tell the settings when to save their state.

Fortunately, this is a very easy thing to do, so stick with me and I'll get to it.
Select the main form window, and add an event handler (by double-clicking)
for the FormClosing event, this is to save our program settings before the
application exits.


Step 6.
Now the fun part, the one that teaches you how to use the settings in your code!
In the event handler function body, add the following code:

Properties.Settings.Default.Save();

You may also notice a member of concern is: Properties.Settings.Default.Toggle

Alright, that's all folks.

Tuesday, June 21, 2011

Synchronous WebBrowser control navigating

For the random people that stumble upon this blog, this article relates to the .NET framework's "WebBrowser" class which basically allows a programmer to use internet explorer within his application.  In fact, you can create 10 instances of IE and programmatically make them perform a task, if you want to.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

This is some high-level goodness provided by programming languages like C# and Visual Basic, aka the butter-knife of programming languages.  You might enjoy this humerous photo:


Often times subscribing to an event just isn't synchronous enough, one common case is navigating to a web page using the WebBrowser control.

To make it behave synchronously, this snippet is of use:


this.webBrowser.Navigate(url);
while (this.webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}

Friday, June 10, 2011

Artificial Neural Networks

I've recently came across a collection of interesting articles on artificial neural networks, so I thought I'd share the wealth!

This article is pretty good, and is geared toward someone with no experience with ANNs at all.  It will provide you with at least some glossed-over theory on the ANN, and maybe more.  It's worth checking out.
http://www.codeproject.com/KB/recipes/brainnet.aspx

This pdf looks pretty good too, http://arxiv.org/ftp/cs/papers/0308/0308031.pdf

I recommend reading and understanding one article in it's entirety before moving on to another.

As always, stay tuned.

Friday, June 3, 2011

C++ Casting

I wrote a little demo to better describe casting to someone on a programming forum, and I thought it was pretty informative so here it is:

[code]
#include <iostream>
#include <limits>
#include <bitset>
#include <cassert>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
assert(sizeof(short) == 2);
assert(sizeof(signed char) == 1);

string bigBitString =
"0111111100000000";
string smallBitString =
"00000000";

bitset<16> big;
bitset<8> small;

for(string::size_type i = 0; i < bigBitString.size(); i++)
{
     big[i] = bigBitString[i] ==
'1' ? 1 : 0;
}


for(string::size_type i = 0; i < smallBitString.size(); i++)
{
     small[i] = smallBitString[i] ==
'1' ? 1 : 0;
}

short sBig = (short)big.to_ulong();
signed char cSmall = (signed char)small.to_ulong();

cout <<
"Sizeof short: " << sizeof(short) << endl;
cout <<
"Sizeof signed char: " << sizeof(signed char) << endl;
cout <<
"Short bits: " << big.to_string() << endl;
cout <<
"Signed char bits: " << small.to_string() << endl;

signed char casted = (signed char) big.to_ulong();

bitset<8> castedBits(casted);

cout <<
"Signed char bits after down-casting: " << castedBits.to_string() << endl;

cin.get();
return 0;
}
[/code]

I also wrote a small C++ string class demo, if you don't know what C++ strings are, you should definitely play around with this:

[code]
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;

int main()
{

string cppString = "Hello World.";
cppString = "No error, no confusing C functions.";
getline(cin,cppString);//No buffer overflow from too much input.

//Use much like an array.
for( std::string::size_type i = 0; i < cppString.size(); i++ )
cppString[i] = 'x';

cppString += " -- Simple string concatenation too.";

//Usable with the C++ STL algorithms.
random_shuffle(cppString.begin(), cppString.end());

cout << "Can still get a C string if you REALLY want to also." 
<< endl << cppString.c_str() << endl;

//Easily convert from string to int/floating point type.
int someInt = 0;
cin >> cppString;
if((stringstream(cppString) >> someInt).fail())
cout << "Invalid number, woops!";
else
cout << someInt << endl << "Just your lucky number." << endl;
}
[/code]