I am Very new at C# and have written a fairly clunky code. I have been doing a lot of courses online and a lot say that there are several ways to approach problems. Now i have made a program that will Load up a .Doc Word file and then search for the relevant information using if statements.
Now my problem with my solution is that this program takes FOREVER!!! I am talking about 30Mins - 1Hour to complete the following code.
Any ideas of how to make my little program a little less clunky? I hope that solutions to this will increase my knowledge substantially so thanks in advance everyone!
regards chris
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int id = 0;
public int[] iD = new int[100];
public string[] timeOn = new string[100];
public string[] timeOff = new string[100];
public string[] dutyNo = new string[100];
public string[] day = new string[100];
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = application.Documents.Open("c:\\Users\\Alien\\Desktop\\TESTJOBS.doc");
//the following for will loop for all words
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// the following if statement will look for the first word that is On
// this is then (on the file) proceded by 04:00 (thus i+2/3/4 respectively)
if (document.Words[i].Text == "On")
{
iD[id] = id;
// Console.WriteLine("ID Number ={0}", iD[id]);
dutyNo[id] = document.Words[i - 14].Text;
// Console.WriteLine("duty No set to:{0}", dutyNo[id]);
timeOn[id] = document.Words[i + 2].Text + document.Words[i + 3].Text + document.Words[i + 4].Text;
// Console.WriteLine("on time set to:{0}", timeOn[id]);
// the following if (runs if the last word was not "On" and then searches for the word "Off" which procedes "On" in the file format)
// this is then (on the file) proceded by 04:00 (thus i+2/3/4 respectively)
}
else if (document.Words[i].Text == "Off")
{
timeOff[id] = document.Words[i + 2].Text + document.Words[i + 3].Text + document.Words[i + 4].Text;
//Console.WriteLine("off time set to:{0}", timeOff[id]);
// the following if (runs if the last word was not "Off" and then searches for the word "Duty" which procedes "Off" in the file format)
// this is then (on the file) proceded by 04:00 (thus i+2/3/4 respectively)
}
else if (document.Words[i].Text == "Days" && !(document.Words[i + 3].Text == "Type"))
{
day[id] = document.Words[i + 2].Text;
//Console.WriteLine("day set to:{0}", day[id]);
//we then print the whole new duty out to ListBox1
listBox1.Items.Add(string.Format("new duty ID:{0} Time on:{1} Time off:{2} Duty No:{3} Day:{4}", iD[id], timeOn[id], timeOff[id], dutyNo[id], day[id]));
id++;
}
}
for (int i = 1; i <= 99; i++)
{
Console.WriteLine("new duty ID:{0} Time on:{1} Time off:{2} Duty No:{3} Day:{4}", iD[id], timeOn[id], timeOff[id], dutyNo[id], day[id]);
}
}
}
}