Solved

Insert counter in Try/ Catch block

Posted on 2004-10-18
9
329 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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