Solved

Insert counter in Try/ Catch block

Posted on 2004-10-18
9
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

734 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