Resolved Another issue from a beginner C# programmer.

C# learner

Active member
Joined
Dec 22, 2022
Messages
40
Programming Experience
Beginner
I created a method that uses a value parameter to be used to find a record in a file and out parameters. I learned how to get the out parameters initialized, but having trouble with the first parameter, idIndividualID. I don't want to null the field because the value is used in the if statement to find the correct record. Why is the field highlighted in red? Error says " The name idIndividualID does not exist in the current context".
C#:
 static void FindIndRecord (string idIndividualID, // field in question
                                          out string idSurname,
                                          out string idGivenName,
                                          out string idBirthDate,
                                          out string idBirthPlace,
                                          out string idMarriageDate,
                                          out string idMarriagePlace,
                                          out string idDeathDate,
                                          out string idDeathPlace,
                                          out string idSex,
                                          out string idChildCode,
                                          out string idParentCode)
                             
                {
                   
                    idSurname = null;
                    idGivenName = null;
                    idBirthDate = null;
                    idBirthPlace = null;
                    idMarriageDate = null;
                    idMarriagePlace = null;
                    idDeathDate = null;
                    idDeathPlace = null;
                    idSex = null;
                    idChildCode = null;
                    idParentCode = null;
                }

                {   
                    // While control variable
                    string recordLine;
                   

                    string path = (@"C:\Users\steve\OneDrive\Documents\Donna's Documents\Steves stuff\Family Tree Development Folder\GEDCOM Individual Data.csv");
                    using StreamReader id = new StreamReader(path, true);
                    {
                        while ((recordLine = id.ReadLine()) != null)
                        {
                           
                            // using the split method to parse "line" into lineParts array
                            string[] lineParts = recordLine.Split(',');

                            //string trimIndividualID = null;
                             if (lineParts[1] = idIndividualID  //field in question
                            {
                                string idSurname = (lineParts[2] != null && lineParts[2] != " ") ? lineParts[2] : " ";
                                string idGivenName = (lineParts[3] != null && lineParts[3] != " ") ? lineParts[3] : " ";
                                string idBirthDate = (lineParts[4] != null && lineParts[4] != " ") ? lineParts[4] : " ";
                                string idBirthPlace = (lineParts[5] != null && lineParts[5] != " ") ? lineParts[5] : " ";
                                string idMarriageDate = (lineParts[6] != null && lineParts[6] != " ") ? lineParts[6] : " ";
                                string idMarriagePlace = (lineParts[7] != null && lineParts[7] != " ") ? lineParts[7] : " ";
                                string idDeathDate = (lineParts[8] != null && lineParts[8] != " ") ? lineParts[8] : " ";
                                string idDeathPlace = (lineParts[9] != null && lineParts[9] != " ") ? lineParts[9] : " ";
                                string idSex = (lineParts[10] != null && lineParts[10] != " ") ? lineParts[10] : " ";
                                string idChildCode = (lineParts[11] != null && lineParts[11] != " ") ? lineParts[11] : " ";
                                string idParentCode = (lineParts[12] != null && lineParts[12] != " ") ? lineParts[12] : " ";

                                Array.Clear(lineParts, 0, lineParts.Length);
                                return;
                            }
                           
                        }
                    }
                }
 
In the future, please put your code in code tags while posting. Failing to do so causes all the indentation formatting to not be visible. I've put your code in code tags for you.
 
Anyway, the answer is because your method ends at line 27, but you are trying to use the parameter out in line 44. If you used indentation properly on your code (and didn't rely on syntax coloring) the problem would be more obvious.
 
The code tag button looks like the </> on the toolbar above the textarea where you type in your post.
 
Anyway, here's your code reformatted to use more consistent indentation (as well as fix the missing closing parenthesis on line 41):
C#:
 static void FindIndRecord (string idIndividualID, // field in question
                            out string idSurname,
                            out string idGivenName,
                            out string idBirthDate,
                            out string idBirthPlace,
                            out string idMarriageDate,
                            out string idMarriagePlace,
                            out string idDeathDate,
                            out string idDeathPlace,
                            out string idSex,
                            out string idChildCode,
                            out string idParentCode)
{
   
    idSurname = null;
    idGivenName = null;
    idBirthDate = null;
    idBirthPlace = null;
    idMarriageDate = null;
    idMarriagePlace = null;
    idDeathDate = null;
    idDeathPlace = null;
    idSex = null;
    idChildCode = null;
    idParentCode = null;
}
{  
    // While control variable
    string recordLine;
 
    string path = (@"C:\Users\steve\OneDrive\Documents\Donna's Documents\Steves stuff\Family Tree Development Folder\GEDCOM Individual Data.csv");
    using StreamReader id = new StreamReader(path, true);
    {
        while ((recordLine = id.ReadLine()) != null)
        {
            // using the split method to parse "line" into lineParts array
            string[] lineParts = recordLine.Split(',');
            //string trimIndividualID = null;
            if (lineParts[1] = idIndividualID)  //field in question
            {
                string idSurname = (lineParts[2] != null && lineParts[2] != " ") ? lineParts[2] : " ";
                string idGivenName = (lineParts[3] != null && lineParts[3] != " ") ? lineParts[3] : " ";
                string idBirthDate = (lineParts[4] != null && lineParts[4] != " ") ? lineParts[4] : " ";
                string idBirthPlace = (lineParts[5] != null && lineParts[5] != " ") ? lineParts[5] : " ";
                string idMarriageDate = (lineParts[6] != null && lineParts[6] != " ") ? lineParts[6] : " ";
                string idMarriagePlace = (lineParts[7] != null && lineParts[7] != " ") ? lineParts[7] : " ";
                string idDeathDate = (lineParts[8] != null && lineParts[8] != " ") ? lineParts[8] : " ";
                string idDeathPlace = (lineParts[9] != null && lineParts[9] != " ") ? lineParts[9] : " ";
                string idSex = (lineParts[10] != null && lineParts[10] != " ") ? lineParts[10] : " ";
                string idChildCode = (lineParts[11] != null && lineParts[11] != " ") ? lineParts[11] : " ";
                string idParentCode = (lineParts[12] != null && lineParts[12] != " ") ? lineParts[12] : " ";
                Array.Clear(lineParts, 0, lineParts.Length);
                return;
            }
        }
    }
}
 
I went back and double checked the indentation and corrected any errors. Still getting the same error and is underlining the "idIndividualID" field in red. Could it be a problem with the call to the method?
 
Yes, because as I said your method is only actually just lines 1-26 of post number #6. The scope of a parameter is only within a method. If you want your method to be all of lines 1-57, you need to delete lines 26 and 27.

Or in other words you currently have something that looks like
C#:
void DoSomething(string input)
{
   Console.WriteLine(input);
}
{
    Console.WriteLine(input);
}

input on line 3 refers to the parameter input on line 1. The compiler won't know what to do with input on line 6.

To fix things you'll want:
C#:
void DoSomething(string input)
{
    Console.WriteLine(input);
    Console.WriteLine(input);
}

Until you get more C# experience, I highly recommend not using the top level statements feature of C#.
 
Yes, because as I said your method is only actually just lines 1-26 of post number #6. The scope of a parameter is only within a method. If you want your method to be all of lines 1-57, you need to delete lines 26 and 27.

Or in other words you currently have something that looks like
C#:
void DoSomething(string input)
{
   Console.WriteLine(input);
}
{
    Console.WriteLine(input);
}

input on line 3 refers to the parameter input on line 1. The compiler won't know what to do with input on line 6.

To fix things you'll want:
C#:
void DoSomething(string input)
{
    Console.WriteLine(input);
    Console.WriteLine(input);
}

Until you get more C# experience, I highly recommend not using the top level statements feature of C#.

I took those lines out and the "idIndividualID" is not red underlined anymore. Now all the if statement out variables are red underlined. Let's see if I can do the code tag properly:



FindIndRecord:
 static void FindIndRecord(string idIndividualID,
                                          out string idSurname,
                                          out string idGivenName,
                                          out string idBirthDate,
                                          out string idBirthPlace,
                                          out string idMarriageDate,
                                          out string idMarriagePlace,
                                          out string idDeathDate,
                                          out string idDeathPlace,
                                          out string idSex,
                                          out string idChildCode,
                                          out string idParentCode)
                {

                    idSurname = null;
                    idGivenName = null;
                    idBirthDate = null;
                    idBirthPlace = null;
                    idMarriageDate = null;
                    idMarriagePlace = null;
                    idDeathDate = null;
                    idDeathPlace = null;
                    idSex = null;
                    idChildCode = null;
                    idParentCode = null;
              
                    // While control variable
                    string recordLine;


                    string path = (@"C:\Users\steve\OneDrive\Documents\Donna's Documents\Steves stuff\Family Tree Development Folder\GEDCOM Individual Data.csv");
                    using StreamReader id = new StreamReader(path, true);
                    {
                        while ((recordLine = id.ReadLine()) != null)
                        {

                            // using the split method to parse "line" into lineParts array
                            string[] lineParts = recordLine.Split(',');

                            //string trimIndividualID = null;
                            if (lineParts[1] == idIndividualID)
                            {
                                string idSurname = (lineParts[2] != null && lineParts[2] != " ") ? lineParts[2] : " ";
                                string idGivenName = (lineParts[3] != null && lineParts[3] != " ") ? lineParts[3] : " ";
                                string idBirthDate = (lineParts[4] != null && lineParts[4] != " ") ? lineParts[4] : " ";
                                string idBirthPlace = (lineParts[5] != null && lineParts[5] != " ") ? lineParts[5] : " ";
                                string idMarriageDate = (lineParts[6] != null && lineParts[6] != " ") ? lineParts[6] : " ";
                                string idMarriagePlace = (lineParts[7] != null && lineParts[7] != " ") ? lineParts[7] : " ";
                                string idDeathDate = (lineParts[8] != null && lineParts[8] != " ") ? lineParts[8] : " ";
                                string idDeathPlace = (lineParts[9] != null && lineParts[9] != " ") ? lineParts[9] : " ";
                                string idSex = (lineParts[10] != null && lineParts[10] != " ") ? lineParts[10] : " ";
                                string idChildCode = (lineParts[11] != null && lineParts[11] != " ") ? lineParts[11] : " ";
                                string idParentCode = (lineParts[12] != null && lineParts[12] != " ") ? lineParts[12] : " ";

                                Array.Clear(lineParts, 0, lineParts.Length);
                                return;
                            }
                        }
                    }
                }
 
Last edited by a moderator:
I took those lines out and the "idIndividualID" is not red underlined anymore. Now all the if statement out variables are red underlined. Let's see if I can do the code tag properly:



[CODE lang="csharp" title="FindIndRecord" static void FindIndRecord(string idIndividualID,
out string idSurname,
out string idGivenName,
out string idBirthDate,
out string idBirthPlace,
out string idMarriageDate,
out string idMarriagePlace,
out string idDeathDate,
out string idDeathPlace,
out string idSex,
out string idChildCode,
out string idParentCode)
{

idSurname = null;
idGivenName = null;
idBirthDate = null;
idBirthPlace = null;
idMarriageDate = null;
idMarriagePlace = null;
idDeathDate = null;
idDeathPlace = null;
idSex = null;
idChildCode = null;
idParentCode = null;

// While control variable
string recordLine;


string path = (@"C:\Users\steve\OneDrive\Documents\Donna's Documents\Steves stuff\Family Tree Development Folder\GEDCOM Individual Data.csv");
using StreamReader id = new StreamReader(path, true);
{
while ((recordLine = id.ReadLine()) != null)
{

// using the split method to parse "line" into lineParts array
string[] lineParts = recordLine.Split(',');

//string trimIndividualID = null;
if (lineParts[1] == idIndividualID)
{
string idSurname = (lineParts[2] != null && lineParts[2] != " ") ? lineParts[2] : " ";
string idGivenName = (lineParts[3] != null && lineParts[3] != " ") ? lineParts[3] : " ";
string idBirthDate = (lineParts[4] != null && lineParts[4] != " ") ? lineParts[4] : " ";
string idBirthPlace = (lineParts[5] != null && lineParts[5] != " ") ? lineParts[5] : " ";
string idMarriageDate = (lineParts[6] != null && lineParts[6] != " ") ? lineParts[6] : " ";
string idMarriagePlace = (lineParts[7] != null && lineParts[7] != " ") ? lineParts[7] : " ";
string idDeathDate = (lineParts[8] != null && lineParts[8] != " ") ? lineParts[8] : " ";
string idDeathPlace = (lineParts[9] != null && lineParts[9] != " ") ? lineParts[9] : " ";
string idSex = (lineParts[10] != null && lineParts[10] != " ") ? lineParts[10] : " ";
string idChildCode = (lineParts[11] != null && lineParts[11] != " ") ? lineParts[11] : " ";
string idParentCode = (lineParts[12] != null && lineParts[12] != " ") ? lineParts[12] : " ";

Array.Clear(lineParts, 0, lineParts.Length);
return;
}
}
}
}[/CODE]
Lol, nope...but you get an A for effort...lol
 
Back
Top Bottom