Tuesday, July 12, 2011

Moving data between forms with properties

Back when I started writing programs in C# I had no idea how to pass information between different forms. So like so many others I started by searching the internet. I found a ton of sites that showed how to do it. A lot of example code. But little explanation about what is actually going on. I think it’s more effective to know what’s happening rather than just “to do it use this code”. There are a half dozen ways you can pass data around in winforms applications in c# but the way I prefer to pass information back and forth in C# is with properties. To create a property in C# you create a field variable and a property.

Think of it like you think of a textbox. If you have textbox1 on your form you can access the .Text property of the textbox by using textbox1.text=”this”; or string variable = textbox1.text; What you’re doing is creating a form property that you can access from other forms.
Set up a windows form application in Visual Studio. Add a second form to it. In the second form create a field variable called _name and define the Name property. This is the code that does it:
private string _name;
public MyForm()
{
     InitializeComponent();
}
public string Name
{
     get { return _name; }
     set { _name = value; }
}
The _name variable is private which means it is visible only to the second form. When you want to change the variable in the second form you modify _name. The Name property is public and is visible everywhere in the program. When you want to change the variable anywhere except in the second form, modify Name.

Basically what happens is whenever Name is changed Set {_name = value;} takes whatever is in Name and puts it in _name. And when Name is queried, get {return _name} takes what is in _name and puts it in Name. So if we had Form1 and Form2 and our property is in Form2. Then in Form1, to send information to Form2 we would do the following:
Form2 child = new Form2();
child.Name = "Joe";
child.ShowDialog();
This creates an instance of Form2 called child. Then it sets the Name property to “Joe”. Then it shows the window.
If you wanted to change the name and send the new name to the form that called it, you would change the field variable in Form2 and then get the Name property in Form1.
So in Form2 you would have:
_name = “New Name”;
And in Form1 (after the ShowDialog()) you would read the property
string newName = child.Name;

See. Clear as mud.

Monday, July 11, 2011

Greetings Earthling.

tl;dr version: I'm a geek. I'm going to blog about programming. Some personal history/get to know you stuff below.

I'll be using this to share tidbits of useful coding information for various platforms. I've been working with and learning computers and technology since 1984 and thought I would share some of the things that I've learned over the years/found useful. But before I get into all of that techie stuff, a little about me. I'm the typical geek. D&D, video games, gadgets and technology. I'm also the typical country boy. I know how to pluck a chicken, roof a house, change an engine, plumb a bathroom, and skin a rabbit (among other things). 

I've made my living through the years doing Systems Administration and support (usually the only person in the IT department) and love my job.  The first computer I owned was a Tandy Color Computer 2 back when I was in high school. Like all the other kids I thought the games were cool (Oregon Trail, Robot Odyssey, Zork, etc) but it really wasn't enough for me.

I wanted to know how thy did that. How do you get a computer to do that? So I dove in head first. A year later I was submitting my first program for publication. A text adventure game written in BASIC called Save the Earth. It was published through T&D Software. A monthly publication where they mailed out disks and cassettes to their subscribers with various programs on them (games, utilities, productivity, etc). Shortly after the BASIC program I decided that it still wasn't enough. I needed to figure out how things worked on a more basic level so I dove into ASM for the 6809 processor. Once I wrapped my head around that I submitted my second program for publication. A utility in ASM that let you change the background color and font color on the Color Computer that was persistent until a system restart. Simple stuff but it gave me a good foundation to understand what was really going on inside computers.

From there I went on to helping out the school district with their student attendance/records software. Worked part time helping the one computer business doing things like building systems and running network cabling. Started college and worked in the computer lab under their college work study program. I also ventured into the 'dark side' of computers for a bit. BBS's, hacking, cracking, piracy, all of that sordid stuff. I think I learned more in that couple years than I did in any other time period. Then I was done with college and everything changed. I had to get a real job. I ended up going to work for an insuarnce agency as their Jr. Network Administrator. Which pretty much meant I did all of the physical work like building and moving systems, running cable, answering help desk calls, etc.

This went on a couple of years, the admin left for greener pastures, I got a girlfriend, the company was sold, changed names, I lost a girlfriend, new department head, new girlfriend, got married, had a severe personality conflict with the department head, and it was over. Moved to Dallas, got a new job as Network Administrator (same job, new title, better pay, more people, longer hours). Spent 4 years without sleep keeping everything running, went on vacation and came back to the locks on the doors changed. And that was over. Went to work a week later where I am now. I love this job. I love the people I work for and with. The job is still the same job as before but with much much more. Everything from running network cable to writing industry specific applications. I've been here for 11 years and plan on being here for the duration.

There's so much more I could post but I'd bore you.