Solved

Insert counter in Try/ Catch block

Posted on 2004-10-18
9
321 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:armoghan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The question has been PAQ'd and the 125 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now