|
It fails on the var version = call propeller function line, but only on that one machine (so far). I read the documentation with the 2 words in the long, can you explain how they are converting the numeric value to text? I had it print out as binary and I still couldn't figure out how it was being converted?
|
|
|
|
|
Where are you getting the text from??
NM. I just found in it in the code you originally posted. If it is an address, which your code seems to indicate it is, then the library is just storing a string and passing the address to you.
But, if this really is the case, the documentation doesn't match what the library is doing and you should direct all further queries about this library to the people who wrote it.
|
|
|
|
|
Ok that confirms my confusion. There is another way to get the same value by using an int instead of a ptr (if I remember correctly), I will check my code and post that as well when I get home to see if heads or tails can be made of that.
|
|
|
|
|
I'm not sure why this would behave differently on different win7 machines ... but calling the InitPropellent function in form1_load fixed this issue? Strange eh?
InitPropellent((long)this.Handle, false, "");
|
|
|
|
|
No, not strange. You just converted the Handle value to a 64-bit integer. This tells me that your other machine where it works is a 32-bit machine and your Win7 machine is 64-bit.
|
|
|
|
|
Both machines are actually 64 bit, although I have the project set to compile to 32 bit output since the dll is 32 bit.
|
|
|
|
|
I would like to define a property that holds 3 chars, so Im thinking of an array. Is this the way to do it? Can it be done with an automatic property somehow?
public abstract class Package
{
private char[] request;
public Package()
{
request = new char[3];
}
public char[] Request
{
get { return request; }
set { request = value; }
}
}
|
|
|
|
|
There are some ways you can do. What to choose, depends on your application, but most likely, way 1 and 2 will be most helpful to you.
- Use an array, as you did. If you do this, check for the length when the array is changed:
set
{
if (value.Length == 3)
{
request = value;
}
else
{
throw new Exception("Length of Request must be 3.");
}
}
- Use a
Tuple : http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx[^]
private Tuple<char, char, char> request;
public Package()
{
request = new Tuple<char, char, char>('\0', '\0', '\0');
}
public Tuple<char, char, char> Request
{
get { return request; }
set { request = value; }
}
Items in the Tuple can be accessed like Request.Item1 , Request.Item2 , Request.Item3 .
- As properties in your class. Personally, I wouldn't use this unless it is really necessary for some reason.
private char _char1;
private char _char2;
private char _char3;
public Package()
{
_char1 = _char2 = _char3 = '\0';
}
public char Char1
{
get { return _char1; }
set { _char1 = value; }
}
The quick red ProgramFOX jumps right over the Lazy<Dog> .
|
|
|
|
|
Arrays are so last century. Better options would be a string, a Tuple, a custom class or struct.
|
|
|
|
|
As Piebald suggests, a struct would be a lot better than an array: an array is a reference type, so it both takes more space and is slower to use than a struct - which is a value type, so it is "inline data" rather than a separate heap allocation and a reference field. And you can name the struct fields, which should make your code more readable as well.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
That code has the interesting issue that the returned array can easily, even accidentally, be used to mess up the state of the Package. That's not necessarily the Worst Thing Ever (it is always possible to mess up objects with reflection anyway), but it's something to at least be aware of.
|
|
|
|
|
I think the "best practice" ... the implementation details ... will be an "organic" function of the way you intend to use this data-structure; including what you want your code to communicate to yourself, and, possibly, others, in the future.
One suggestion: before you draw any simple one-size-fits-all conclusions about how .NET allocates memory for Structs, and other "Value Types" compared to "Reference Types," please read Eric Lippert's two articles on this topic: [^], [^]. Eric is a gurus'-guru who was a key player in creating .NET for many years, and I think these articles provide a very valuable insight into what actually goes on in memory in .NET ... perhaps not it's not as clear-cut as many people think.
There are many ways you could implement this, from simple to fancy, from one-shot single-purpose to general-use multi-purpose; a few years ago I wrote a multi-purpose class for maintaining a set of selected Char that was, perhaps, too fancy: I just looked at the code tonight and wondered why I made it so elaborate ... but I have a "thang" for writing multi-purpose
If you care to say more about your goals here, I'll respond, and I am sure others will.
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
TMattC wrote: Can it be done with an automatic property One further comment: an automatic property is one where the private backing-field is created for you by using the declarative syntax:
private char[] CharProp { set; get; }
The moment you specify a "body" for either the 'set or the 'get, it's no longer an automatic property, and, since it's clear your use-case would involved accessing characters within the Array here, you'd have to do that in the 'get, and, for that to be done, you'd have to have an value to use as an Index into the Array, and there's no way to "pass in" a value to be used internally in a 'get, or 'set.
The next version of C#, C#6, is going to add some features to Property declaration: [^].
Of course, with a Property whose Type is Array you can always use the 'SetValue and 'GetValue methods of the Array object; however, as you'll see in this example, 'GetValue returns a Type Object which will need to be cast to 'Char to be used in the way you probably want to use it.
private char[] CharProp { set; get; }
private void TestCharProp()
{
CharProp = new char[3] {'a', 'b', 'c'};
char value2 = (char) CharProp.GetValue(2);
CharProp.SetValue('x',2);
} That's not the type of code I'd write: no error check for null; no check for bounds; unnecessary cast required. But, you can write your own methods to 'set and 'get, of course that do validate and do avoid the cast.
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
I am working with a FileSystemWatcher and intercepting 'FileCreated and 'FileChanged events: no problem there.
The file is kept open in a simple text editor application.
I read the file using this code:
private string[] theFileContents;
private void ReadTheFile(string thePath)
{
theFileContents = File.ReadAllLines(thePath);
} If I start with the file inside the watched folder, the first time I change the text in the text editor and save, I get the notification, and can verify I am reading the changed text.
The second time I edit and save in the text editor I get the "in use" error System.IO.IOException:
"The process cannot access the file because it is being used by another process.
If I start with the file outside the watched folder and then drag-drop it in: I get the 'created notification, and can read the file.
If I then edit the file in the text editor and save, I get the "in use" error again.
Is there a way to keep the file open for reading during multiple FileSystemWatcher events ?
If there isn't, I am thinking that maybe copying the file, and then reading the copy, and then discarding the copy is one possible strategy.
Appreciate your thoughts !
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
modified 2-Jan-15 8:25am.
|
|
|
|
|
Whenever you open the file for writing, you establish an exclusive lock on it - and no other process can get access to the file for read or write. So you need to look at your "simple text editor" and find out exactly what it does with files - some (primitive) ones open the file for read and write and hold it until they are finished (which is a pain if you have automated backups running)
And that sample code opens the same file twice...first to create a FileStream, and then again to actually read the data. You don't need the stream at all:
theFileContents = File.ReadAllLines(thePath); creates it's own stream and reads the file through that. Try without the stream, and see if the problem goes away.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thanks, Griff,
Using only File.ReadAllText this happens:
Test 1
1. text file is open in NotePad++ and has been saved to the Desktop
2. drag-drop file into watched folder: created event fires, no error, file read as expected
3. change text in the file in NotePad++ and save without changing save-directory location: changed event fires; no error, file read as expected
4. change text in NotePad++ again: "in use" error
Test 2
1. text file is open in NotePad++ and has been saved to watched folder before the .NET is run
2. change text in the file in NotePad++:
first, and second edits/and/saves to same location:
changed event fires; no error, file read as expected
third edit and save to same location: "in use" error
Happy New Year !
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
I wonder if it's Notepad also monitoring the file it "has open" even though it's closed it so that it can track changes outside the editor - I use PSPad which does that - and the two processes are getting the notification at the same time and one is finding the file in use as a result?
Might be worth a sleep-retry-sleep-retry to give it another chance?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thanks again, Griff,
I am able to work around the problem by simply copying the dragged-dropped/changed file, then reading/processing the copied file, then deleting the copy. So, obviously, I do not alter the original in this scenario.
cheers, Bill
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
If I helped, then you are welcome!
Somehow, I don't think I did...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Bill, can I ask why you're reading the file contents in this way? You do a File.Open , create a StreamReader and associate it with the file, then you don't do anything with it? Instead, you do File.ReadAllLines on the file. Why not just use File.ReadAllLines instead? It opens, reads and closes the file.
|
|
|
|
|
Thanks, Pete,
Yes, you're absolutely right; OriginalGriff pointed this out to me, and ... my bad ... I have not corrected the code.
cheers, Bill
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
In C++, we can add reference to an array element as shown below:
int aArray[50];
int& Ref = aArray[1];
How do we do the same in C#?
|
|
|
|
|
You don't.
An int is not a reference type - it's a value type. You cannot "take a reference" to a value type.
The closest you can come is to pass the array element by reference to a method:
public void ChangeIt(ref int x) { x += 1; }
...
int[] aArray = Enumerable.Range(1, 50).ToArray();
ChangeIt(ref aArray[3]);
C++ and C# just share sufficient syntax to look similar: they are very different languages, and you should not try to "force" c++ ways of working on C# - it will end in tears...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Just to add a minor-key note to this post:
If you are going to need to change a lot of values in an Array, you can pass the entire Array by reference.
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
You don't need to - an array is a reference type anyway (even if it's an array of value type elements) so you can do this and it'll work:
int[] arr = Enumerable.Range(1, 50).ToArray();
ChangeIt(arr);
...
public void ChangeIt(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i]++;
}
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|