|
I think you're looking for this[^].
For example:
public class MyObjectClass
{
private string internalValue = "Something";
public static implicit operator int(MyObjectClass value)
{
return int.Parse(value.internalValue);
}
public static implicit operator MyObjectClass(int value)
{
return new MyObjectClass(value);
}
private MyObjectClass()
{
}
public MyObjectClass(int value)
{
internalValue = value.ToString();
}
}
And to use it:
MyObjectClass x = new MyObjectClass(201);
int value = (int)x;
|
|
|
|
|
Yes, thanks, this looks like a very good solution.
|
|
|
|
|
So, before we overcomplicate things, why aren't you just putting those objects into an array?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
DaveK's post here is an excellent lesson in use of 'implicit operators: I up-voted it, and, so should you !
I wonder why you want to use such a technique, involving casting, boxing, when all you need is a simple integer field, or property, in the class ?
public class MyClass
{
public int Id { private set; get; }
public MyClass(int id)
{
Id = id;
}
}
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
That's what I use today. Basically, I have portable products (with ARM processors inside) with registers that I can read from and write to with a PC-application:
uint32 readReg(uint address)
void writeReg(uint address, uint newValue)
In the beginning, I used enum that I casted into uint for the addresses, but then I found that I wanted to share some addresses across products but I couldn't figure out a way to inherit enum. So I created a class instead (see for example java - Simulate a class of type enum - Stack Overflow ) but now I wasn't able to cast into uint anymore, which I think is a cleaner solution.
|
|
|
|
|
in C# how can i get my app to start on open if i remove the start function, say i dont want to command it to start, but actually start as soon as i execute the exe file.
|
|
|
|
|
Do you want to try that again? This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So stop trying to type the minimum words you can get away with and help us to help you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i have a system that monitors the contents of a file. File checker created in C#, with a start and stop function, i want to use task scheduler to startup the app and stop it, but it cant start the actual file monitoring process, is the a way in which i could remove the start and stop function so that the application starts monitoring the files immediately its executed and stops when it is closed .
|
|
|
|
|
How do you expect us to know that, given that we can't see your code at all and have no idea how you have written it?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
All programs start as soon as you execute their exe files. Your question makes no sense.
|
|
|
|
|
Did you by accident write a windows service? If yes, delete the source-code; you shouldn't be writing those.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
is it possible to create a class that schedules a program to start and stops at specific times of the day, say it run from 00;00 to 23;59 every day
|
|
|
|
|
Within your app, yes - but it will only work if your app is running at the time.
If it isn't, you can't use a class to do that, because all class instances are destroyed at the end of the app.
You can however run an app at a specific time each day: Task Scheduler (Windows)[^] will let you do that pretty easily.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thanx, for example its a file checker, with start and stop function, at a specific time i would like it to stop and restart agin, while the app is still on. is there code i can view to help me with this
|
|
|
|
|
Just create a background thread - see the BackgroundWorker class[^] for a simple method and an example.
Then in the thread, use DateTime.Now to get the current date and time, and if you are outside the "running" time, call Thread.Sleep(60000) to wait for one minute before trying again.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
I'd tend to go with an Rx solution where I calculated the time difference between now and the end time, then use that as the parameter in an Onservable.Timer[^]. Then again, I have obviously have masochistic tendencies
This space for rent
|
|
|
|
|
Why don't you do this via a Windows service? A Windows service will run even if a user isn't logged on the machine. You can use a config file to control processing, or if it's a file checker, it will watch the specified file for changes (or folder for new files), and react accordingly.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
well am thinking of putting it on a server.
{
public partial class frmNotifier : Form
{
private StringBuilder m_Sb;
private bool m_bDirty;
private System.IO.FileSystemWatcher m_Watcher;
private bool m_bIsWatching;
public frmNotifier()
{
InitializeComponent();
m_Sb = new StringBuilder();
m_bDirty = false;
m_bIsWatching = false;
}
private void btnWatchFile_Click(object sender, EventArgs e)
{
if (m_bIsWatching)
{
m_bIsWatching = false;
m_Watcher.EnableRaisingEvents = false;
m_Watcher.Dispose();
btnWatchFile.BackColor = Color.LightSkyBlue;
btnWatchFile.Text = "Start Watching";
}
else
{
m_bIsWatching = true;
btnWatchFile.BackColor = Color.Red;
btnWatchFile.Text = "Stop Watching";
m_Watcher = new System.IO.FileSystemWatcher();
if (rdbDir.Checked)
{
m_Watcher.Filter = "*.*";
m_Watcher.Path = txtFile.Text + "\\";
}
else
{
m_Watcher.Filter = txtFile.Text.Substring(txtFile.Text.LastIndexOf('\\') + 1);
m_Watcher.Path = txtFile.Text.Substring(0, txtFile.Text.Length - m_Watcher.Filter.Length);
}
if (chkSubFolder.Checked)
{
m_Watcher.IncludeSubdirectories = true;
}
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
m_Watcher.EnableRaisingEvents = true;
}
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
}
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.OldFullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append("to ");
m_Sb.Append(e.Name);
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
if (rdbFile.Checked)
{
m_Watcher.Filter = e.Name;
m_Watcher.Path = e.FullPath.Substring(0, e.FullPath.Length - m_Watcher.Filter.Length);
}
}
}
private void tmrEditNotify_Tick(object sender, EventArgs e)
{
if (m_bDirty)
{
lstNotification.BeginUpdate();
lstNotification.Items.Add(m_Sb.ToString());
lstNotification.EndUpdate();
m_bDirty = false;
}
}
private void btnBrowseFile_Click(object sender, EventArgs e)
{
if (rdbDir.Checked)
{
DialogResult resDialog = dlgOpenDir.ShowDialog();
if (resDialog.ToString() == "OK")
{
txtFile.Text = dlgOpenDir.SelectedPath;
}
}
else
{
DialogResult resDialog = dlgOpenFile.ShowDialog();
if (resDialog.ToString() == "OK")
{
txtFile.Text = dlgOpenFile.FileName;
}
}
}
private void btnLog_Click(object sender, EventArgs e)
{
DialogResult resDialog = dlgSaveFile.ShowDialog();
if (resDialog.ToString() == "OK")
{
FileInfo fi = new FileInfo(dlgSaveFile.FileName);
StreamWriter sw = fi.CreateText();
foreach (string sItem in lstNotification.Items)
{
sw.WriteLine(sItem);
}
sw.Close();
}
}
private void rdbFile_CheckedChanged(object sender, EventArgs e)
{
if (rdbFile.Checked == true)
{
chkSubFolder.Enabled = false;
chkSubFolder.Checked = false;
}
}
private void rdbDir_CheckedChanged(object sender, EventArgs e)
{
if (rdbDir.Checked == true)
{
chkSubFolder.Enabled = true;
}
}
private void frmNotifier_Load(object sender, EventArgs e)
{
}
}
}
thats a full code of filemonitor i found online.
am trying to edit it in such a way that when i click the exe file it already starts checking the files, without having to click the start button and stop button.
|
|
|
|
|
Like I said, write it as a windows service, and it can just sit there watching the file/folder without any user interaction at all.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
thanks, so am new to programing, how can i write it as a windows service? sorry for asking too much,
|
|
|
|
|
0) I suspect that you don't have the requisite skillset to handle it. Windows services require a more intimate knowledge of how Windows works.
1) The act of writing a Windows service cannot possibly be covered on a programming forum. However, CodeProject has many dozens of articles that can show you the basics.
2) As a new developer, if you learn nothing else, learn this - google is your friend. If you don't learn how to use google, your development career will be less than stellar. Here's a Microsoft site that explains how to write a Windows service. I found it with google.
Walkthrough: Creating a Windows Service Application in the Component Designer | Microsoft Docs[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 2-Feb-18 6:59am.
|
|
|
|
|
Sort of. All you can guarantee is that, at some point, you should be able to make the application stop. The reason that I'm being cagey about this is because you can't exactly guarantee the point at which an operation is going to be run on a computer because there might be other tasks running that push your timing slightly out.
This space for rent
|
|
|
|
|
thanx, for example its a file checker, with start and stop function, at a specific time i would like it to stop and restart agin, while the app is still on. is there code i can view to help me with this
|
|
|
|
|
how to create the image button using asp.net program and how to browsing the image?
|
|
|
|
|