When can I use the GoTo instruction in VB.Net?

I have a process that should create a bunch of records in the database and drop everything if something goes wrong. I want to do the following:

Public Structure Result
    Public Success as Boolean
    Public Message as String
End Structure

Private _Repository as IEntityRepository

Public Function SaveOrganization( _
    ByVal organization As rv_o_Organization) As Result
    Dim result = Result.Empty

    _Repository.Connection.Open()
    _Repository.Transaction = _Repository.Connection.BeginTransaction()

    ''//Performs validation then saves it to the database
    ''// using the current transaction
    result = SaveMasterOrganization(organization.MasterOrganization)
    If (Not result.Success) Then
        GoTo somethingBadHappenedButNotAnException
    End If

    ''//Performs validation then saves it to the database
    ''//using the current transaction
    result = SaveOrganziation(dbOrg, organization)
    If (Not result.Success) Then GoTo somethingBadHappenedButNotAnException

somethingBadHappenedButNotAnException:
    _Repository.Transaction.Commit()
    _Repository.Connection.Close()
    Return result
End Sub

Is it ok to use a GoTo instruction or just a bad design? Is there a more elegant solution? I hope this sample can get a point in

+3
source share
16 answers

If you need to ask, do not do this.

For your specific code, you can do it like this:

Public Function SaveOrganization(ByVal organization As rv_o_Organization) As Result
    Dim result As Result = Result.Empty

    _Repository.Connection.Open()
    _Repository.Transaction = _Repository.Connection.BeginTransaction()

    'Performs validation then saves it to the database 
    'using the current transaction
    result = SaveMasterOrganization(organization.MasterOrganization)

    'Performs validation then saves it to the database 
    'using the current transaction
    If result.Success Then result = SaveOrganziation(dbOrg, organization)

    _Repository.Transaction.Commit()
    _Repository.Connection.Close()
    Return result
End Sub
+13
source

Really bad design. Yes.

+6
source

Goto , . , goto - , .

.

+6

, , , , .

Using, . , , IDisposable ( , ), Dispose. (, ).

, TransactionScope, , , .

+2

, goto, - .

, , - .

try... , , (, #)

void DoStuff()
{
    Connection connection = new Connection();
    try
    {
        connection.Open()
        if( SomethingBadHappened )
            return;
    }
    finally
    {
        connection.Close();
    }    
}
+2

. , GOTO, . , , vb On Error Goto.

+1

goto , . , .

. , , REAL, .

+1

Gotos - . / goto ( !) while ( ) gotos, . - - ( - )

, . , . , , .

, . , , , - , - , , , .

, , goto (ps. psuedocode -):

dbMethod() {
    start transaction
    if(doWriteWorks())
        end Transaction success
    else
        rollback transaction
}
doWriteWorks() {
    validate crap
    try Write crap
    if Fail
        return false
    validate other crap
    try Write other crap
    if Fail
        return false
    return true
}

, VB, VB 3 ( , MS ), , - -, . , MS , ...

+1

goto , , Try Catch, , "Retry?, Cancel", , StartMyTask: .

.

For each Items in MyList

 If VaidationCheck1(Item) = false then goto SkipLine
 If ValidationCheck(Item) = false then goto skipline

 'Do some logic here, that can be avoided by skipping it to get better performance.
 'I use then like short circuit operands, why evaluate more than you actually have to?

 SkipLine:
Next

, , , , -.

+1

, goto, . , "",

0

, , , . , 20 , Goto , .

0

catch try, , , . , GOTO.

, GOTO , , , , , . , , BASIC.

0

, , . .

, , GOTO select, #, VB.

0

go . , , 6 " ".

go to , , . , try... catch , , go tos.

, , : -)

go - , , . clean_up: .

0

Eh, VBscript/ASP . ASP , .

in.net? , !

0

, . GOTO - - .

, , - . GOTO , .

, , , , .NET.

0
source

Source: https://habr.com/ru/post/1703532/


All Articles