-
Add Procedure, does it exist in VB.Net?
In VB6.0 there is the Add Procedure wherein one could easily make procedures like Sub, Function, Property or Event, what is its equivalent in VB.Net? I am actually starting to learn VB.Net, I used the Upgrade Wizard to upgrade an app I have made in VB6.0 which consists of only two forms but when it was converted it told me to add a property (IsInitializing or the like, forgot it) because my app from VB6.0 had a code in the Form_Resize event, there was a note/remarks added to the upgraded app that I need to add a property to determine if the app is still loading or something...
This is my first post in VB.Net and its the first time I visited this section. :)
-
Re: Add Procedure, does it exist in VB.Net?
Just type something like:
VB Code:
public property MyProperty as string
and hit ENTER and all the default structure of the property definition will be entered for you by the IDE. As far as I'm aware there are no menu options or shortcuts to do this type of thing. Given that you would have to add arguments and return types yourself anyway, as well as change the name, I'd guess that just typing the definition yourself would be no slower and maybe faster. The IDE does do some things for you, though, like with properties as I've already mentioned. It also adds "End Sub", "End Function" and "ByVal" for you. Intellisense also makes declaring argument types and return types quicker.
-
Re: Add Procedure, does it exist in VB.Net?
I don't think that exists in .Net, rather, type it along the class scope.
public class SomeClass
' ... Some Generated Code
sub SomeSubRoutine()
end sub
function SomeFunction() as SomeType
return SomeTypeVarialbe
end function
end class
I guess, that takes time but worth a doing.
Edit: Opps. jmcilhinney replies faster.
-
Re: Add Procedure, does it exist in VB.Net?
VB6 upgrade wizard, ewe :sick: Good luck using it as your seeing. I would recommend you rewrite the app in .net instead (if time allows).
Usually when you rewrite an app you almost always write it better and more effecient. :)
-
Re: Add Procedure, does it exist in VB.Net?
Wouldn't it a "good" idea to use the upgrade wizard just to discover the equivalent of those vb6.0 functions, methods, events, etc in .Net?
-
Re: Add Procedure, does it exist in VB.Net?
You could possibly do that but when you figure out things out on your own by looking in the help file your more likely to remember
and understand the reasons and definitions of the equilivalent funtions. ;)
Kind of like the Forums. If you ask a question and someone writes the code for you and you copy/paste it into your app you really dont get
the benefit of learning. ;)
-
Re: Add Procedure, does it exist in VB.Net?
I'm not sure, given that I've never used the upgrade wizard, but I'd say VB6 functions will generally be upgraded to the equivalent VB Runtime function, so you would never get the System-based equivalent any way, e.g. MsgBox would be upgraded to MsgBox rather than MessageBox.Show. That's why I say use the Runtime functions for now but explore and be on the lookout for System-based alternatives.
-
Re: Add Procedure, does it exist in VB.Net?
How/where can I find their System-based alternatives? After some reading I think I will not use Microsoft.VisualBasic namespace so as to really code the .Net way, how/where can I disable it?
-
Re: Add Procedure, does it exist in VB.Net?
Quote:
Originally Posted by dee-u
How/where can I find their System-based alternatives? After some reading I think I will not use Microsoft.VisualBasic namespace so as to really code the .Net way, how/where can I disable it?
If you really want to prevent yourself from inadvertently using a Runtime function then you should open the properties dialogue for the project and go to Common Properties -> Imports and remove the Microsoft.VisualBasic entry. As for finding the System-based alternatives, there isn't really a list anywhere that I'm aware of that shows Runtime functions and their System equivalent. I'd not be surprised if some zealot had compiled one somewhere, though. Otherwise, it's just observation, investigation and experience.
-
Re: Add Procedure, does it exist in VB.Net?
So, am I correct to think not the upgrade wizard really just converts one's vb6.0 code to Microsoft.VisualBasic?
-
Re: Add Procedure, does it exist in VB.Net?
Quote:
As for finding the System-based alternatives, there isn't really a list anywhere that I'm aware of that shows Runtime functions and their System equivalent.
MSDN has a very comprehensive list on this, take a look here.
-
Re: Add Procedure, does it exist in VB.Net?
Wow, thanks a lot for that Parallax! :thumb:
-
Re: Add Procedure, does it exist in VB.Net?
Nice link Parallax. I just going to have to read it all now. :)
-
Re: Add Procedure, does it exist in VB.Net?
When I removed the Microsoft.VisualBasic namespace the ff. error occurred and I couldn't figure out what to do...
Name 'Asc' is not declared
Name 'IsNumeric' is not declared
Name 'Chr' is not declared
Name 'vbCrLf' is not declared
Name 'Err' is not declared
Name 'Erl' is not declared
vbNullString also causes an error... Should I replace it with ""?
-
Re: Add Procedure, does it exist in VB.Net?
You need to replace those functions with VB.NET functions.
String.Empty = vbNullString
ControlChars.NewLine = vbCrLf = vbNewLine
ControlChars.CrLf = vbCrLf = vbNewLine
Err and Erl do not exist in .NET - Use a Try Catch block
Etc.
-
Re: Add Procedure, does it exist in VB.Net?
How about these Gangsta Yoda?
Name 'Asc' is not declared
Name 'IsNumeric' is not declared
Name 'Chr' is not declared
-
Re: Add Procedure, does it exist in VB.Net?
I havent see an exact equilivalent for Chr and Asc but this may help?
VB Code:
'Get the first character in the string test
Dim test as string = "Gangsta Yoda!"
Messagebox.show(test.Chars(0).ToString()) '"G"
-
Re: Add Procedure, does it exist in VB.Net?
How about this?
VB Code:
Format$(Date, "mmddyyyy")
This is actually where I need the Asc and Chr, could you help me out convert it to .Net? It capitalizes all alphabets in a textbox...
VB Code:
Private Sub txtData_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
End Sub
-
Re: Add Procedure, does it exist in VB.Net?
Quote:
Originally Posted by dee-u
How about these Gangsta Yoda?
Name 'Asc' is not declared
Name 'IsNumeric' is not declared
Name 'Chr' is not declared
Use Convert.ToInt32 instead of Asc and Convert.ToChar instead of Chr. Although it's a tiny bit less staightforward, use Double.TryParse instead of IsNumeric.
-
Re: Add Procedure, does it exist in VB.Net?
Quote:
Originally Posted by dee-u
How about this?
VB Code:
Format$(Date, "mmddyyyy")
This is actually where I need the Asc and Chr, could you help me out convert it to .Net? It capitalizes all alphabets in a textbox...
VB Code:
Private Sub txtData_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
End Sub
myDate.ToString("MMddyyyy")
Note that as Date in an inbuilt data type, I have changed the variable name to myDate.
KeyAscii = Convert.ToInt32(Char.ToUpper(Convert.ToChar(KeyAscii)))
-
Re: Add Procedure, does it exist in VB.Net?
I just re-read your previous post. Just set the CharacterCasing property of the TextBox to Upper and it's done for you.
-
Re: Add Procedure, does it exist in VB.Net?
But the CharacterCasing will convert the entire string. It needs Proper Casing. Is there one for that?
-
Re: Add Procedure, does it exist in VB.Net?
And how about this code of mine in VB6.0, could it be possibly converted to .Net?
VB Code:
Private Sub PopulateBL()
'<EhHeader>
On Error GoTo PopulateBL_Err
'</EhHeader>
Dim rsRecords As New ADODB.Recordset
100 With rsRecords
102 .Open "SELECT HBL_BLNumber FROM HBL WHERE HBL_Status <> 3", cnn, adOpenDynamic, adLockOptimistic
104 Do While Not .EOF
106 cboBL.AddItem .Fields("HBL_BLNumber")
108 .MoveNext
Loop
110 cboBL.AddItem vbNullString
112 .Close
End With
114 Set rsRecords = Nothing
'<EhFooter>
Exit Sub
PopulateBL_Err:
Select Case ErrorGlobalHandler("AISIS.frmHBL.PopulateBL")
Case vbAbort
Exit Sub
Case vbRetry
Resume
Case vbIgnore
Resume Next
Case Else
MsgBox Err.Description, vbCritical, "Critical Error Encountered"
End
End Select
'</EhFooter>
End Sub
Public Function ErrorGlobalHandler(ByVal strErrorSource As String) As Long
Dim strErrorMessage As String
strErrorMessage = "Error Occured In: " & strErrorSource & vbCrLf & _
"Error Date & Time: " & Now & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Line Number: " & Erl & vbCrLf
'Pass to other subroutine to trap any error that might occur in the ErrorGlobalHandler
ErrorGlobalHandler = PromptAndLogError(strErrorMessage, App.Path)
End Function
Public Function PromptAndLogError(ByVal ErrorMessage As String, ByVal SystemPath As String, Optional ShowPrompt As Boolean = True) As Long
On Error Resume Next
Dim fso As New FileSystemObject, tsm As TextStream, ErrorFile As String
ErrorFile = SystemPath & "\ErrorLog" & Format$(Date, "mmddyyyy") & ".txt"
If fso.FileExists(ErrorFile) = True Then
Set tsm = fso.OpenTextFile(ErrorFile, ForAppending)
tsm.Write String$(30, "-") & vbCrLf & ErrorMessage
tsm.Close
Set tsm = Nothing
Set fso = Nothing
Else
Set tsm = fso.CreateTextFile(ErrorFile, True)
tsm.Write ErrorMessage
tsm.Close
Set tsm = Nothing
Set fso = Nothing
End If
If ShowPrompt = True Then
PromptAndLogError = MsgBox(ErrorMessage & vbCrLf & "Please report to the programmer any persistent error. ", vbCritical + vbAbortRetryIgnore, "Error Encountered")
End If
End Function
-
Re: Add Procedure, does it exist in VB.Net?
Proper casing.
VB Code:
Dim test As String = "gangsta Yoda"
test = test.ToUpper.Chars(0) & test.Substring(1)
MessageBox.Show(test) '"Gangsta Yoda"
-
1 Attachment(s)
Re: Add Procedure, does it exist in VB.Net?
You should try to use ADO.NET instead of ADODB.
You can show line numbers in the IDS automatically by clicking:
Tools > Options > Text Editor > All Languages > General > Display > check "Line Numbers".
It will also show you what line number your cursor is on in the status bar at the bottom of the IDE on the right side. ;)
-
Re: Add Procedure, does it exist in VB.Net?
Its not the Ado that I'm concerned with, its the error handling scheme? :) Could it be converted to .Net? Anybody who could help me out converting it?
-
Re: Add Procedure, does it exist in VB.Net?
But dee-u said ALL letters. If you want proper casing then there isn't a property of the TextBox that can do it, but the TextInfo class has a ToTitleCase method that you can use on the whole string, e.g.:
VB Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim newText As String = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Me.TextBox1.Text)
If Me.TextBox1.Text <> newText Then
Dim cursorPosition As Integer = Me.TextBox1.SelectionStart
Me.TextBox1.Text = newText
Me.TextBox1.SelectionStart = cursorPosition
End If
End Sub
-
Re: Add Procedure, does it exist in VB.Net?
Ok, take the
"On Error Resume Next"
"On Error GoTo PopulateBL_Err"
PopulateBL_Err:
and replace the handling with a Try Catch block.
VB Code:
Try
'Do you stuff that has a potential for errors
Catch ex As Exception
MessageBox.Show(ex.Source & " - " & ex.Message)
End Try
-
Re: Add Procedure, does it exist in VB.Net?
How about the functions ErrorGlobalHandler and PromptAndLogError? Couldn't
I log the errors anymore and pinpoint what specific line the error occurred?
How does the new error handling in .Net works now? No more resume next and resume? How will it go back to the line where the error occurred or skip any line?
-
Re: Add Procedure, does it exist in VB.Net?
Use ex.StackTrace to get the line number that threw the exception.
You would also remove that too. You can wrap a global Try Catch block if your starting your app from a module or doing Application.Run(Form1).
VB Code:
Public Module1
Public oForm1 As New Form1
Public Sub Main()
try
Applicaation.Run(oForm1)
catch ex as exception
MessageBox.Show(ex.Source & " - " & ex.Message, ex.StackTrace)
end Try
End Sub
End module
-
Re: Add Procedure, does it exist in VB.Net?
:confused:
How does that work? How about if I'll have Try... Catch in my procedures? Will it still go to that global Catch?
-
Re: Add Procedure, does it exist in VB.Net?
It works a similar way as it did in VB6. If you have nested error handling then it will buble up the path of Try Catches until it
gets to the last one.
-
Re: Add Procedure, does it exist in VB.Net?
Say you have a Try Catch in your Form1 from my example. Then Form1 generates an error thats inside form1's Try Catch block.
It will go to the forms Try Catch and display a messagebox (if you place one in there). Tne it will go to the Try Catch in the module and
display the messagebox message of the error and its source and stacktrace. ;)
-
Re: Add Procedure, does it exist in VB.Net?
How about this?
Quote:
Originally Posted by dee-u
How does the new error handling in .Net works now? No more resume next and resume? How will it go back to the line where the error occurred or skip any line?
In VB6.0 I really never used Nested Error handling. :(
-
Re: Add Procedure, does it exist in VB.Net?
There may be a way to have it resume but its not something I have tried yet. I was just reading about it today as a matter of fact. :D
When you catch the exception you can reset it (I think is how it was done) but if it errors again your out of luck.
I'll see if I can find the article on MS but I have not seen any examples from MS where they do this. Usually its just handle the error.
-
Re: Add Procedure, does it exist in VB.Net?
Thanks a lot Rob. :) But would you believe I got a neg. rep for this thread? :cry:
-
Re: Add Procedure, does it exist in VB.Net?
I found the srticle at MS. Its actually a good read to learn more on error handling in .NET :thumb:
http://msdn.microsoft.com/library/de...tml/vb01f1.asp
Its compliments of kfcSmitty in my error thread here.
:eek: Really? Maybe they made a mistake or dont understand whats going on in this thread?
-
Re: Add Procedure, does it exist in VB.Net?
Double.TryParse and ControlChars raises some errors! Why is it? Did I miss something?
-
Re: Add Procedure, does it exist in VB.Net?
Sorry DU, looks like the ControlChars is also in the MS.VB namespace. Since you removed the reference that is perhaps why
your getting the error. :(
-
Re: Add Procedure, does it exist in VB.Net?
ControlChars is indeed a member of a Microsoft.VisualBasic module. You can replace all the ControlChars constants, though. Use Environment.NewLine instead of ControlChars.NewLine, Convert.ToChar(Keys.Tab) instead of ControlChars.Tab, etc. Double.TryParse should give you no issues, though, as long as you give it the correct arguments.