My Adventures In App Development (Part 1: The Beginning)

I hate saying that I’m relatively new to app development, but I am. I had originally moved into the Apple ecosystem in 2009 with somewhat of a desire to develop applications. At the time, however, I was spending most of my development time on websites. App development seemed like a much more daunting task.

Over the years, I would occasionally open up Xcode with the intent of creating some kind of basic iPhone app. Each time, though, it seemed like such a different beast than what I was used to. (The MVC model wasn’t a stranger to me, but I didn’t have a lot of experience with it at the time.) I’d add in a few buttons and views, type up some code, but I couldn’t get myself to really dive into it.

One thing I’ve come to really love using since I first got my first iPhone in 2009 is Evernote. I use it to store notes about almost anything, from guitar tab to food recipes. For a while now, I’ve also stored different ideas that come into my head. As I put more and more ideas down in writing, I was able to get a better idea of what I wanted to do.

Two breakthroughs helped me to really push forward in app development, however. The first was making use of video tutorials online, especially on iTunes U, that helped me to really learn and understand Objective-C and iOS app development. Finding an iOS development course is a big plus, as it is just like being in a class where you are taught how to develop for the iPhone and iPad. It definitely beats just reading documentation and following along in a book.

Second was the latest iOS release, iOS 7. As soon as I saw the new look and feel of iOS unveiled at WWDC, as well as reading about the new APIs built into the release, I became excited for development again. In fact, I hadn’t felt this excited about it before.

It was with iOS 7 that my interest was aflame again. Since June, I’ve been pushing forward and working on several ideas. I’ve not been so excited about iPhone app development as I am now. I have a few projects on the burners and, as they get closer to release, I’ll start sharing some information about them, either here or via my business site (which will be updated soon). I’ll also document my travels as I learn more about Objective-C, Xcode, and developing for the iPhone, iPad, and Mac.

[C#] Using Nini .NET Configuration Library

When developing a desktop application, there will be times when you want to store settings for your program. A database is one option, but on Windows, you might just wish to have your settings stored in an INI file. One way to work with an INI file in C# is with the Nini Library. This makes it quite easy to read from and write to an INI file.

Let’s get started.

After installing the library, we’ll need to set our namespace.

using Nini.Config;

What will our INI file look like? Something like this:

;conf.ini
[Options]
Zipped = 0
Filename = test.txt

For my application, I decided to make a class devoted to the configuration file. So, let’s define that and a few other variables.

public class OurConfig
{
string NL = Environment.NewLine; // New line character
private string configFile = "conf.ini"; // Our INI file

IConfigSource config; // Instance of our config

}

Now that we have our variables declared, let’s create a couple of useful methods.

public void set_is_zip(int zipped)
{
config.Configs["Options"].Set("Zipped", zipped);
}

public void set_filename(string fname)
{
config.Configs["Options"].Set("Filename", fname);
}

These two methods will update the INI file with new settings, should we change them in our program. Of course, if we make these changes, they need to be saved. Thankfully, we can declare something in our constructor (which we will write a little later) that will auto-save our changes as we make them.

config.AutoSave = true;

Now, let’s create a pair of methods to return the data. This will be useful in our program when we need to use these settings.

public int return_is_zip()
{
return config.Configs["Options"].Get("Zipped");
}

public string return_filename()
{
return config.Configs["Options"].Get("Filename");
}

With these methods, we now have a basic class for handling a configuration file. All that is left is our constructor.

But before we get to the constructor, there is something else I created. What if our INI file doesn’t exist? I decided that I would make a function to create a default INI file, should the old one not exist anymore. This is also useful if we want to distribute our program without an INI file.

private void fill_new_ini()
{
// Put default values into the INI file
// Essentially, we're writing a blank file, so this is fairly simple
string toWrite = ";conf.ini" + NL
+ "[Options]" + NL
+ "Zipped = 0" + NL
+ "Filename = test.txt" + NL;

System.IO.File.WriteAllText(@"conf.ini", toWrite);
}

We can do a check when we initialize our class that will check to see whether or not this file exists. If not, we’ll create it so we can work with it.

That makes this our constructor:

public OurConfig()
{
// Initialize the INI file if it doesn't exist
try
{
configFile = new IniConfigSource("conf.ini");
}
catch (Exception ex)
{
// Write default values into it
fill_new_ini();
configFile = new IniConfigSource("conf.ini");
}

configFile.AutoSave = true; // Auto save config file as we make changes

}

Our whole class thus looks like this:

using Nini.Config;

public class OurConfig
{
string NL = Environment.NewLine; // New line character
private string configFile = "conf.ini"; // Our INI file

IConfigSource config; // Instance of our config

public OurConfig()
{
// Initialize the INI file if it doesn't exist
try
{
configFile = new IniConfigSource("conf.ini");
}
catch (Exception ex)
{
// Write default values into it
fill_new_ini();
configFile = new IniConfigSource("conf.ini");
}
configFile.AutoSave = true; // Auto save config file as we make changes
}

private void fill_new_ini()
{
// Put default values into the INI file
// Essentially, we're writing a blank file, so this is fairly simple
string toWrite = ";conf.ini" + NL
+ "[Options]" + NL
+ "Zipped = 0" + CL
+ "Filename = test.txt" + CL;
System.IO.File.WriteAllText(@"conf.ini", toWrite);

}

public void set_is_zip(int zipped)
{
config.Configs["Options"].Set("Zipped", zipped);
}

public void set_filename(string fname)
{
config.Configs["Options"].Set("Filename", fname);
}

public int return_is_zip()
{
return config.Configs["Options"].Get("Zipped");
}

public string return_filename()
{
return config.Configs["Options"].Get("Filename");
}

} // End OurConfig

That’s how simple it can be to work with your own INI files in C#.

Did you find this useful? Let me know in the comments!

Working with DataTable in C#

One thing I’ve had to do lately is learn some C#. I will admit that it isn’t the language I would prefer to work with during the week, but it does help me become more versatile when it comes to application development.

Working with C#, I had to take data and enter it into a data grid. Being that I still consider myself a C# newbie, I figured this was something I might as well learn to use.

In the end, creating a data table in C# isn’t that difficult. In this example, I’ll be taking data from a Microsoft Access database and entering it into a DataTable object.

First, let’s define our DataTable and its columns.

[crayon lang=”C#”]

DataTable table = new DataTable();

table.Columns.Add(“Name”, typeof(string));
table.Columns.Add(“Age”, typeof(int));
table.Columns.Add(“Weight”, typeof(int));

[/crayon]

Now, while it isn’t necessary, I like having temporary variables to hold data from a database before entering it into our table. For what we have, that would be:

[crayon lang=”C#”]

string theName;
int theAge;
int theWeight;

[/crayon]

All that is left to get is the data itself. Assuming we’re getting it from some kind of database, we can get the information in a while loop and add all of the data into the table.

[crayon lang=”C#”]

OleDbCommand cmd = new OleDbCommand(“Select FullName, PersonAge, PersonWeight FROM PersonalInfo”, con); // Our Query
OleDbDataReader reader = cmd.ExecuteReader(); // Execute Query
while (reader.Read()) // If we can read rows…
{
// Show records in text fields
theName = reader.GetString(0);
theAge = reader.GetInt32(1);
theWeight = reader.GetInt32(2);

table.Rows.Add(theName, theAge, theWeight);
}

[/crayon]

Now we have our data in the DataTable. All that is left is to output it. For this, I have a DataGridView object in my form called myDataGrid.

[crayon lang=”C#”]

myDataGrid.DataSource = table; // Put DataTable data into our DataGrid
myDataGrid.AutoResizeColumns(); // Automatically resize columns to show all data

[/crayon]

And that’s all there is to using a DataTable in C#.

Have you worked with DataTables? Submit your experiences and comments below!