Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert counter in Try/ Catch block

Posted on 2004-10-18
9
Medium Priority
?
332 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
8 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Integration Management Part 2
Loops Section Overview
Suggested Courses

782 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