PDA

Click to See Complete Forum and Search --> : [2.0] Slight issues here, classes - structures - arraylists - databases oh my...


ThisIsMyUserName
Apr 15th, 2006, 03:14 AM
Hello,

I recently started programming in C# a few days ago. Having a bit of a problem here.

At first I tried this...

I have a seperate class from the form -- and this is the logic:

1) A public struct with my program's settings is setup (window locations, etc.)
2) A public sub calls a private function which gets the data from a database. That private function stores the returned column in an array list.
3) the public sub gets the values from the array list and fills the variables of the structure.

At this point I thought I could just call the structure in my main form and read the value, but it's setting it up as null because I guess when I do "MyStruct ms = new MyStruct();" it resets it?

I'm pretty confused to be honest. I have absolutely no idea how I can set this up at the moment.

Edit:

Guess I need to ditch the structure, use private variables in the class, and pass them with methods? Going to try that now, btw if this ends up working -- would that setup be considered the normal way to get things done?

I want to start writing "good" code, rather than jerry rigged "it works, but it's retarded" code. :)

Kasracer
Apr 15th, 2006, 03:32 AM
When you create a struct, you can't add data to it, you just define what objects it will hold. When you create a new one, it's a new one. Everything has the value of null until you do something with them. If you want to access them, now you would populate your new structure with items from the arraylist or database (whichever way you were going)

jmcilhinney
Apr 15th, 2006, 03:59 AM
An instance of a structure is just an object like any other. You can create an instance and then pass it around like you do any other object. All this talk of public and private methods is irrelevant. Create an instance, set its properties and then pass that instance around from and to whatever properties and methods you like. If you use the "new" keyword then you're creating a new instance that has no relationship to your old instance. Note that when you pass an instance of a structure you are creating a copy, while with a class you are passing a reference to the same object. This fact doesn't mean you can't do what you want though.

ThisIsMyUserName
Apr 15th, 2006, 06:12 AM
"When you create a struct, you can't add data to it, you just define what objects it will hold. When you create a new one, it's a new one. Everything has the value of null until you do something with them. If you want to access them, now you would populate your new structure with items from the arraylist or database (whichever way you were going)"

Yeah I know. I had a pre-set amount of variables in the structure. I just wanted to set them with a LoadProgramSettings() routine in the class, and then read the values from the struct on the main form, in addition to be able to change them (when I get around to saving the new settings), but of course not add to the struct itself.

Hmm I think I need to do more research...I'm not really sure how to pass around objects as an instance yet.

jmcilhinney
Apr 15th, 2006, 07:09 AM
If you're using .NET 2.0 you can use application settings. They aren't as easy to use in C# as they are in VB but they are specifically designed to let you set, retrieve and update application settings and data.

If you want to keep going the way you are now, I'd suggest a Shared method to load the data that returns an instance, then an instance method to save:Public Structure MyStructure

'Private Variables here.

'Public properties here.

Public Shared Function Load() As MyStructure
Dim obj As MyStructure

'Get values from file and set properties of obj here.

Return obj
End Function

Public Sub Save()
'Save properties of Me to file here.
End Sub

End StructureYou would then use it something like this:Dim settings As MyStructure = MyStructure.Load()

'Edit properties of settings as required here.

settings.Save()

jmcilhinney
Apr 15th, 2006, 07:18 AM
Oops. Forgot I was in C# territory. Shared means static. Here's the code translated.public struct MyStructure
{

// Private variables here.

// Public properties here.

public static MyStructure Load()
{
MyStructure obj;

// Get values from file and set properties of obj here.

return obj;
}

public void Save()
{
// Save properties of 'this' to file here.
}
}MyStructure settings = MyStructure.Load();

// Edit properties of settings as required here.

settings.Save();

ThisIsMyUserName
Apr 15th, 2006, 12:47 PM
Pretty neat concept. Thanks.

Here is what my main class looks like now. Seems ok, or could be setup better?

clsMainClass code:


using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

public class clsMain
{
private static ArrayList queryArray = new ArrayList();

private static string ocs = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db.mdb";
private static OleDbConnection odc = new OleDbConnection(ocs);

public clsMain()
{ }

private static void QueryColumn(string which_table, string which_column)
{
queryArray.Clear();

string SQL = "SELECT * FROM " + which_table;
OleDbDataAdapter oda = new OleDbDataAdapter(SQL, odc);

DataSet ds = new DataSet();
oda.Fill(ds);

foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
queryArray.Add(dr[which_column]);
}
}

odc.Close();
}

public struct ProgramSettings
{
public string psWindowLocation;

public static ProgramSettings Load()
{
ProgramSettings obj;

clsMain.QueryColumn("tProgramSettings", "SettingsValue");

obj.psWindowLocation = queryArray[0].ToString();

return obj;
}

public void Save()
{
// Save properties of 'this' to file here.
}
}
}


frmMain code:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public partial class frmMain : Form
{
clsMain.ProgramSettings ps = clsMain.ProgramSettings.Load();

//[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}

public frmMain()
{
InitializeComponent();

// Load the program settings.
MessageBox.Show(ps.psWindowLocation.ToString());
}
}

jmcilhinney
Apr 15th, 2006, 06:03 PM
I wouldn't nest the ProgramSettings structure in the main Class. Also, if you're saving things like window locations then you really should be using application settings as that's exactly what they're for. You go to the ApplicationSettings section of the Properties window and create a new setting for the Location property of the form. If you give it Application scope then it is the same for every user and it is read-only at run time. If you give it User scope then a value will be saved for each Windows user who uses your app and any changes made to the Location property at run time will be saved and reloaded automatically at next startup. In the screen-shot below the icon by the Location property indicates that it is bound to a setting value. If you want more information about application settings then start here (http://msdn2.microsoft.com/en-us/library/0zszyc6e(VS.80).aspx). Just note that VB 2005 has an additional mechanism to access settings that is easier than C#, so some information will be VB-specific.

ThisIsMyUserName
Apr 15th, 2006, 08:56 PM
Either I need to get smarter or microsoft needs to hire someone who knows how to explain things.

I like the idea of using application settings, but after reading their ENTIRE section on the subject, I still don't know how to get the settings to save when the program exits.

jmcilhinney
Apr 15th, 2006, 09:00 PM
I haven't used settings in a C# app yet. I'll give it a go and let you know if I find anything worthwhile, but it may not be in the next 24 hours.

ThisIsMyUserName
Apr 16th, 2006, 12:47 PM
You can skip looking into it if you'd like. I'm going to ditch this method of loading/saving, and just manually create my own XML file.

The whole property loader integration seems buggy. I was trying to save a few check states in a context menu, in addition to a combo box selection, etc.

1) It keeps resetting the property of the context menus to None everytime I close the IDE.
2) There was no property (that I saw) to save the combo box's "selectedindex" value.
3) To save files it was as simple as typing:

Properties.Settings.Default.Save();

However upon doing this it would only save the window location successfully. Getting the values of other properties on load worked though, so I don't think it's an issue of setting the incorrect property.

Example:

I manually set one of the context menus to True in the config file, then on form load I put:

MessageBox.Show(Properties.Settings.Default.enableMyCheckBox.ToString());

It returned true, yet the check in the menu was set to false.