how to find the end of an 'If/End If' block like C# (vs2005)?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to find the end of an If block in VB2005 -- similar to C#
where in C# if you place your cursor next to a bracket and press ctrl
something it highlights the ending bracket? I have some fairly long If / End
If blocks and have a hard time finding the end End If. How to do this?

Thanks,
Rich
 
Is it possible to find the end of an If block in VB2005 -- similar to C#
where in C# if you place your cursor next to a bracket and press ctrl
something it highlights the ending bracket? I have some fairly long If / End
If blocks and have a hard time finding the end End If. How to do this?

Thanks,
Rich

No, I don't believe so.

Personally, I would say if you have a if..then block that is that long
(or that deeply nested) you need to re-design it's functionality. I
would venture a guess that it would be a maintenance nightmare and the
cost of the rewrite would be much less than the cost of maintaining
that code. Is there any reason you can't simplify the logic, or even
wrap the various parts in separate methods?

Thanks,

Seth Rowe
 
Example:

If some condition is true then
Dim da As New SqlDataAdapter,...
da.SelectCommand = New SqlCommand
da.InsertCommand = New SqlCommand
...
da.InsertCommand.Parameters.Add(...)
...
da.InsertCommand.Parameters.Add(...)
....
If conn.State = ConnectionState.Closed Then conn.Open
...
da.SelectCommand.ExecuteNonQuery
...
da.Update(ds, "someTbl")
...
End If
...
If conn.State = ConnectionState.Open Then conn.Close
....
End If

I supposed I could write a bunch of little subs and functions to handle the
stuff, but that would be more stuff to maintain and bounce around when I have
to debug or update something. Some of these If blocks might have 50 lines
which isn't that bad except when you have a bunch of nested if statements.
Well, I guess I can use bookmarks.
 
Rich said:
Is it possible to find the end of an If block in VB2005 -- similar to C#
where in C# if you place your cursor next to a bracket and press ctrl
something it highlights the ending bracket? I have some fairly long If /
End
If blocks and have a hard time finding the end End If. How to do this?

Thanks,
Rich

Have a look at a product called Refactor Pro from DevExpress. It graphicaly
connects all related blocks of code (as well as many other features). I find
this product indispensible
 
Example:

If some condition is true then
Dim da As New SqlDataAdapter,...
da.SelectCommand = New SqlCommand
da.InsertCommand = New SqlCommand
...
da.InsertCommand.Parameters.Add(...)
...
da.InsertCommand.Parameters.Add(...)
....
If conn.State = ConnectionState.Closed Then conn.Open
...
da.SelectCommand.ExecuteNonQuery
...
da.Update(ds, "someTbl")
...
End If
...
If conn.State = ConnectionState.Open Then conn.Close
...
End If

I supposed I could write a bunch of little subs and functions to handle the
stuff, but that would be more stuff to maintain and bounce around when I have
to debug or update something. Some of these If blocks might have 50 lines
which isn't that bad except when you have a bunch of nested if statements.
Well, I guess I can use bookmarks.







- Show quoted text -

Actually, looking at your code I can make three suggestions. First,
don't trust the ConnectionState property, it only returns what it
*thinks* it's state is. It may return that it is Open, and then throw
an exception because it's closed whenever you use it. I highly
recommend you ditch the if...then test and just use a try catch block
to make sure every succeeds. Secondly, I would remove the "If
conn.State = ConnectionState.Open Then conn.Close" block and wrap the
whole sucker in a using block. Then you can ensure that it will
properly close itself and release any non-managed resources. Lastly,
the other Db object should also be disposed of when you are finished
with them. I would recommend you use a Using block, but you could also
use a try...finally structure too. Disposing is a hotly debated topic,
but in my opinion is what you should do to ensure smooth and
performant execution of your code.

Thanks,

Seth Rowe
 
rowe_newsgroups said:
Actually, looking at your code I can make three suggestions. First,
don't trust the ConnectionState property, it only returns what it
*thinks* it's state is. It may return that it is Open, and then throw
an exception because it's closed whenever you use it. I highly
recommend you ditch the if...then test and just use a try catch block
to make sure every succeeds. Secondly, I would remove the "If
conn.State = ConnectionState.Open Then conn.Close" block and wrap the
whole sucker in a using block. Then you can ensure that it will
properly close itself and release any non-managed resources. Lastly,
the other Db object should also be disposed of when you are finished
with them. I would recommend you use a Using block, but you could also
use a try...finally structure too. Disposing is a hotly debated topic,
but in my opinion is what you should do to ensure smooth and
performant execution of your code.

Thanks,

Seth Rowe
Well stated, Seth. The "Using" block is a great friend. Clean and efficient.
In interviewing five different developers for a position in our development
team, none were familiar with this construct. For those who are interested,
even in a "Try/Catch" block, when an error occurs, the "End Using" statement
is always executed (like a "Finally" statement). The only caveat to using a
"Using" block, is that the object needs to implement IDsposable.

For the newbies (like me?) IDisposable is eay to implement. Just put the
line "Implements IDisposable" at the beginning of your class and press
enter. The necessary code is added to your class automatically.

e.g.

Using myObject as New FixEverything(DB_Connect_String, pointer)
With myObject
.NewDataA = "Bloody beautiful"
.Save 'call the save method
End With
End Using

You can also nest "Using"
 
Agreed,

If any method has much more than a screenfull of code it needs to be looked
at...

Guy
 
You can also nest "Using"

As well as list two, or more, IDisposable objects in one statement:

/////////
Using conn As new SqlConnection, com As SqlCommand =
conn.CreateCommand()

End Using
/////////

When you have multiple IDisposable objects the above provides a bit
"cleaner" way (imo) for using Using statements (pun intended). Not to
mention it prevents having to scroll sideways to view a deeply nested
Using block.

Thanks,

Seth Rowe
 
Rich,

I go in VB.Net mostly much deeper in blocks then in C#.

I never needed any tool to find the end of an If, a for loop or whatever in
VB.Net, something that I have often trouble with in C# while busy.

The way as the C# syntax is build and (although better now) the lack of
automatic build in features, make in my idea this kind of all not to
remember gadgets in C# needed.

If it is in VB.Net then I probably would never use it as I seldom use
gadgets. I never have problems to find the end of such a block in VB.Net
even if it is 20 or more deep.

And as you can see, I am against all that building of one time used private
methods, in my idea does it make programs less readable.

Cor
 
Back
Top