|
Hear hear!
"If you don't have time to do it right, when will you have time to do it over?"
|
|
|
|
|
It depends on the situation.
1. If it is an all-good-or-not situation, then it deserves a single overall try-catch.
2. If you want to extract as much as possible, then you need very granular error checking (if possible) or try-catching (if no error peeking is possible).
3. And if you can't handle the exception(s), then it falls back to either #1 or #2 anyway.
|
|
|
|
|
mjackson11 wrote: Is there a lot of overhead associated with these?
Not unless there are in fact a lot of exceptions.
mjackson11 wrote: Is there a better way to do this than having a block in each method?
That depends on what the application does and what you do in the block.
If you have a server that runs 24x7 and moves data from one remote source to another then sometimes there will be connection failures. In a case like that then the it must catch exceptions that originate from communication problems and continue.
If it is a console app that runs either pass/fail by a human and they are not supposed to run it unless everything is already working then catching an exception is optional. Maybe friendly but also an extreme case.
If the app needs to handle 3 sequential cases and all must pass then you must handle exceptions so that you can take some sort of action if step 3 fails after the first two succeeded.
When I write servers I catch almost everything so I can log it.
And so on...
|
|
|
|
|
Can you please post a piece of your code in a try catch block?
|
|
|
|
|
I have heard that you can use WMI to check the status of services running on another machine, and even to start/stop them.
Anyone have any experience with this? Exmaples would be great.
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
Try here[^] and here[^].
I have personally been experimenting with WMI recently, myself. Here at work, we have a network set up. However, I cannot seem to get remote access via WMI. I get "Access denied" messages, though I think it is due to domain security policy issues which are out of my hands. I have not tried it at home yet.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Do you have any code you can share?
Everything makes sense in someone's mind
|
|
|
|
|
Hey i have made to forms. form 1 is the rich text file editor and the other is the find and replace.
in form1 i got 1 richtextbox and in form 2 i got 2 normal text boxes. i have changed all the modifier to public.
here the code i am using.
this is opening form2
private void findText_Click(object sender, EventArgs e)
{
Form2 frm2;
frm2 = new Form2();
frm2.ShowDialog();
}
here the code i am trying to use to find but it is not working
<pre lang="c#">RichTextBox frm1TB = ((Form1)this.Owner).txtDisplay;
int foundAt = frm1TB.Text. IndexOf(button1.Text);
hope someone can put me on the right path
|
|
|
|
|
Setting everything to public is bad, Form1 ought to expose some public interface (in the non-technical meaning of that word) that Form2 can use to communicate with it (which should be public or internal), for example a Find(string) method. (Since this is intrinsically a UI level functionality I think it's okay to do it with forms and in the UI code.)
Your immediate problem, I think, is that Form2 has no owner. Try
frm2.ShowDialog(this);
|
|
|
|
|
To further what BobJanova said...
For something similar I wrote last week, my editor control has the methods:
public void NextMatch ( string Pattern )
public void Replace ( string Pattern , string Substitute )
public void ReplaceAll ( string Pattern , string Substitute )
public void ClearMatches ()
|
|
|
|
|
try: Application.Run(new Form2());
- Nevin Janzen (thundercloudstudios.weebly.com)
|
|
|
|
|
You've already gotten lots of good ideas from the previous answers re the issue of 'communication' between the two Forms.
But, I would like to point out the specific error in your code where in Form2 you attempt to use "this.Owner:" at that point, "this" will refer to Form2, but the 'Owner property will be 'null: unless you have set, somewhere in code we do not see here, the 'Owner property of Form2 to Form1.
I am curious, also, if presenting Form2 via 'ShowDialog, so it will retain focus until closed is an absolute requirement for you. If not, I would like to propose another strategy. An alternative strategy where the 'Owner' property can come in real-handy.
Aside: it always bothers me to use 'ShowDialog' because you can move the mouse to some other Form and click something, and that attempt to take Focus away from the ShowDialog'd whatever, makes it do this little "quiver." imho, use of ShowDialog should perform a 'mouse capture" until it's closed
best, Bill
"... Sturgeon's revelation. It came to him that Science Fiction is indeed ninety-percent crud, but that also—Eureka!—ninety-percent of everything is crud. All things—cars, books, cheeses, hairstyles, people and pins are, to the expert and discerning eye, crud, except for the acceptable tithe which we each happen to like." early 1950's quote from Venture Sci-Fi Magazine on the origin of Sturgeon's Law, by author Theodore Sturgeon: source Oxford English Dictionary on-line "Word-of-the-Day."
|
|
|
|
|
Hi
I want to check the value of DataGridView Cell , If the value of Cell Is empy or Null !
Here is My Code :
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (DataGridView1[i, j].Value == null || DataGridView1[i, j].Value.Equals(""))
{
MessageBox.Show("Some Cells in dataGridView Have not any Value");
return ;
}
else
{
tabControl1.TabPages.Add(TabPage2);
tabControl1.TabPages.Remove(TabPage1);
}
}
}
I want to stay(stop) in TabPage1 if Some Value of Cell is Empty(Nul)
But It does not Work Good !
Thanks !
|
|
|
|
|
Well, it should be: DataGridView1.Cells[i,j]
You missed the Cells part. Now as a side note rant, why do you use those kind of variable names?
DataGridiew1, TabControl1 ...
If it's just a test then OK else kind of bad.
All the best,
Dan
|
|
|
|
|
DataGridView1.Cells[i,j]
There is no Property "Cell" for DataGridView !
in any case , i want to say , how can we evaluate Cell Value , when It is Nul Or Empty ?
I want to stop in TabPage1 when Cell Value is Empty !
thanks!
|
|
|
|
|
True, my bad, confused with another grid like control.
dataGridView1.Rows[i].Cells[j] is the correct one. and your original one is ok too.
[edit]
On a closer look the logic is flawed as hell. I just took a quick glips at the code the first time.
Don't activate tabs inside the for loops.
Use a boolean, set it to true if something is empty, then ouside the loops do your thing
such as showing messageboxes, activating tabcontrols or what have you based on that boolean logic.
All the best,
Dan
|
|
|
|
|
Yes , Thanks , But How can we give an Error and Stop In TabPage1?
Regards!
|
|
|
|
|
First, don't add/remove tabs. An easier way is to just activate the needed one.
If you must, you could set the Visible property to false;
bool error=false;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
if (DataGridView1[i, j].Value == null || DataGridView1[i, j].Value.Equals(""))
{
error=true;
break;
}
}
}
if(error){
MessageBox.Show("something");
tabControl1.SelectTab("tabPage1");
}
else{ tabControl1.SelectTab("tabPage2");}
Note that SelectTab() also supports a int as a parameter, e.g. TabCtrl.SelectTab(0);
All the best,
Dan
modified 23-Nov-11 13:44pm.
|
|
|
|
|
|
I am familiar with the AOP program, PostSharp (for post-processing), though I don't use it, and I was wondering if the following scenario is possible at run-time:
1. you adorn certain classes and/or control definitions ... in a "main Form" ... with custom attributes.
2. at run-time, in the 'Load EventHandler of your main Form, or wherever: via reflection, I assume, the current executing "main Form" is parsed, and a new C# file is created on-the-fly which will define a new Form, into which identical copies of Classes and/or Controls ... in the main Form ... will be inserted ... based on the content of the custom attributes.
3. this new Form definition (assembly ?) will then be instantiated/created, and displayed.
4. if we assume that there is a pre-defined static class that enables access to certain Fields, etc. on the main form, will this new dynamically created form be able to access the content of that static class ?
Not looking for solutions, or code, here, just curious if this is "doable."
thanks, Bill
"... Sturgeon's revelation. It came to him that Science Fiction is indeed ninety-percent crud, but that also—Eureka!—ninety-percent of everything is crud. All things—cars, books, cheeses, hairstyles, people and pins are, to the expert and discerning eye, crud, except for the acceptable tithe which we each happen to like." early 1950's quote from Venture Sci-Fi Magazine on the origin of Sturgeon's Law, by author Theodore Sturgeon: source Oxford English Dictionary on-line "Word-of-the-Day."
|
|
|
|
|
This is doable, but would be a fairly complex application to pull together. Off the top of my head, what you would be looking at here is parsing a form using reflection (fairly trivial to accomplish), and pulling out attributes. Now, depending on the status of these attributes, you would probably need to parse the IL if you need to add an identical version in, and this would require you to Emit this into your newly created object (which you could create using the CodeDOM).
All in all, doable, but not trivial to implement.
|
|
|
|
|
Yes, it would be possible, however it seems like sledgehammer-and-nut territory: if you know enough from compile-time decorations to create a new form class, you know enough at coding time. And if you know how to build the code to make a new form, you know how to instantiate the new form and add controls to it at runtime. I don't see the benefit of creating a runtime type over doing that.
|
|
|
|
|
Hi All,
I am calling a c++ dll from c#. it throwing error like "An unhandled exception of type 'System.AccessViolationException' occurred in exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
[DllImport(@"D:\Project\Working\Toolkit-2.0\output\Win32\Debug\ipetk.dll",
EntryPoint = "?GetModuleTypeString@ModuleIndex@Infrastructure@IPETK@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
CallingConvention = CallingConvention.Cdecl)]
static extern string GetModuleTypeString(ModuleType ths);
string ModuleIndex = GetModuleTypeString(ModuleType.IPD);
MessageBox.Show(ModuleIndex.ToString());
how can i resolve this issue..? any help will be appriciated.
Thanks
G.Paulraj
|
|
|
|
|
This one must be in the declaration. What is the return type declared at on the C++ side? Are you sure it is defined as C call convention?
I suspect you need to marshal the string differently. See [MarshalAs][^].
|
|
|
|
|
Looks like the definition of your C function should be changed to something like:
extern "C" char* GetModuleTypeString(char*);
STL string s do not work well between managed and unmanaged code.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|