I am not a C# programmer, but I have created an app and the final piece that I would like to have function is a static value that I can access and increment by 1 each time I issue a new control number to an output document.
I was reading that static properties might be a good thing for this. I have the following code for the static property but not sure how to initialize it to say 000000001 and GET then SET value++ each time I use it. I also dont know if this is a better solution than say a file containing the control number and just reading then updating the file. what is the longevity of the value contained in the static property?
so my questions are as follows:
1 how do I initialize this based on the code below
2 how do I increment this number , in the method or in the class calling the method?
3 is this a good idea or should I stick with a file or database to carry the control number
using System;
namespace GeneralApp{
static class InterchangeControlNumberCounter
{
public static int ControlNumber
{
get
{
return ControlNumber;
}
set
{
ControlNumber++;
}
}
}
}
thank you
Loading
Mike GoldPosted Sep 14, 2010, 11:37 AM
kieth biasilloPosted Sep 14, 2010, 11:27 AM
So with extreme gratitude, I thank you for taking the time out of your day to be so helpful.
Sam HobbsPosted Aug 17, 2010, 12:01 AM
You can save the value in the application's properties; properties are easy to use since they are designed for things such as this. You can put many things in the properties.
So I think I would have a static member variable in the class that has the static method. I would load the value from the property setting, then I would increment the member variable in the property. When the program finishes, I would sve the value back to the property settings.
Mike GoldPosted Aug 16, 2010, 10:53 PM
class PersistentControlCounter
{
private readonly string _outputFilePath;
public long Counter { get; set; }
public PersistentControlCounter(string outputFilePath)
{
_outputFilePath = outputFilePath;
Read();
}
private void Read()
{
try
{
if (File.Exists(_outputFilePath))
{
var streamReader = new StreamReader(_outputFilePath);
var val = streamReader.ReadLine().TrimStart('0');
streamReader.Close();
long counter;
if (long.TryParse(val, out counter) == false)
{
throw(new Exception("file does not contain a number"));
}
Counter = counter;
}
}
catch (Exception ex)
{
Console.WriteLine("Couldn't read counter --> {0}", ex.Message);
}
}
public void Increment()
{
Counter++;
Write();
}
private void Write()
{
try
{
// read the existing value first
var fileStream = new FileStream(_outputFilePath, FileMode.Create, FileAccess.Write);
var streamWriter = new StreamWriter(fileStream);
streamWriter.Write(String.Format("{0:000000000}",Counter));
streamWriter.Close();
}
catch (Exception ex)
{
Console.WriteLine("Couldn't save counter --> {0}", ex.Message);
}
}
}
}
Mike GoldPosted Aug 16, 2010, 10:45 PM
using System;
using System.IO;
namespace ConsoleApplication1
{
class PersistentControlCounter
{
private readonly string _outputFilePath;
public long Counter { get; set; }
public PersistentControlCounter(string outputFilePath)
{
_outputFilePath = outputFilePath;
Read();
}
private void Read()
{
try
{
if (File.Exists(_outputFilePath))
{
var streamReader = new StreamReader(_outputFilePath);
var val = streamReader.ReadLine();
streamReader.Close();
long counter;
if (long.TryParse(val, out counter) == false)
{
throw(new Exception("file does not contain a number"));
}
Counter = counter;
}
}
catch (Exception ex)
{
Console.WriteLine("Couldn't read counter --> {0}", ex.Message);
}
}
public void Increment()
{
Counter++;
Write();
}
private void Write()
{
try
{
// read the existing value first
var fileStream = new FileStream(_outputFilePath, FileMode.Create, FileAccess.Write);
var streamWriter = new StreamWriter(fileStream);
streamWriter.Write(Counter);
streamWriter.Close();
}
catch (Exception ex)
{
Console.WriteLine("Couldn't save counter --> {0}", ex.Message);
}
}
}
}
to call it:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var persistentControlCounter = new PersistentControlCounter(@"c:\tmp\tmp.txt");
persistentControlCounter.Increment();
Console.WriteLine("Hit Enter to continue");
Console.ReadLine();
}
}
}
I've uploaded the code for you as well.