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.
Hi, Im doing a conversion to an old calculation program at work. It has 1000+ input variables, which I have grouped in 15 or so classes, which I declare arrays of to simulate a database. Then I have a function that reads or writes these to disc.
The passing of the array seems to change the array to a system.array instead of the original <whateverclass>
Private Function ReadDataClassAllFieldsToDisk(ByRef CheckObject() As Object) As Boolean
So, Question 1: Why does the CheckObject I get in the function throw an error when I finish with the function saying that the objects are not of the same type?
Question 2: Can i automatically add a new object of the same type as the otherones in the array? (know I have a select thingy to check the type and make a new instance and add to the array?)
Thanks for just reading this, I hope anyone can help?
Supposing that you are using vb.net, and not knowing the rest of the code in the function, the error might not point to the objects in your array.
Generally speaking a function will work like so:
Private Function MyStringAlittleShorter(ByVal MyInputString As String, Byval Divider as integer) As String
Return MyInputString.Substring(1, MyInputString.Length / Divider)
...because I am returning the same type as I declared my function to be (even though one of the Byvals is of a different type).
Now your Byval is an array of type Object, which means that it can contain elements of different types. If you are also trying to return an array, and your function is declared boolean, you would get a type mismatch error. I.e. declare the function to be an array of type object, or perform the logic inside the function, and return a boolean.
Maybe that is the case ?
As for your second question: if the array is of type Object, than you should be able to add new elements of any type. You might get Index out of bounds errors ofcourse.
Hope it helps,
My advice is free, and you may get what you paid for.
i have developed an application in vb.net and mssql 2000 and create some reports using crystal reports. it is working perfectly on my pc but create some problem on my client's pc.
i used an ODBC Connection Say CrRptOdbc while creating Crystal reports.
normaly i have to create an odbc connection on the Client's pc when i install my application.
in this particular case my client has MS SQLExpress 2005. when i created a SETUP i attached the database file in it. But when i install the same on the client's pc other than Crystal Reports rest is working perfectly.
when i try to fetch a report it show me a dialog box which demand user id and password when i always used Window's Authitications. i tried to create ODBC connection but could not find the file which i attached.
kinly help me how can i configure my crystal reports in this case.
Last Visit: 31-Dec-99 19:00 Last Update: 25-Feb-21 13:04