|
Do you see any problems with my method signatures?
This is the parent, GenericPC.cs:
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special dynamic data");
}
This is the child, PC.cs:
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
The parent, GenericPC.cs calls the method this way:
ValidateDynData(itsEnteredDynamicData, ref returnCode);
I checked the correct version of Generic.dll is referenced in the child's project/class. I did a clean build in the child class. Any other binaries that should be checked?
|
|
|
|
|
I've been looking into this some more, and I think the reason flow in the parent is not seeing the overridden ValidateDynData in the child is because of the way I get flow into the parent from the child class:
Is there another way to get to the parent's method without creating an instance of the parent?
GenericPC baseInst = new GenericPC();
return baseInst.GetPC(cbInst, dllSel, templ, dll, cert0, slaveIndex, BoxID);
|
|
|
|
|
Is it possible to pass parameters from unmanaged code to managed code by reference?
I am programming agents for the Metta Trader 4 (MT4) platform using MetaQuotes Language 4 (MQL4); a C like language.
Normally one would use C++ to generate DLLs but I would like to use C# if I could because when you release something in C++ it is frozen in time as opposed to when you release something in C#. With C# if problems are detected in the underlieing classes they are corrected by normal updates of the .NET platform. This is an advantage I would like to take advantage of to simplify maintenance of products I hope to sell.
I will need to pass large arrays of market data to these DLLs for analysis and would like to do this by reference.
Here is an example of how to pass an array by value:
[DllExport(EntryPoint = "SumIntegerArray")]
public static int Sum([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]int[] values, int count)
{
int result = 0;
for (int i = 0; i < count; i++)
result += values[i];
return result;
}
How would I pass the array by reference to the DLL?
|
|
|
|
|
You could pass a pointer and use unsafe on the C# end instead of arrays. AFAIK you can't (easily) pretend that a native array is a managed array.
|
|
|
|
|
Thanks for the information, I will try it.
I am just unsure how to accomplish it in C#. I didn't know you could use a pointer in managed code.
Off to the articles to see if I can find out how.
|
|
|
|
|
It mostly works as you'd expect, same syntax and all, but you can't take the address of managed objects and you need "unsafe" keywords (and enable unsafe code in the build options).
Also, "int *x, y" makes both of them pointers, not just x.
|
|
|
|
|
Thanks for the information.
|
|
|
|
|
As an alternative, you could put it in a memory mapped file[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't think it would be applicable in my case but I will look into them.
Thanks for the information.
|
|
|
|
|
I want to get the cursor coordinates when the mouse is clicked in inkcanvas.here i used mouse event handler and i am trying to display cordinates in a textbox but somehow its not working.i have used the following code snippet.can someone please help
private void inkCanvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Windows.Point position = e.GetPosition(this);
this.textBox1.Text = position.X.ToString()+","+position.Y.ToString();
}
|
|
|
|
|
I am assuming its winforms
but have a look a
Cursor.Position.X
Further reading System.Windows.Forms.Cursort.Position[^]
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
|
|
I've inherited a wrapper for a gps interpretter. The guy who wrote it is dispose happy, I'm guess from the old days with .net. I once had to go back and change all the close calls in managed database connections to .dispose(). This was code we were maintaining, but not code that was touched in my update. So I had to go through the whole project and change them to prevent memory leaks that wasn't there?
Anyway, I found the msdn page, after, that said that wasn't necessary. That still wasn't proof enough?
Ok, so this wrapper is a static class. It has a member NmeaInterpreter, and a method dispose. Dispose says :
if( GpsInterpreter.Device != null)
{
GpsInterpreter.Device.Dispose();
}
GpsInterpreter.Dispose();
I don't even think that is necessary. Is it?
If it moves, compile it
|
|
|
|
|
loctrice wrote: I don't even think that is necessary. Is it?
Depends. Not disposing means that it'll be disposed of when the GC runs, which usually happens when the system is low on memory. That might also cause other apps to run into problems when trying to allocate memory.
There's another problem with not disposing objects, if they reference unmanaged resources; a stream or a SqlConnection needs be disposed of. Given a file-stream, the file can be "locked" until the reader is disposed of.
The advantage of disposing aggressively is performance. Still, I don't implement dispose unless I have to.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I will leave the dispose in there, just to be on the safe side. It's a mobile device that we are using, so resources are resources.
I always thought, and programmed, that an Sql connection and/or file stream can be .close() 'ed without issue. They don't need both close and dispose called, it's one or the other and close is "good enough"
If it moves, compile it
|
|
|
|
|
loctrice wrote: They don't need both close and dispose called, it's one or the other and close is "good enough"
Dispose would also close it. Closing it will not dispose it, but will return the connection to the pool. This will not work for all classes in the same way though.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
am created sdk for rfid card reader it reads the rfid value and display the output in that sdk screen i want to use the rfid values in my form to use the rfid value
|
|
|
|
|
And? What's your question?
|
|
|
|
|
You don't really know what an SDK is, do you?
|
|
|
|
|
I have a project that i have to create and write to a text file using the StreamWritter class. i create the file just fine and after playing with it a little got the text to enter into the file correctly but i cant add more text to the file after the inital line or it just truncates whats already inside of it, i want the text inside to add each time the button is clicked even after the program closes and i reopen it. here is my code so far
using System.IO;
namespace SWardVideoSalesApplication
{
public partial class MainSalesForm : Form
{
public MainSalesForm()
{
InitializeComponent();
}
#region Variables
StreamWriter testFile = File.CreateText("TestFile2.txt");
#endregion
private void summaryButton_Click(object sender, EventArgs e)
{
bool Flag = true;
testFile = File.
if (Flag)
{
testFile.WriteLine("This is a test {0}", todayDate.ToString());
testFile.Close();
}
else
{
MessageBox.Show("ERROR");
}
}
|
|
|
|
|
Please ignore the testFile = File. statement i was playing around with an idea and forgot to delete it before i posted my code
|
|
|
|
|
how about :-
using (StreamWriter testfile = new StreamWriter("TestFile2.txt", true))
{
testfile.WriteLine("This will be appended");
}
'g'
|
|
|
|
|
Hi,
The answer to your question is to set the stream writer to append mode.
Try this:
string _fileName = "TestFile2.txt";
private void summaryButton_Click(object sender, EventArgs e)
{
bool Flag = true;
if (Flag)
{
string content = string.Format("This is a test {0}", todayDate.ToString());
writeToFile(_fileName, content);
}
else
{
MessageBox.Show("ERROR");
}
}
private void writeToFile(string fileName, string content)
{
FileInfo fi = new FileInfo(fileName);
if (!fi.Exists)
{
MessageBox.Show("file doesn't exists!");
return;
}
else
{
using (StreamWriter sw = new StreamWriter(fileName, true, System.Text.Encoding.Default))
{
sw.Write(content);
}
}
}
Hope this helps.
Regards
Jegan
Think! Don't write a line of code unless you absolutely need to.
|
|
|
|
|
The reason the code is failing is because the following is happening:
- You create the TestFile2.txt the first MainSalesForm is instantiated. Subsequent instantitations of MainSalesForm will overwrite the file effectively blanking it. (try prepenging the DateTime.Now.Ticks to the filename to see when the file is actually being created/overwritten).
- On button click, you write to the file and close the stream so the file can no longer be written to. If the CreateFile is executed later (say the form is closed and reopened) it will overwrite what has been written.
I suggest the following code:
private void summaryButton_Click(object sender, EventArgs e)
{
WriteToFile(string.Format("This is a test {0}", todayDate.ToString());
}
private void WriteToFile(string fileName, string content)
{
FileInfo fi = new FileInfo(fileName);
if (!fi.Exists)
{
using(StreamWriter testFile = File.CreateText(fileName)
{
sw.Write(content);
}
}
else
{
using (StreamWriter sw = new StreamWriter(fileName, true))
{
sw.Write(content);
}
}
}
Once the button is pressed the file will be created (if it does not exist) or will be appeneded to with the string passed to the method. Note that the file is not locked between clicks (so you can open it for reading and writing). This may not be the desired behaviour, if not the code will be unsuitable. The code I have provided may not compile (I haven't tested it) and could be tidied a little but it should get you on track.
|
|
|
|