Y
Y
Yuri Denisov2016-04-19 08:50:36
C++ / C#
Yuri Denisov, 2016-04-19 08:50:36

Why is variable not visible outside if in c#?

Good day to all!
There was a problem with the visibility of the variable.
To unserialize an array serialized using PHP, I use the PHPSerializationLibrary library.
Here is the beginning where I connect it

public partial class mainForm : Form
{
   public Conversive.PHPSerializationLibrary.Serializer serializer;
   public mainForm()
        {
            InitializeComponent();
            serializer = new Conversive.PHPSerializationLibrary.Serializer();
        }
}

Then I take an array and throw it to this library like this:
if (cardsForPrint.Rows[k][20].ToString() == "L")
   {
       Hashtable ht = (Hashtable)serializer.Deserialize(LParams);
   }

This piece of code is in the button click event. Then I need to take the result and work with it, outside the conditions, i.e. after if else I write:
newPrintTemplate = newPrintTemplate.Replace("$v67", krest(Convert.ToInt32((string)ht[1])));

And here he does not see the ht variable, if you put this code inside if, he sees it. Outside it is not.
Help the guys out, I've been struggling for several days already :)
PS When I take out the declaration of a variable outside if, then they stop working. Those. I write Hashtable ht = new Hashtable(); above the conditions, an empty hash table is seen after the conditions.
PPS Comments prompted and earned in the end like this:
Hashtable ht = new Hashtable();
if (cardsForPrint.Rows[k][20].ToString() == "L")
{
ht = (Hashtable)serializer.Deserialize(LParams);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2016-04-19
@denissov

It's strange that a few days. Learn what the scope of a variable is professorweb.ru/my/csharp/charp_theory/level3/3_5.php.
To fix the problem, just move the variable declaration outside the if

S
Sergey, 2016-04-19
@sergey_kzn

You need to read about variable scope. For example here:
metanit.com/sharp/tutorial/2.18.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question