-
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.
-
Re: Add Procedure, does it exist in VB.Net?
Thats where I seen it before - Environment. Thanks John, I suggested the wrong one. I hate it when that are so many
different ways to call the same funciton. :(
-
Re: Add Procedure, does it exist in VB.Net?
Ok I'll try those Environment functions when I'm home.
Double.TryParse works if the argument passed is numeric but it errors when the passed argument is non-numeric, I just want to verify if a certain data is numeric or not, could Double.TryParse do that of is there any other way?
Another query I have is this, can't the picturebox group controls anymore? There is this control (GroupBox) which replaced it already? In vb6.0 I always tend to use picturebox to group my controls since it has the 3d effect...
-
Re: Add Procedure, does it exist in VB.Net?
Use Double.TryParse like this:
VB Code:
Dim stringToParse As String
Dim parsedResult As Double
If Double.TryParse(stringToParse, Globalization.NumberStyles.Any, Nothing, parsedResult) Then
MessageBox.Show("The string is a number. It's value is " & parsedResult.ToString() & ".")
Else
MessageBox.Show("The string is not a number.")
End If
You can alter the second and third arguments to modify the behaviour. The function returns False if the string is not a number of the specified format. If the string is a number, the function returns True and the numerical value is contained in the fourth argument.
-
Re: Add Procedure, does it exist in VB.Net?
Use a PictureBox only to display an image. Use a Panel if you want to group controls without the GroupBox's frame. The Panel has a BorderStyle property. I know that you are just learning dee-u, but I think you need to read and experiment a little more before posting every little question. It absolutely was not me, but I'd guess that that is why someone else repped you down for this thread.
-
Re: Add Procedure, does it exist in VB.Net?
Thats more like it, instead of giving me negative reps I would appreciate an advice instead of giving me negative rep. :)
I am actually reading some tutorials/articles about migrating from vb6.0 to vb.net but it seems those things that I need are not tackled and searching the MSDN does not provide any help also hence here I am asking here instead. :( I know its good to experiment and discover things on your own but I want to be properly guided on the .Net way without using the Microsoft.VisualBasic namespace, with it it seems I almost do lots of stuff in VB.Net with my knowledge in VB6.0 but I really want to learn VB.Net without MS.VB namespace... There are really lots of things that confuses me in learning this VB.Net and proper guidance from you experts in this language is all I'm longing. :)
Whats more problematic for me is those examples I run into uses MS.VB namespace! :(
-
Re: Add Procedure, does it exist in VB.Net?
Quote:
Originally Posted by dee-u
Whats more problematic for me is those examples I run into uses MS.VB namespace! :(
That's because using Microsoft.VisualBasic IS using proper VB.NET. You have fallen into the same trap that I did and have started thinking of using VB Runtime functions as somehow wrong. It is not. If you want to avoid those Runtime functions to learn the System-based alternatives then by all means do so, but be aware that they are just that: alternatives. Many people will write library functions for themselves that wrap up multiple lines of VB.NET code into a one-line function call. That is all the VB Runtime functions do. People say that they are bad because they "are VB6 code", but they are not. You cannot write VB6 code in VB.NET. The help documentation is littered with examples using VB Runtime functions because Microsoft put them there to be used.
-
Re: Add Procedure, does it exist in VB.Net?
Using Microsoft.VisualBasic I could use IsNumeric, will it be just fine then? Will it not be "VB6.0" code? And if I gain ample knowledge in VB.Net using MS.VB will I now be frowned upon if ever I'll apply for jobs requiring VB.Net skill?
-
Re: Add Procedure, does it exist in VB.Net?
Just like here, that depends on the individual. I give you a 100%, iron-clad guarantee that using IsNumeric is not writing VB6 code. There are people around this forum who will still frown at you for using it though, just like there will be people in the work-force, including prospective employers, who will frown at you. I'd say your time now would be best spent learning the big stuff, like how do I access data using VB.NET, etc. There's no good going to an employer with an alternative to IsNumeric if you don't know what a DataAdapter is. The small stuff will come to you over time, with research and experience, just as it does for everyone. If you see some code on this forum or elsewhere that you don't recognise or understand, look it up in the help. That's what I do and it is one of the best ways to learn.
-
Re: Add Procedure, does it exist in VB.Net?
When I was learning VB6.0 that was my approach before, I didn't care if my code is not optimized or not the best way to do it as long as I could do the same thing with its equivalent optimized code, since I am still learning VB.Net I wish I could already start at the "proper" way...
Will it really be fine investing time using MS.VB at this point in time? In VB6.0 there were things you could do to optimize your code, in VB.Net what should I know if my code is the "best/optimized" way to do it?
And isnt it that VB.Net is still in the process of evolving? What would the future hold for those programmers who still uses MS.VB?
-
Re: Add Procedure, does it exist in VB.Net?
I've been using VB.NET and C# for 18 months without ever having coded in VB6, and I'm still learning new ways to optimise my code, as I'm sure everyone else on this forum is. Just because you don't use Runtime functions doesn't mean that your code is optimised. Just like in everything, you need to decide how to spend your time most productively. If you want to proceed without Runtime functions then by all means do so, but be aware that any time you spend looking for alternatives is time not spent doing something else. It's up to you to decide what is the best use of your time.
As to the future of VB.NET, I doubt anyone around here really knows what will happen to Runtime function support in the future, although many will have an opinion. Like I said either here or in another of your threads, System.Web.Mail is already deprecated in .NET 2.0, so just because you stick to System namespaces is no guarantee of future support, although there are obviously ceratin things, like String.Length, that are unlikely to ever dissappear.
-
Re: Add Procedure, does it exist in VB.Net?
Quote:
Originally Posted by jmcilhinney
VB Code:
Dim stringToParse As String
Dim parsedResult As Double
If Double.TryParse(stringToParse, Globalization.NumberStyles.Any, Nothing, parsedResult) Then
MessageBox.Show ("The string is a number. It's value is " & parsedResult.toString() & ".")
Else
MessageBox.Show ("The string is not a number.")
End If
I have found this alternative, what do you think is the more efficient to use?
VB Code:
Private Sub txtHoursDay_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHoursWeek.KeyPress
'Only accept numbers
If Not e.KeyChar.IsDigit(e.KeyChar.toString) Then
e.Handled = True
End If
End Sub
-
Re: Add Procedure, does it exist in VB.Net?
That code will reject any key presses in the TextBox that are not in the range 0 - 9. I think that prevention is better than cure, i.e. it's better to stop invalid input than allow it and then give the user an error message. With validating TextBox input there are a lot of other circumstances to consider though. Your code will prevent the user using the BackSpace and Delete keys, amongst other things. It also doesn't stop them pasting any old text from the clipboard from the right-click menu. To create a TextBox that properly prevents any non-numeric input while allowing all the things you would normally want, which may include negative numbers and decimals, is more complex than that.
-
Re: Add Procedure, does it exist in VB.Net?
I have actually modified it to allow backspace and other things. I just wish to know what is the better alternative between the two. :)
-
Re: Add Procedure, does it exist in VB.Net?
As I said, in my opinion it is better to prevent invalid input in the first place, if possible, than to allow it and report an error. One thing I always like to do is give the user some audible feedback when I reject a character. I use the Beep function, but for those who don't like Runtime functions you can use the MessageBeep API.