Solved

Insert counter in Try/ Catch block

Posted on 2004-10-18
9
323 Views
Last Modified: 2012-06-22
Problem:  I want to add only unique records to my Process table.  the data that is being read can have multiples.  I think I am getting close with the following code, but the compiler is telling me that "y" in my catch is an "unassigned local variable".  If the ProcessID of my first instance is 1, then I would want the return line to return "1" for the ProcessID for each of the subsequent duplicates (necessary for proper child handling).

Thanks

**Code**
private int addProcess(string process, int locationID)
{
      DataRow row = data.Tables["Process"].NewRow();
      row["Name"] = process;
      row["LocationID"] = locationID;
      row["GUID"] = System.Guid.NewGuid();
      int y;
      int z;
try {
      y = 0;
      data.Tables["Process"].Rows.Add(row);
      return (int) row["ProcessID"];
}
      
catch {
      z = y++;
      return (int) row["ProcessID"]- z;
}
}

0
Comment
Question by:ImageryGrl
9 Comments
 
LVL 10

Expert Comment

by:smegghead
ID: 12343173
Just move the y=0 line to before the try block.. There is no need to assign this in the try block
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12344473
Dont seem to have any use of variable y and z
change the code to this

private int addProcess(string process, int locationID)
{
     DataRow row = data.Tables["Process"].NewRow();
     row["Name"] = process;
     row["LocationID"] = locationID;
     row["GUID"] = System.Guid.NewGuid();

try {
     data.Tables["Process"].Rows.Add(row);
     return (int) row["ProcessID"];
}
     
catch {
         return (int) row["ProcessID"]- 1;
}
}
0
 

Author Comment

by:ImageryGrl
ID: 12348765
This only works if there is only one duplicate.  Example: Original row["ProcessID"] statement  = 0, the first duplicate would be 0 - which is correct.  However if there was a third duplicate it would show row["ProcessID"] = 1 (2-1), not 0. What I want is for the errors to increment so that the "-z" in my catch statement would be -1 for the first, then -2 for the second, etc.  
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Expert Comment

by:armoghan
ID: 12354539
You mean something like this
int y=0;
int z=0;

while(true)
{
try {
     data.Tables["Process"].Rows.Add(row);
     return (int) row["ProcessID"]-z;
}
     
catch {
     z = y++;
}
}
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12354548
sorry this

int y=0;
int z=0;

data.Tables["Process"].Rows.Add(row);

while(true)
{
try {
        int abc = (int) row["ProcessID"]-z;
        return abc;
}
     
catch {
     z = y++;
}
}
0
 

Author Comment

by:ImageryGrl
ID: 12372987
Still not quite.  the Try needs to work on the Add(row) as well as the return.  This one is the best yet, but the z keeps getting reset to 0 and I need it to keep the counter from the last round.  


int y=0;
int z=0;

while(true)
{
   try
   {data.Tables["Process"].Rows.Add(row);
   int processID = (int) row["ProcessID"];
   return processID;

   }
   catch
   {
   z = ++y;
   int processID = (int) row["ProcessID"]-z;
   return processID;

   }
   }
0
 

Author Comment

by:ImageryGrl
ID: 12375377
When I re-read all the logic of the program, the loop will not work.  I have decided to go another direction:  Instead of trying to use a counter to get the correct processID, I am simply re-pulling it from the table based on the current record.  This is what the new Catch looks like:

catch
{
      String filterExpression = "Name = '{0}'";
      String filter = String.Format(filterExpression, risk);
      DataRow[] datarows = data.Tables["Process"].Select( filter );
      riskID = (int)datarows[0]["ProcessID"];
      return processID;
}

Many thanks to armoghan and smegghead for trying to assist.  I will request the question be closed.
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12411766
The question has been PAQ'd and the 125 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 49
Printing 1 51
Can I command line build a C# application? 6 33
Need help with a Crystal Report in my C#.Net application 2 16
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question