|
We can't see your tag: Try checking this before pasting your XML: Encode "<" (and other HTML) characters when pasting
edit: Damm, my browser cache.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<CONNECTION HOST =\"XYZ\"/>");
XmlElement xml = doc.DocumentElement;
MessageBox.Show("Atribute name: " + xml.Attributes[0].Name + "\r\n Atribute Value: " + xml.Attributes[0].Value);
|
|
|
|
|
Hi,
I'm a newbie and need some help.
How do I go about saving a variable using c# code.
|
|
|
|
|
It depends on the type of the variable and what you mean by 'saving'... to a file, a database, in memory, sharing with another class etc...
|
|
|
|
|
Thanks alot for the reply.
I need to save the variable temporarily for use later.
Thanks in advance for any help you can provide...
|
|
|
|
|
|
I would say hours at the most
|
|
|
|
|
Look into variable SCOPE, assuming you are not talking about saving the value past the end of the current session (stopping the application) a variable is available until it runs out of SCOPE. Do NOT fall into the VB trap of making all your variables global or static. Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi!
I have a problem. I have created a time sheet system that enables a person to enter a start time and an end time. It then calculates the total hours worked.
The problem I have is this:
When a person has been on lunch from say 11:00 to 11:30 and then wants to enter a time worked say 11:15 then an exception should be thrown stating that times cannot overlap. How will I go about checking for this?
Thanks in advance!!Illegal Operation
|
|
|
|
|
two intervals [b1,e1] and [b2,e2] overlap when any of the following is true:
b1<=b2<=e1
b1<=e2<=e1
b2<=b1<=e2
b2<=e1<=e2
So start comparing begin and end DateTimes.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
modified on Sunday, February 28, 2010 8:15 PM
|
|
|
|
|
If you google for c# DateRange you will find a number of classes that people have written to address this sort of problem, i.e. dealing with fixed periods of time and testing for intersection with other periods, and so on. One example is here:
http://noticeablydifferent.com/CodeSamples/DateRange.aspx[^]
This example is OK, except I would make it immutable since in its present form it's not thread-safe.
|
|
|
|
|
Hi,
I'm trying to move a file from 1 directory to another.
But no matter what I try, I keep getting this error at runtime:
Cannot create a file when that file already exists.
string[] filePaths = Directory.GetFiles(dir1, "*.txt");
Directory.Move(filePaths[i], checkedFiles);
All the files in filePaths are in bin\debug\FilesToCheck folder
And checkedFiles folder is in bin\debug\checkedFiles
What am I doing wrong?
|
|
|
|
|
Make sure checkedFiles ends on a backslash, making it a folder, not a file.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
It has an ending backslash. The file is made with Path.Combine()
And in there i added the ending slash.
|
|
|
|
|
Please show us the first and last of the filePaths[] elements, and the checkedFiles value, as seen by your app.
Why do you use Directory.Move rather than File.Move ?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
I tried them both. And both give me the same error at runtime.
Here are the 2 elements in the filePaths[]:
[0]"C:\\Users\\The User\\Documents\\Visual Studio 2008\\Projects\\ProjectMultiThread\\ProjectMultiThread\\bin\\Debug\\filesToCheck\\634029044974663752.txt"
[505]"C:\\Users\\The User\\Documents\\Visual Studio 2008\\Projects\\ProjectMultiThread\\ProjectMultiThread\\bin\\Debug\\filesToCheck\\634029930615165770.txt"
The checkedFiles variable:
C:\Users\The User\Documents\Visual Studio 2008\Projects\ProjectMultiThread\ProjectMultiThread\bin\Debug\Checked Files\
I tried the 'checked files' with and without a space.
Just for the record, I'm trying to move this file from folder filesToCheck: '634029930615165770.txt'
To the 'Checked Files' folder.
I have used File.Move and:
FileInfo fi = new FileInfo(this.filesToCheck);modified on Monday, March 1, 2010 5:28 AM
|
|
|
|
|
Luc Pattyn,
I fixed the problem. I found this site:
http://www.devnewsgroups.net/dotnetframework/t1557-file-move-file-copy-exception.aspx[^]
And: Jacky Kwok's reply to someone else's post did the trick.
But I don't quite understood what he said. A quote from what I mean:
[quote]
The "BackUpPath" in both "Copy" and "Move" in your code are directory only.
However, the "File.Copy" and "File.Move" require it is filename.
[/quote]
If I understand this correctly, File.Move wants in both arguments a File name like this:
File.Move(file1, file2);
Isn't that odd? I'm trying to move a file from one directory to another. But here, you're moving a file to another file?
|
|
|
|
|
Yes, File.Move takes two filenames, that is perfectly clear in the documentation.
FYI: when both files reside in the same disk partition, the file data is not copied, the file is just renamed, i.e. one or two folder lists get modified.
The documentation on Directory.Move is not very clear however; AFAIK it can move an entire folder (when given two folder names), or it moves a single file (when given two file names).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
So you're saying i have to pass the same file name to the destination argument too?
|
|
|
|
|
Hi Luc,
It seems that the files which are moved, are somehow messed up.
- If i try to open it, it's empty and ms windows is 'asking' me if i want to create a new file.
- If i try to delete the file, windows is 'telling' me that the file "is no longer located in the-file-path".
I was hoping you could help me out here.
|
|
|
|
|
hello everybody,
i need to wrap a Queue so multiple threads will be able to add/get messages from it. if the queue is empty - the thread will wait until it has data.
it works for a couple of seconds and then throws an exceptions that my Queue is empty. why is that?
thanks,
public class SyncQueue<T>
{
private Queue<T> queue;
private object readerLock;
private object queueLock;
private AutoResetEvent dataAvailable;
public SyncQueue()
{
this.queue = new Queue<T>();
this.readerLock = new object();
this.queueLock = new object();
this.dataAvailable = new AutoResetEvent(false);
}
public void Enqueue(T item)
{
lock (queueLock)
{
queue.Enqueue(item);
}
dataAvailable.Set();
}
public T Dequeue()
{
lock (readerLock)
{
if (queue.Count == 0)
{
dataAvailable.WaitOne();
}
lock (queueLock)
{
return queue.Dequeue();
}
}
}
} modified on Sunday, February 28, 2010 4:40 PM
|
|
|
|
|
I can't reproduce problem. I used 8 threads and try it also as static.
|
|
|
|
|
 input is: 1,2,3,4,5,6,7,8,9,10 (several times)
namespace Test
{
public class SyncQueue<T>
{
private Queue<T> queue;
private object readerLock;
private object queueLock;
private AutoResetEvent dataAvailable;
public SyncQueue()
{
this.queue = new Queue<T>();
this.readerLock = new object();
this.queueLock = new object();
this.dataAvailable = new AutoResetEvent(false);
}
public void Enqueue(T item)
{
lock (queueLock)
{
queue.Enqueue(item);
Console.WriteLine("Enqueued Item: " + item);
}
dataAvailable.Set();
}
public T Dequeue()
{
lock (readerLock)
{
if (queue.Count == 0)
{
dataAvailable.WaitOne();
}
lock (queueLock)
{
return queue.Dequeue();
}
}
}
}
public class Tester
{
SyncQueue<int> myQ = null;
public Tester(SyncQueue<int> queue)
{
myQ = queue;
}
public void ProcessMessages(object obj)
{
while (true)
{
Console.WriteLine(myQ.Dequeue());
}
}
}
public class Program
{
static void Main(string[] args)
{
SyncQueue<int> myQ = new SyncQueue<int>();
Tester t1 = new Tester(myQ);
Tester t2 = new Tester(myQ);
Tester t3 = new Tester(myQ);
ThreadPool.QueueUserWorkItem(new WaitCallback(t1.ProcessMessages));
ThreadPool.QueueUserWorkItem(new WaitCallback(t2.ProcessMessages));
ThreadPool.QueueUserWorkItem(new WaitCallback(t3.ProcessMessages));
while (true)
{
string[] snumbs = Console.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string snumb in snumbs)
{
myQ.Enqueue(int.Parse(snumb));
}
}
}
}
}
the exception is: Invalid Operation Exception - queue is empty.
any idea?
thanks,
|
|
|
|
|
It was pretty easy to debug when you posted actual test code. The problem lies within AutoResetEvent. WaitOne will not wait till its state isn't resetet. add this:
dataAvailable.Reset()
dataAvailable.WaitOne();
|
|
|
|
|
ok thanks,
but isn't autoResetEvent supposed to change it's own state automatically after WaitOne()?
i thought that's what it is doing...
maybe i don't understand the difference between auto and manual reset events. can anyone explain?
thanks again.
|
|
|
|
|