|
Hi,
Have a look at my article about Speech Recognition, Speech to Text and Text to Speech:
Speech recognition in C#[^]
The quick red ProgramFOX jumps right over the Lazy<Dog> .
|
|
|
|
|
1. Create a new VS windows form application.
2. Add a button to it.
3. Double click the button to add the click event.
4. Add a new class to the project. Change its declaration and make it a partial class using the same name as the main form.
5. Move the button click event into the partial class.
6. Go back to the main form and double click the button so that it opens the click event in the code window.
If, like my VS (2012) you'll find it doesn't actually open the event code. In fact, like mine, you'll probably find it places the cursor where the code used to be in the main form. In other words, it doesn't jump to the code in the other source file where you created the partial class and copied the code to. It also does the same in VS2008.
If it's a bug, is there a workaround?
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
Confirmed this is the same on my machine.
I searched and found this[^] which seems to say it's not considered a bug but by design as a limitation of the Forms designer.
|
|
|
|
|
No, it's not a bug, , there is no workaround for it, and if you think about it, it does make a bit of sense.
If you double-click on a control to generate an event, the code will go into the file of the same name as the myFormName.Designer.vb file. It's the only file that it knows with 100% certainty belongs to that form.
|
|
|
|
|
Upto a point, Dave, I agree with what you write. I also look it at it the other way where it could also search through all the same partial classes to see where it might reside. But, yes, if it's a limitation by intent I can live with it.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
The workaround.
In VS solution explorer the form.designer.cs file is shown as nested within form.cs and I suspect that the partial class that you have added is not. Fixing the project file to nest the new file will also fix the problem with the event handler methods.
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Form1.Extras.cs">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.MoreStuff.cs">
<SubType>Form</SubType>
</Compile>
In this project file fragment the entries for Form1.cs , Form1.designer.cs and Form1.MoreStuff.cs are exactly as created by VS but the entry for Form1.Extras.cs has been modified to establish the correct DependentUpon relationship to Form1.cs . Event handlers within Form1.Extras.cs will now be found by VS whereas any that were moved into Form1.MoreStuff.cs will not.
Alan.
|
|
|
|
|
Thanks for that Alan. I've not tried it yet but I'll schedule some playtime to test it tomorrow sometime.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
Alan,
Many thanks. I just had the time to try it out and it worked 100% fine. Double-clicking the button does indeed jump to the partial form.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
I'm trying to create a program that displays a table of all the even numbers within a range given by the user but im having some trouble making it work. the range has to be between 1 and 120. I would also like to incorporate the tryparse method. here is what I have so far:
using System;
public class Range
{
public static void Main(string[] args)
{
int[] range;
string firstNumber;
Console.Write("What is the initial number of the range: ");
firstNumber = Console.ReadLine();
Console.Write("What is the last number of the range: ");
string lastNumber;
lastNumber = Console.ReadLine();
int totalNumber;
int firstNum = Convert.ToInt32(firstNumber);
int lastNum = Convert.ToInt32(lastNumber);
if (firstNum < 1)
{
Console.WriteLine("Error: the firstNumber can't be less than 1.");
}
else if (lastNum > 120)
{
Console.WriteLine("Error: the lastNumber can't be higher than 120.");
}
else if (firstNum >= lastNum)
{
Console.WriteLine("Error: the first number can't be higher than the last number.");
}
totalNumber = lastNum - firstNum;
range = new int[totalNumber + 1];
int tally = 0;
for (int i = 0; i < totalNumber; i++)
{
range[i] = i + 1;
if (range[i] % 2 == 0)
{
Console.Write(range[i] + " ");
}
tally++;
if (tally > 10)
{
Console.WriteLine();
tally = 0;
}
}
Console.Write("\nPress any key to exit program: ");
Console.ReadKey();
}
}
|
|
|
|
|
You made an array "range" but didn't initialize its contents. It will only contain zero.
That array isn't necessary in the first place, you could just iterate over the range directly:
for (int number = firstNum; number <= lastNum; number++)
{
if (number % 2 == 0)
; print it or whatever
}
And you don't actually need that test to see if it's even, either, because you can make sure to only ever generate the even numbers:
for (int number = firstNum + (firstNum & 1); number <= lastNum; number += 2)
{
}
Of course this part: firstNum + (firstNum & 1) is something slightly special, it's rounding up to the next multiple of 2.
firstNum & 1 takes the lowest bit, which will be 1 if odd and 0 if even. Adding that back to the original number will not change it when it's even, but make it one higher if it's odd.
|
|
|
|
|
I have the following statement in a C# 2008 windows application that was converted to a console application:
finally
{
Console.ReadLine();
Environment.Exit(1);
}
<pre>
The error message that is displayed says there is not enough io memory.
Can tell show me in code and/or point me to a reference that will show me how to solve this problem?
|
|
|
|
|
Quote: The error message that is displayed says there is not enough io memory
The code you pasted here has no relation to the error message. It might be other part of your code creating this error. debug your application and run line by line and see on executing which method this error popsup.
also copy the complete exception stack here so that we can able to tell something from that.
|
|
|
|
|
Change the exit code: 0 indicates success, any other number is an error code. Exactly how the error code is interpreted depends on the environment the application is run under - I suspect that your app returning "1" is being interpreted as an I/O error.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
The program that is executed is called by a C# 2010 windows application (called app1) that was converted from a console application.
The following is an example of some of the calls made from app1 to the C# 2008 application that is having the memory problem called app2.
string[] SubPkgIDs = rData.details.Where(c => c.Package_ID.StartsWith("SUB").Select(c => c.PackID).Distinct().ToArray();
foreach (string SubPkgID in SubPkgIDs)
{
{
Process eProcess = new Process();
String Process_Arguments = null;
eProcess.StartInfo.UseShellExecute = false;
e_Process.StartInfo.FileName = "app2.exe";
Process_Arguments = " 3 " + SubPkgID;
eProcess.StartInfo.Arguments = Process_Arguments;
eProcess.Start();
eProcess.WaitForExit(1800);
eProcess.Dispose();
Process_Arguments = null;
}
}
I think the line of code that is causing the memory problem possibly is the following:
eProcess.WaitForExit(1800);
App1 is not waiting for a response basically from app2.exe program that is being called. It waits the 1800 *.secs and makes
the next calls. There is no reason for app1 to wait or app2 to finish executing since it does not need a response from app2.
However if this is causing the memory problem I can wait for the app2 program to finish executing.
App1 and app2 were setup to be single threaded.
In the app1 program that calls the second program called app2, I am thinking of putting the following code at the end of the program:
foreach (Process proc in Process.GetProcessesByName("app2"))
{
proc.Kill();
}
This way the app2 processes that are still in memory can be released.
Thus based upon what I mentioned previously and what I just mentioned above, can you show me code and/or tell me what you would do to solve the memory problem I am having?
|
|
|
|
|
You have two processes. It isn't clear which one is running out of memory but because they are different processes they are different and neither would impact the other UNLESS there is something odd with the environment setup.
If there is something odd in the environment they you should at least track that down and understand it first. It would have to do with a system and\or user limit on the maximum memory possible. You can't figure out the environment problem by looking at the code you have.
Otherwise if the following are all true
- App1, the one that uses Process, is single threaded (not just assumed but actually is.)
- App1 is the one running out of memory
Then something in App1 before you make the Process call is using a lot of memory.
If it is App2 then either App2, not App1, has a bug or one of the options you are passing is wrong.
|
|
|
|
|
I have the following addtional questions to ask you since I am the only programmer at my small company, I have have 1 year of experience, and I have never come across memory issues in the past"
1. Based upon your statement, "It isn't clear which one is running out of memory but because they are different processes they are different and neither would impact the other UNLESS there is something odd with the environment setup.". Can you tell me what would be an 'odd' setup in my environment?
2.If there is something 'odd' setup in the environment, can you tell me how to tell what is the system and/or user limit on the maximum memory possible? Is there a tool that I can use, a process that I can look at? Is there a reference I can use to know how to set set the maximum limits?
3. If app1 is using alot of memory before it makes the call, can you tell me how to check on the memory that app1 is using?
|
|
|
|
|
dcof wrote: Can you tell me what would be an 'odd' setup in my environment?
Someone messed up the virtual memory setup (they would need to do this explicitly) or it is being run in a situation, such as a hosted environment, that explicitly limits the memory.
dcof wrote: If app1 is using alot of memory before it makes the call, can you tell me how to check on the memory that app1 is using?
Pretty sure there is a C# api that tells you that. Google for it.
|
|
|
|
|
I'm trying to use the child class method instead of the parent class for some child class cases. For some reason, I get to the parent class method, but it never gets to the override in the child. Any ideas why? I've been referring to http://stackoverflow.com/questions/9067885/calling-child-class-method-from-parent[^]
but it's not getting to the child's method.
In my PC.cs of the child class (Ko):
public partial class PC : GenericPC
{
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
}
In my GenericPC.cs of the parent class:
private int processDynData()
{
ValidateDynData(itsEnteredDynData, ref returnCode);
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
Any ideas why it's not going to the child class implementation of ValidateDynData when I call ValidateDynData in my parent class? This is the only area in my code where I am trying to have a child class override a parent implementation, so I'm not sure if I'm doing something wrong?
|
|
|
|
|
I tried it myself, by pasting your code into an app, and adding a few bits to prevent compilation errors:
private void button2_Click(object sender, EventArgs e)
{
PC pc = new PC();
int i = 4;
pc.ValidateDynData(null, ref i);
}
public partial class PC : GenericPC
{
public override void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Ko PC::ValidateDynData");
}
}
public class GenericPC
{
TextBox[] itsEnteredDynData;
int returnCode;
private int processDynData()
{
ValidateDynData(itsEnteredDynData, ref returnCode);
return returnCode;
}
public virtual void ValidateDynData(TextBox[] tb_dynData_Array, ref int result)
{
Console.WriteLine("Generic::ValidateDynData passing off to child to validate special data");
}
} Ignoring the "not assigned" warning message, when I press the button I get:
Ko PC::ValidateDynData Which is what I would expect - the overloaded version.
Could it be that you are using an instance declared as the base class, rather than the derived?
GenericPC gpc = new GenericPC();
gpc.ValidateDynData(null, ref i);
would give you
Generic::ValidateDynData passing off to child to validate special data
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
It's possible that's the problem. My object instance is created with Reflection:
_assembly = Assembly.LoadFrom(programDll);
_type = _assembly.GetType("CrWriter.PC");
_objectInstance = Activator.CreateInstance(_type);
_parameters = new Object[] { cb1, programDll, templateArr, itsDll, cert, i, cb1.cbInfos[i].boxID };
controls[i] = new Control();
controls[i] = (Control)_type.InvokeMember("GetPC", BindingFlags.Default | BindingFlags.InvokeMethod, null, _objectInstance, _parameters);
controls[i].Dock = DockStyle.None;
this.Controls.Add(controls[i]);
Since my Invocation of GetPC lands in the child class and not the parent, it must be created correctly. So I'm not sure why it's not landing in the correct ValidateDynData method. Maybe I need to cast my object to the programDll somehow. When I run the program and inspect the _objectInstance it could be a problem:
variable.............................................................value
base: {GenericCrWriter.GenericPC} ......CrWriter.PC
baseInst: .........................................................GenericCrWriter.GenericPC
But then, the _assembly is referring to Ko/PC and not Generic/GenericPC.
Also, my _assembly.GetType looks good. My Generic/parent doesn't have anything named CrWriter.PC
I wonder if there is a way to cast an object obtained through reflection?
modified 1-Mar-13 15:56pm.
|
|
|
|
|
MichCl wrote: Since my Invocation of GetPC lands in the child class and not the parent, it must be created correctly. So I'm not sure why it's not landing in the correct ValidateDynData method.
Presuming the former is really true then the possibilities are.
- The child signature of ValidateDynData is wrong
- You are not running the code that you think you are (binary does not match source.)
|
|
|
|
|
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.
|
|
|
|