Somebody correct me if I'm wrong but is 'exit sub' generally regarded as 'dirty' programming?
Yes, it's generally preferred that a method should have only one exit point, so that the code is easier to follow.
You should however consider if adding more exit points makes the code significantly simpler. Sometimes the robustness of the code stands in conflict with the elegance of the code, and it's robustness that makes the code work well.
Despite everything, the person most likely to be fooling you next is yourself.
The Return statement is used in Function to return the any value that is retrieve in function and return the control on calling function.
and then Exit Sub statement is used in the subroutine to exit from current routine.
exit sub return the control on last line in our routine(on End Sub statement)
May be this post is useful for you.
When used within a function, the return statement allows a value to be specified; that value will be given to the calling program.
Within function 'foo', for example, one could replace:
foo = 5
The Return statement may also be used without an argument, in which case it will behave like 'Exit Sub'. Earlier versions of VB required "Exit Sub", while other languages use "Return". Generally, I prefer Return, though that may be a matter of style. Note that in prior versions of VB, Return was a valid statement, but it meant something very different from in vb.net.
As for whether performing a 'Return' in the middle of a function constitutes good structured programming, I would suggest that there are cases where it is appropriate and places where it is not. If a sub or function will sometimes do nothing, it's better to use an 'early abort' test and return statement than to enclose the entire routine in a giant 'if'. On the other hand, a Return statement in the middle of a routine that generally runs to the end can sometimes cause confusion.
The biggest thing to watch out for, historically, has been with routines that begin some action that needs to be cleaned up. An early-exit test which skips the routine before it does anything requiring cleanup poses no problem, but returns in the middle can pose a problem. If the routine has allocated any resources, they need to be deallocated before the routine exits. This has typically been handled by putting some cleanup code before every Return or Exit statement. Such an approach could frequently lead to bugs, if cleanup requirements changed but not all the cleanup code was adjusted.
In a modern language like vb.net, structures like Try/Finally blocks and Using statements can alleviate some of the cleanup issues associated with mid-function exits. On the other hand, they can also cause code to be executed in confusing sequence. If a 'Return' or 'Exit Sub' is performed within a Try/Finally block, the 'Finally' clause will be executed, but code following the block will not.
Hello all, I found a problem with my database connection. It prompout 2 error message,the following are the 2 message prompout:
1. An unhandled exception of type 'System.ArgumentException' occurred in mysql.data.dll
2. An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module. Additional information: The type initializer for "POS_.Module1" threw an exception.
Public ObjMyComm As New MySqlCommand
Public ObjMyRead As MySqlDataReader
Public MyConn As MySqlConnection = New MySqlConnection(Connect())
Public strSetKey As String
Public ADD_BINLOG As String = "ADD_BINLOG"
Public Conn As String
Public Conn_localServer As String
Public Conn_localDb As String
Public Conn_localDbUID As String
Public Conn_localDbPassword As String
Public Conn_localDbPort As String
Public Function Connect()
'Data load from App.Config file
Conn_localDb = ConfigurationSettings.AppSettings("MyLocalDb")
Conn_localServer = ConfigurationSettings.AppSettings("MyLocalServer")
Conn_localDbUID = ConfigurationSettings.AppSettings("MyLocalUID")
Conn_localDbPassword = ConfigurationSettings.AppSettings("MyLocalPass")
Conn_localDbPort = ConfigurationSettings.AppSettings("MyLocalPort")
Conn = "DATABASE=" & Conn_localDb & ";DSN=" & Conn_localDb & ";OPTION=3;PORT=" & Conn_localDbPort & ";SERVER=" & Conn_localServer & ";UID=" & Conn_localDbUID & ";"
Public Function LoadBlnStartupData(ByVal strSetKey As String)
Dim test As String
MessageBox.Show("**** = " & strSetKey & " " & TerminalLocate & " " & TerminalID & " ")
'ObjMyComm = New MySqlCommand("SELECT * FROM pos_setting WHERE LocationCode=' " & TerminalLocate & " ' AND SettingKey='ADD_BINLOG' ", MyConn)
ObjMyComm = New MySqlCommand("SELECT * FROM pos_setting", MyConn)
ObjMyRead = ObjMyComm.ExecuteReader
If ObjMyRead.Read Then
test = ObjMyRead.Item(0)
MessageBox.Show("This is " & test & "")
Catch ex As Exception
The 1st Error are pointing to > Public MyConn As MySqlConnection = New MySqlConnection(Connect())
Could anyone give me some solution about this matter?
I doesn't found any problem with my connection string as I have create a udl file to check if I get the right connection but it shows me the connection string is correct. I'm using VS 2003 .NET. Is that problem cause by this version of Visual Studio? I heard ppl said that is alot of problem in this version of Visual Studio. Anyone have other suggestion for this solution?
A picture box is often mistaken for a high end control. It's there for people who are incapable of writing some basic drawing code. I'd create my own control, have it draw the image in it's paint event, then it's trivial to have it draw the image to the bounds of the control, and not hard to make it draw as big as it can with the correct aspect ratio.
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
I have build an application which needs to write log files in user specified folder during settings with in the application + it need to connect with Access and update the records. When my application runs on XP it works fine but when It runs on Vista it do not get the default credentials and used to have readonly rights on the machine.So write log fails and while updating access it does not allow me.
I can check the default rights using system.security.principle class but how can set the default credentials for application as the user who run's the application do have rights to read/write/delete on machine.