I’m still a novice programmer(from C++) and I am creating a GIU which displays a Tag from a running software. The purpose is to display the tag properties Description and Eng Units . I have been given 2 dlls InTouchDataAccess and NDde.
I’ve read on exception handeling and the best idea I’ve seen was this one:
Create a Validate function
which I did. But the program does no enter the function, I goes straigh into the catch block of my SelectButton_Click fct.
TagBrowser.cs
using System;
using System.Windows.Forms;
using IOM.InTouchDataAccess;
namespace InTouchTagBrowser
{
public partial class InTouchTagBrowser : Form
{
public string tagName;
public string description;
public string engUnits;
public InTouchTagBrowser()
{
InitializeComponent();
}
private void TagBrowser_Load(object sender, EventArgs e)
{
}
private void SelectButton_Click(object sender, EventArgs e)
{
try
{
tagName = tagNameBox.Text;
InTouchDdeWrapper inTouchWrapper = new InTouchDdeWrapper();
inTouchWrapper.Initialize();
TagDotField tagDotField = new TagDotField(tagName);
string value = inTouchWrapper.Read(tagName);
if (EngValidate(inTouchWrapper.Read(tagDotField.EngUnits)) != 0)
{
engUnits = inTouchWrapper.Read(tagDotField.EngUnits);
}
else
{
engUnits = "N/A";
}
if (inTouchWrapper.Read(tagDotField.Description) != "")
{
description = inTouchWrapper.Read(tagDotField.Description);
}
else
{
description = "N/A";
}
descriptionlbl.Text = description;
englbl.Text = engUnits;
valuelbl.Text = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.Source);
MessageBox.Show(ex.HelpLink);
MessageBox.Show(ex.StackTrace);
}
}
private void WriteButton_Click(object sender, EventArgs e)
{
try
{
if (tagName == "")
{
MessageBox.Show("Please enter a tag!");
}
else
{
string inputValue = ValueBox.Text;
InTouchDdeWrapper inTouchWrapperWriter = new InTouchDdeWrapper();
inTouchWrapperWriter.Initialize();
TagDotField tagWriter = new TagDotField(inputValue);
inTouchWrapperWriter.Write(tagName, inputValue);
valuelbl.Text = inputValue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Tag change successfull");
}
}
public int EngValidate(string engString)
{
string exception;
int x;
try
{
InTouchDdeWrapper inTouchWrapper = new InTouchDdeWrapper();
inTouchWrapper.Initialize();
TagDotField tagDotField = new TagDotField(tagName);
engString = inTouchWrapper.Read(tagDotField.EngUnits);
x = 1;
}
catch (Exception msg)
{
exception = msg.ToString();
if (exception == "")
x = 1;
else
x = 0;
}
return x;
}
}
}
You’re declaring a variable
engStringfor your method which you don’t actually use. Additionally, the way you set this variable reads from the wrapper which can cause an exception (which is then catched by the outer catch block). A better approach would be to declare such a method reading only once:Called like this:
I hope you get the idea of this pattern. You instantiate (and initialize) a lot of classes over and over again in your code where it would be better to just have one wrapper, one field, etc … and to read the values only once. Additionally, you shouldn’t make use of return codes in C#, that’s what exceptions are for.