-1

I am facing a problem related to variable scope please suggest me a good solution for this problem.

Error: temp dose not exist in current context.

        if (cause_list_type_fk == 1)
        {
            Regularcause temp = (Regularcause) obj;
        }
        else if (cause_list_type_fk == 2)
        {
            urgentcauselist temp = (urgentcauselist) obj;
        }
        else 
        {
            supplementryTable temp = (supplementryTable)obj;
        }


            //
            // loging info to a file
            //
            using (StreamWriter w = System.IO.File.AppendText(@"D:\log-entriesSupply.txt"))
            {
                string tmp = "-->Hearing id: " + hearing_id + "-->bench id: " + bench_id + "-->pary id: " + party_id + "\n";
                Log(tmp +  "-->case_id= " + case_id + "-->= " + temp.caseno + "-->temp.Id= " + temp.Id, w);
                w.Close();
            }
wasipeer
  • 1,015
  • 11
  • 23
  • Which `temp` are you wanting to use? What have you researched? – Sayse Mar 05 '15 at 09:42
  • What **type** is your variable `temp`? It is not clear and is also defined in the `if` block, not visible outside the block – user3021830 Mar 05 '15 at 09:42
  • i have to declare temp by checking the value of cause_list_type_fk. Is there any way to declare a variable in such a way ? – wasipeer Mar 05 '15 at 09:45
  • Yes, you have done. But do all 3 types have a `caseno`? do they all inherit from the same type? What have you researched? – Sayse Mar 05 '15 at 09:47
  • You can inherit `supplementryTable`, `urgentcauselist` and `Regularcause` from the same interface if it is appropriate for you. – user3021830 Mar 05 '15 at 09:48
  • No all three types are different. but following code remain same for all the types that`s y i am trying to use one name and don`t want to replicate the code. – wasipeer Mar 05 '15 at 09:49
  • I believe you are talking about compile time error. for that declare object before your if condition. & then use as object temp; a better way would be to use a common interface for the 3 objects – Abdul Rehman Sayed Mar 05 '15 at 09:52
  • No, they are not all different. As @Andy stteted in his answer you havecommon properties and can put them in an interface which you will inherit your classes from. – user3021830 Mar 05 '15 at 09:53
  • these classes are auto generated by .net entity data model so if i implement interface is there any affect on the project ? ? – wasipeer Mar 05 '15 at 09:58

1 Answers1

2

First of all: you need to declare temp variable outside of if block, otherwise it will not be visible outside that block.

Second: from your code it looks like you're trying to use fields caseno and Id of your temp, but in your if-else conditions you're assigning objects of three different types Regularcause, urgentcauselist and supplementryTable to the same variable. Probably all of these types contains such a fields, but you can't do this in the manner you're trying to do.

Possible solution: create some interface, say

public interface IMyInterface
{
    object caseno, Id
}

Of course, it should contain your actual field names and types. It is just a sample.

Make your classes Regularcause, urgentcauselist and supplementryTable implementing this interface.

And then declare your temp variable like

IMyInterface temp = obj as IMyInterface;

instead of your if-else block.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • +1. Also, OP could just make a `Write(IMyInterface temp)` method that will remove the need to create the temp variable at all – Sayse Mar 05 '15 at 09:53
  • 1
    these classes are auto generated by .net entity data model so if i implement interface is there any affect on the project ? ? – wasipeer Mar 05 '15 at 09:55
  • @wasipeer Are these generated classes `partial` ones? If yes - then you can add your own part to each class (in a separate file) and specify implementation of interface in this part. This will not affect the rest of generated class. – Andrey Korneyev Mar 05 '15 at 10:01