Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have a method with a string argument. I want to call that method in an event but when I try to pass the argument, it tells me that the variable is not present in the context.

Please help.

Regards
Aman Chaurasia

What I have tried:

The method(function):

private void GetZoneTag(string ZoneArea)
        {
            ZoneCmbZoneTag.Items.Clear();
            if (ListView1.Items.Count > 1)
            {
                for (var iCount = 0; iCount < ListView1.Items.Count; iCount++)
                {
                    if (ListView1.Items[iCount].SubItems[1].Text == ZoneArea)
                    {
                        ZoneCmbZoneTag.Items.Add(ListView1.Items[iCount].SubItems[0].Text);
                    }
                }
            }
        }


The event:

private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GetZoneTag(ZoneArea);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Posted
Updated 24-May-18 20:28pm

When you call a method, you pass it a value that it receives in the variable it declares in it's method header:
C#
private void GetZoneTag(string ZoneArea)
Inside the method, the value is called "ZoneArea" but that variable does not exist outside the method itself. To call it, you pass it an actual value:
C#
GetZoneTag("Hello world");
Or
C#
string zoneName = "Hello World";
...
GetZOneTag(zoneName);
Whee you get the value from, I don;t know - I have no access to the rest of your code!
 
Share this answer
 
Comments
Primo Chalice 25-May-18 2:22am    
I am now getting this error 'InvalidArgument=Value of '0' is not valid for 'index', at line String str = ListView1.SelectedItems[0].Text, when I am clicking on the ListView
OriginalGriff 25-May-18 2:30am    
Then use the debugger and find out what is in the collection - particularly since the code is not what you originally showed...
I can't see your screen, I can't access your HDD - and I don't have any access to your data! :laugh:
private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
String str = listView1.SelectedItems[0].Text;
try
{
GetZoneTag(str);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Here u are passing a value which is a Zone but Zone is not Present in ListView and u need to fetch that value first and after pass it to the function, u don't have
declaration or initilization for the zone therefore it giving a error " the variable is not present in the context " try it.
 
Share this answer
 
Comments
Primo Chalice 25-May-18 2:22am    
I am now getting this error 'InvalidArgument=Value of '0' is not valid for 'index', at line String str = ListView1.SelectedItems[0].Text, when I am clicking on the ListView
rivate void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
String str = "";


if (listView1.SelectedItems.Count > 0)
{
if (listView1.SelectedItems[0].Tag != null)
{
str = listView1.SelectedItems[0].Tag.ToString();
GetZoneTag(str);
}
else
{
str = "";
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900