Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - OOP - Am I heading along the right track?
    text
    copied!<p>First post, long time reader.</p> <p>I'm currently learning C# with "Head First C#" (I'm up to Encapsulation and Get/Set properties)</p> <p>I'm writing a small program to work with pictures for a friend, I'm just wondering if I'm heading along the right lines with my PictureController class? My main problem is that I am setting a lot of form items with this class, and it feels unnatured to keep referencing form items from within the class, I'm pasting my code below, if you could let me know if I'm doing anything wrong then I'd be most grateful :) </p> <p>Many thanks!</p> <p><strong>PictureController.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; namespace PictureController { class PictureController { private int arrayPosition = 0; private int numFiles = 0; private string[,] arrayPictures; public PictureBox myPictureBox; public RadioButton myCopyButton; public RadioButton myDeleteButton; public TextBox mySource; public ComboBox myDestinations; private FolderBrowserDialog sourceFolder; private FolderBrowserDialog destFolder; public void InitialisePicture() { if (arrayPictures != null &amp;&amp; arrayPictures.Length &gt; 0) { myPictureBox.ImageLocation = arrayPictures[arrayPosition, 0]; } else { MessageBox.Show("The folder you have selected contains no pictures..."); myPictureBox.ImageLocation = null; } } public void NavigatePicture(int direction) { if (arrayPosition + direction &gt;= 0 &amp;&amp; arrayPosition + direction &lt; numFiles) { arrayPosition += direction; myPictureBox.ImageLocation = arrayPictures[arrayPosition, 0]; myCopyButton.Checked = Convert.ToBoolean(arrayPictures[arrayPosition, 1]); myDeleteButton.Checked = Convert.ToBoolean(arrayPictures[arrayPosition, 2]); } } public void UpdateActions(bool copyChecked, bool deleteChecked) { if (arrayPictures != null) { arrayPictures[arrayPosition, 1] = copyChecked.ToString(); arrayPictures[arrayPosition, 2] = deleteChecked.ToString(); } } public void GetFiles() { sourceFolder = new FolderBrowserDialog(); sourceFolder.ShowDialog(); if (sourceFolder.SelectedPath != "") { string[] arrayTempFiles = Directory.GetFiles(sourceFolder.SelectedPath,"*.jpg"); numFiles = arrayTempFiles.Length; arrayPictures = new string[arrayTempFiles.Length,3]; for (int i = 0; i &lt; arrayTempFiles.Length; i++) { arrayPictures[i, 0] = arrayTempFiles[i]; arrayPictures[i, 1] = "false"; arrayPictures[i, 2] = "false"; } mySource.Text = sourceFolder.SelectedPath; InitialisePicture(); } } public void AddDestinationFolder() { destFolder = new FolderBrowserDialog(); destFolder.ShowDialog(); if (destFolder.SelectedPath != "") { myDestinations.Items.Add(destFolder.SelectedPath); } } } } </code></pre> <p><strong>Form1.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace PictureController { public partial class Form1 : Form { PictureController PicControl; public Form1() { InitializeComponent(); PicControl = new PictureController() { myPictureBox = pbPhoto, myCopyButton = rbMove, myDeleteButton = rbDelete, mySource = tbSource, myDestinations = cbDestination }; } private void btnPrev_Click(object sender, EventArgs e) { PicControl.NavigatePicture(-1); } private void btnNext_Click(object sender, EventArgs e) { PicControl.NavigatePicture(1); } private void rbMove_CheckedChanged(object sender, EventArgs e) { if (rbMove.Checked || rbDelete.Checked) { PicControl.UpdateActions(rbMove.Checked, rbDelete.Checked); } } private void rbDelete_CheckedChanged(object sender, EventArgs e) { if (rbMove.Checked || rbDelete.Checked) { PicControl.UpdateActions(rbMove.Checked, rbDelete.Checked); } } private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.A: PicControl.NavigatePicture(-1); break; case Keys.D: PicControl.NavigatePicture(1); break; case Keys.W: rbMove.Checked = true; break; case Keys.S: rbDelete.Checked = true; break; } } private void btnGetFiles_Click(object sender, EventArgs e) { PicControl.GetFiles(); } private void btnProcess_Click(object sender, EventArgs e) { } private void btnAddDest_Click(object sender, EventArgs e) { PicControl.AddDestinationFolder(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload