Learning C #, trying to write a method to check for the existence of a certain text, declared the nameIsOK variable and try to assign a value to it, but I get a message that the returned variable does not have a value assigned to me. How do I fix the code?

public bool CheckTheSectionName(string sectionName) { var doc = Application.DocumentManager.MdiActiveDocument; var db = doc.Database; var ed = doc.Editor; bool nameIsOk; using ( var tr = db.TransactionManager.StartTransaction() ) { var model = (BlockTableRecord) tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); foreach ( ObjectId id in model ) { if ( id.ObjectClass.DxfName == "TEXT" ) { var text = (DBText) tr.GetObject(id, OpenMode.ForRead); if ( text.TextString == sectionName ) { nameIsOk = true; } } else if ( id.ObjectClass.DxfName == "MTEXT" ) { { var mtext = (MText) tr.GetObject(id, OpenMode.ForWrite); if ( mtext.Text == sectionName ) { nameIsOk = true; } } } else { nameIsOk = false; } } tr.Commit(); } return nameIsOk; } 

    1 answer 1

    You have 2 options:

    The first is: Assign the initial value to the variable nameIsOk (during initialization) .

     bool isNameOk = false; 

    And the second option is to assign a value if mtext.Text != sectionName

     //... if ( mtext.Text == sectionName ) { nameIsOk = true; } //... 

    Add another block else . if the condition is not fulfilled here ( mtext.Text != sectionName ) , then the nameIsOk variable will remain virtually empty. Yes, she will be given a standard value for this type (we have it false ). But the studio makes a warning in order to avoid mistakes. So, choose one of the more appropriate options for your method. I will leave the code for the first option:

     public bool CheckTheSectionName(string sectionName) { var doc = Application.DocumentManager.MdiActiveDocument; var db = doc.Database; var ed = doc.Editor; bool nameIsOk = default(bool); //or bool nameIsOk = default; for C# 7.1 using ( var tr = db.TransactionManager.StartTransaction() ) { var model = (BlockTableRecord) tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); foreach ( ObjectId id in model ) { if ( id.ObjectClass.DxfName == "TEXT" ) { var text = (DBText) tr.GetObject(id, OpenMode.ForRead); if ( text.TextString == sectionName ) nameIsOk = true; } else if ( id.ObjectClass.DxfName == "MTEXT" ) { var mtext = (MText) tr.GetObject(id, OpenMode.ForWrite); if ( mtext.Text == sectionName ) { nameIsOk = true; } } else nameIsOk = false; } tr.Commit(); } return nameIsOk; //If mtext != sectionName method will return false. } 
    • And by the way, you can make a try/finally block inside using , and in the finally part put the tr.Commit(); - this will seriously reduce the code, and in this case, in the right places, immediately organize the output return true/false - NewView
    • @NewView yes, of course you can, but the question is different. I explained why this error appears. And in the blocks themselves, I am also confused, so your editing can be accepted. - Aqua
    • No, no, we will leave yours, it is essentially correct, but the author’s code is too cumbersome for such a task, too many unnecessary variables, IMHO of course - NewView
    • @NewView I think you're right =) - Aqua 8:59 pm