|
HOW DO I PASS ARRAYS TO METHODS?
|
|
|
|
|
Hi,
you mean like this:
public void MyCaller() {
string[] aMyArray = ...
this.MyMethod(aMyArray);
}
public void MyMethod(string[] aArray) {
...
}
Does this help?
Regards
Sebastian
P.S.: By the way, I would suggest using a List or sth. similar, because these offer much more functions...
|
|
|
|
|
an array is an object, hence just like any other object.
String.Join() is an example.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
"7. Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job."
public void MyMethod(int[] myArray)
{
...
}
|
|
|
|
|
Hi all,
I have found this some unusual problem while working on the debug and release build. I have two forms MainForm and ModelForm. Following is the code for these respective forms .cs files
Code for MainForm.cs :
namespace ModelDialogExceptions
{
public partial class MainForm : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ModelDialog mdl = new ModelDialog();
mdl.ShowDialog();
}
catch (Exception exc)
{
MessageBox.Show("Exception captured!!!");
}
}
}
}
And code for ModelForm.cs :
namespace ModelDialogExceptions
{
public partial class ModelDialog : Form
{
public ModelDialog()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
throw new System.IO.FileNotFoundException("");
}
}
}
If I run this project in debug build, message box gets poped up however, in the Release (when i run output .exe in) build an unhandeled exception is raised.
Any body know, why this code is behaving differently for different builds?
or any comments about this!!
|
|
|
|
|
You can add a top level exception handler, which will catch exceptions from other threads. Why you have this difference, I don't know.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
I guess top level exception handler wont be useful as application is going to shut down anyway!!
it will be just more or less equals to saying "good bye"!!
some additional info:
actually this difference is not related to debug or release(sorry for previous info- i just got this difference)
instead it is in between output .exe (both in debug and release) and visual studio code,when i run from the visusal studio i gets the message box
|
|
|
|
|
1. debug and release EXE files are stored in different folders
2. your "current directory" initially is set to the folder containing the EXE
3. you forgot to copy the file your app wants from your debug folder to your release folder, so the debug version finds it, the release version does not.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I don't read his code that he's looking for any file at all, he's just playing with exceptions. But, if he failed to post most of his code, then you're probably right. A try/catch that then throws a FileNotFoundException is a bit retarded, tho
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
no guys!
i am not at all playing with files!
that was just for demonstration
instead of throwing FileNotFoundException I can throw any Exception
|
|
|
|
|
That's what I thought
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
The code you showed works perfectly fine, so you either omitted something or you have a problem with your VS and/or .NET framework, in which case you should re-install it all.
Unless there's a problem with your CPU or your Win... I really think you omitted something.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
have you tried running .exe file generated in debug/release directory under bin directory.
if you tried then i will have to follow what you are saying otherwise please try running the .exe and let me know the resust
thanks in advance!
|
|
|
|
|
Sorry I think I didn't make myself clear: what I mean is it's perfectly normal that the application, when run directly (i.e. running the exe file) crashes. But this will happen for both Debug and Release!
If you run the app inside the VS debugger, on the contrary, then it will behave differently: both Debug and Release versions will catch the exception and show the dialog box.
This happens because exception handling is slightly different while running the debugger.
Just to be sure I made the experiment myself just now, and I confirm what I said.
In order to capture the exception you must use a local try/catch block in the ModelDialog form.
There is no way I know of handling the exception the way you want to. I suggest your change your approach. Consider this (just a raw example):
namespace ModelDialogExceptions
{
public partial class ModelDialog : Form
{
public ModelDialog()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
throw new System.IO.FileNotFoundException("");
DialogResult = DialogResult.OK;
}
catch (Exception)
{
DialogResult = DialogResult.Abort;
}
}
internal static bool GetMyFile()
{
ModelDialog md = new ModelDialog();
switch (md.ShowDialog())
{
case DialogResult.OK:
return true;
case DialogResult.Abort:
throw new ApplicationException("Operation aborted, an exception may have been thrown.");
case DialogResult.Cancel:
throw new ApplicationException("Cancelled by user.");
default:
throw new ApplicationException("Unknown error.");
}
}
}
}
namespace ModelDialogExceptions
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (ModelDialog.GetMyFile())
{
MessageBox.Show("All ok!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
thanks for such wonderful reply!!
You know what I did the same (Because didnt had any other alternative)
I thought I could share you with you all guys this unusual behavior, might be somebody knowing the exactly why this happens?
anyways thanks for reply!
|
|
|
|
|
As I said, it happens because of the debugger. The debugger contains an exception handler which "watches" for exceptions while running the code. When an exception is caught, it will walk up not only the function call stack (which is what the runtime does when you run the exe), but the program flow - searching for a try/catch block which can catch the exception. If it's not found, then the debugger's handler kicks in (it's the piece of code that shows you the exception message box and lets you point at the code which raised it).
Now, calling ShowDialog() is not like calling any other function. A normal function call will just transfer the current thread's execution to the function's code, saving the return address on the stack, and return to the calling code when the function exits. This allows for the raised exception to be passed directly up to the calling code and get caught by the upper level try/catch block.
What happens when you call ShowDialog() is that the new form is created and shown, and the UI's message pump (remember it all runs on a single thread, but it's message-based) starts managing it together with all other open forms, with the only exception that other forms will be "frozen". When the dialog closes and returns its result, this is passed back to the calling code, and execution flow returns there, but in this case exceptions do not cross the boundaries of forms, since there's no "bridging" code for that to happen.
This is the best explanation I can give, I hope it clears your doubts. Maybe some other guy can give a more complete or deeper explanation.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Yeh Moreno Airoldi !!!!
that was great info !!!
thanks a ton for such nice explanation!!
|
|
|
|
|
My pleasure.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Hi falks!
I need to parse a text file including an NC code which is a simple CNC tool motion determinator. In the file, each line shows the instantaneous positon of the tool tip. For example, the first line is X0 Y0 Z0 F200 and second line X10 Y10 Z10 this means that the tool is at (0,0,0) and will go to (10,10,10) with a speed of 200 m/min. This motion will take some time. Depending on the machine tool specificaion (user will enter), according to sampling time entered by user (0.1 second for example) succesive points will be determined between (0,0,0) and (10,10,10) one point in each 0.1 second. I have to parse the text file and give a list as an output showing the position of the tool at eah multiples of the sampling time. A sample NC code is:
G1X0Y0Z0F200
X10.Y10.Z10.
X21.Y16.Z8.
Y10.Z5.
G1 means linear motion. I will use only linear motion. Blanks between the characters are not important. However, to specify X position of tool as 10 it should be written as "X10." or "X10.0". X10 means 10*sampling time (e.g 10*0,1=1). Any caharacter which is not writen in a line means that it is the same as the last written form in earlier lineas. My output will be like
X Y Z
0
0,1 1 1 1
0,2 2 2 2
0,3 . . .
. . . .
.
.
I need help!
|
|
|
|
|
What is the issue ? String parsing ? You can use regex for this, or just use methods on the string class.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
|
How is 'hi' a valid subject line ?
Use a delegate to communicate between forms
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
A couple of things:
1) Read the bit at the top of the page entitled: "How to get an answer".
2) Describe your problem in the subject. "Hi" and "Hello guys" do not describe your problem.
3) Explain your problem as clearly as you can.
4) Tell us what you have tried.
and very definately:
5) DO NOT WAIT THREE MINUTES AND THEN POST THE SAME PROBLEM AGAIN IN WORSE ENGLISH
As all you will do is annoy people.
Thank you for your attention.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Same with "Hello Guys"..huh..??
|
|
|
|
|
You are right.
I think he don't know how to ask a question
|
|
|
|