Does not exist in class or namespace??

A

Assimalyst

Hi,

I declare an object, surgeonRow within an if statement:

if(addNewSurgeonChkBx.Checked == true)
{
DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToString();
dsAddAssessment.Tables["Surgeon"].Rows.Add(surgeonRow);
}

however when i try to use it again like this:

if(addNewSurgeonChkBx.Checked == true)
{
assessmentRow.SetParentRow(surgeonRow);
}

surgeonRow is highlighted and the build error "The name 'surgeonRow'
does not exist in the class or namespace" is given.

is there a way to reference it? I cant move the

DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

line out of the if statement to overcome this error, as it produces
more errors.

Thanks.
 
J

Johann Blake

surgeonRow is declared inside an "if" statement and therefore is local
to that statement. Declare it outside of the "if" block at the top of
your method or the top of your class.

Best Regards
Johann Blake
 
A

Assimalyst

I have tried this but it creates many more problems if i do. i may have
to play around the the code to overcome those instead.

Thanks
 
G

Guest

The problem is that variables declared in the scope of an if are disposed of
when the if finishes. Declare the variable before the if

as

DataRow surgeonRow

and then in the if just assign to it as

surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

the value will then be available throght out the method.

Or if you declare the variable at class level through out the class
 
J

Johann Blake

No you're doing it wrong. You don't move the entire line, just the
declaration, like this...


DataRow surgeonRow;

if(addNewSurgeonChkBx.Checked == true)
{
surgeonRow = dsAddAssessment.Tables["Surgeo­n"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToStr­ing();
dsAddAssessment.Tables["Surgeo­n"].Rows.Add(surgeonRow);
}

Best Regards
Johann Blake
 
P

Pohihihi

Basically what Johann suggested is what is usually done and is the best
solution. Work around could be that you copy surgeonRow datarow to a global
datarow object and refer that in your later code. If your datarow is
returning lots of rows then it is very much you will consume double resource
to hold same data.

Just curious, why you can not declare surgeonRow out side of your if block?
 
P

Pohihihi

if the second use of that value is not outside of that method then you can
still use it via using() statement. that way it will dispose after that use
and still is not shown all over class or even inside the method.


tony lock said:
The problem is that variables declared in the scope of an if are disposed
of
when the if finishes. Declare the variable before the if

as

DataRow surgeonRow

and then in the if just assign to it as

surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

the value will then be available throght out the method.

Or if you declare the variable at class level through out the class


Assimalyst said:
Hi,

I declare an object, surgeonRow within an if statement:

if(addNewSurgeonChkBx.Checked == true)
{
DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();
surgeonRow["sgnTitle"] = titleCboBx.SelectedValue;
surgeonRow["sgnFName"] = fNameTxtBx.Text;
surgeonRow["sgnLName"] = lNameTxtBx.Text;
surgeonRow["estNo"] = Session["establishment"].ToString();
dsAddAssessment.Tables["Surgeon"].Rows.Add(surgeonRow);
}

however when i try to use it again like this:

if(addNewSurgeonChkBx.Checked == true)
{
assessmentRow.SetParentRow(surgeonRow);
}

surgeonRow is highlighted and the build error "The name 'surgeonRow'
does not exist in the class or namespace" is given.

is there a way to reference it? I cant move the

DataRow surgeonRow = dsAddAssessment.Tables["Surgeon"].NewRow();

line out of the if statement to overcome this error, as it produces
more errors.

Thanks.
 
A

Assimalyst

I can declare it as Johann suggested, never thought of that, but can't
declare it as i was doing initially, it was creating a new row where i
wouldn't necessarily need one. generating an " Object reference not set
to an instance of an object" error. I only need to add data to table
Surgeon sometimes, if the user doesn't select an existing surgeon form
a list.

Johanns and Tony's suggestion generates an 'Unassigned local variable
'surgeonRow' build error on the
assessmentRow.SetParentRow(sur­geonRow); line.

I need a way to pass the row from the if statement to the
assessmentRow.SetParentRow(sur­geonRow); section of code. How would I
go about copying the complete surgeonRow to a global object? Perhaps
this would do the trick

Thanks.
 
P

Pohihihi

as per your other posting (that you do not want to show it as class value
and also limit view in method), please see my reply for that.

for copy, you can still copy row by row, slow but works. not a very good way
to do if speed is needed.

but if you are ok to copy then you should be able to use it as suggested by
others. To avoid that error check if your object is null or not before
SetParentRow(sur­geonRow);

if(myobj != null)
SetParentRow(sur­geonRow);

--
Po


I can declare it as Johann suggested, never thought of that, but can't
declare it as i was doing initially, it was creating a new row where i
wouldn't necessarily need one. generating an " Object reference not set
to an instance of an object" error. I only need to add data to table
Surgeon sometimes, if the user doesn't select an existing surgeon form
a list.

Johanns and Tony's suggestion generates an 'Unassigned local variable
'surgeonRow' build error on the
assessmentRow.SetParentRow(sur­geonRow); line.

I need a way to pass the row from the if statement to the
assessmentRow.SetParentRow(sur­geonRow); section of code. How would I
go about copying the complete surgeonRow to a global object? Perhaps
this would do the trick

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top