|
Now VB.NET is object-oriented language. The following are some of the differences:
Data Type Changes
The .NET platform provides Common Type System to all the supported languages. This means that all the languages must support the same data types as enforced by common language runtime. This eliminates data type incompatibilities between various languages. For example on the 32-bit Windows platform, the integer data type takes 4 bytes in languages like C++ whereas in VB it takes 2 bytes. Following are the main changes related to data types in VB.NET:
. Under .NET the integer data type in VB.NET is also 4 bytes in size.
. VB.NET has no currency data type. Instead it provides decimal as a replacement.
. VB.NET introduces a new data type called Char. The char data type takes 2 bytes and can store Unicode characters.
. VB.NET do not have Variant data type. To achieve a result similar to variant type you can use Object data type. (Since every thing in .NET including primitive data types is an object, a variable of object type can point to any data type).
. In VB.NET there is no concept of fixed length strings.
. In VB6 we used the Type keyword to declare our user-defined structures. VB.NET introduces the structure keyword for the same purpose.
Declaring Variables
Consider this simple example in VB6:
Dim x,y as integer
In this example VB6 will consider x as variant and y as integer, which is somewhat odd behavior. VB.NET corrects this problem, creating both x and y as integers.
Furthermore, VB.NET allows you to assign initial values to the variables in the declaration statement itself:
br> Dim str1 as string = Hello
VB.NET also introduces Read-Only variables. Unlike constants Read-Only variables can be declared without initialization but once you assign a value to it, it cannot be changes.
Initialization here
Dim readonly x as integer
In later code
X=100
Now x can’t be changed
X=200 *********** Error **********
Property Syntax
In VB.NET, we anymore don't have separate declarations for Get and Set/Let. Now, everything is done in a single property declaration. This can be better explained by the following example.
Public [ReadOnly | WriteOnly] Property PropertyName as Datatype
Get
Return m_var
End Get
Set
M_var = value
End Set
End Property
Example:
Private _message as String
Public Property Message As String
Get
Return _message
End Get
Set
_message = Value
End Set
End Property
ByVal is the default - This is a crucial difference betwen VB 6.0 and VB.NET, where the default in VB 6.0 was by reference. But objects are still passed by reference.
Invoking Subroutines In previous versions of VB, only functions required the use of parentheses around the parameter list. But in VB.NET all function or subroutine calls require parentheses around the parameter list. This also applies, even though the parameter list is empty.
User-Defined Types - VB.NET does away with the keyword Type and replaces it with the keyword Structure
Public Structure Student
Dim strName as String
Dim strAge as Short
End Structure
Procedures and Functions
In VB6 all the procedure parameters are passed by reference (ByRef) by default. In VB.NET they are passed by value (ByVal) by default. Parantheses are required for calling procedures and functions whether they accept any parameters or not. In VB6 functions returned values using syntax like: FuntionName = return_value. In VB.NET you can use the Return keyword (Return return_value) to return values or you can continue to use the older syntax, which is still valid.
Scoping VB.NET now supports block-level scoping of variables. If your programs declare all of the variables at the beginning of the function or subroutine, this will not be a problem. However, the following VB 6.0 will cause an issue while upgrading to VB .NET
Do While objRs.Eof
Dim J as Integer
J=0
If objRs("flag")="Y" then
J=1
End If
objRs.MoveNext
Wend
If J Then
Msgbox "Flag is Y"
End If
In the above example the variable J will become out of scope just after the loop, since J was declared inside the While loop.
Exception Handling
The most wanted feature in earlier versions of VB was its error handling mechanism. The older versions relied on error handlers such as "On Error GoTo and On Error Resume Next. VB.NET provides us with a more stuructured approach. The new block structure allows us to track the exact error at the right time. The new error handling mechanism is refered to as Try...Throw...Catch...Finally. The following example will explain this new feature.
Sub myOpenFile()
Try
Open "myFile" For Output As #1
Write #1, myOutput
Catch
Kill "myFile"
Finally
Close #1
End try
End Sub
The keyword SET is gone - Since everything in VB.NET is an object. So the keyword SET is not at all used to differentiate between a simple variable assignment and an object assignment. So, if you have the following statement in VB 6.0
Set ObjConn = Nothing
Should be replaced as
ObjConn = Nothing.
Constructor and Destructor
The constructor procedure is one of the many new object-oriented features of VB.NET. The constructor in VB.NET replaces the Class_Initialize in VB 6.0. All occurance of Class_Initialize in previous versions of VB should now be placed in a class constructor. In VB.NET, a constructor is added to a class by adding a procedure called New. We can also create a class destructor, which is equivalent to Class_Terminate event in VB 6.0, by adding a sub-procedure called Finalize to our class. Usage of Return In VB.NET, we can use the keyword return to return a value from any function. In previous versions, we used to assign the value back with the help of the function name itself. The following example explains this:
Public Function Sum (intNum1 as Integer, intNum2 as Integer) as Integer
Dim intSum as Integer
intSum = intNum1 + intNum2
Return intSum
End Function
Static Methods
VB.NET now allows you to create static methods in your classes. Static methods are methods that can be called without requiring the developer to create instance of the class. For example, if you had a class named Foo with the non-static method NonStatic() and the static method Static(), you could call the Static() method like so:
Foo.Static()
However, non-static methods require than an instance of the class be created, like so:
Create an instance of the Foo class
Dim objFoo as New Foo()
Execute the NonStatic() method
ObjFoo.NonStatic()
To create a static method in a VB.NET, simply prefix the method definition with the keyword Shared.
Regards,
Munna Sarfraz Ahmad
--------------------------
Senior Software Engineer
Fiserv, Noida UP INDIA
Email msskythelimit@yahoo.com
|
|
|
|
|
|
And your point is?
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Wow - I've seen people write questions in articles, now it's articles in forums.
The answer is much shorter - VB6 is rubbish, and VB.NET is a real language.
Christian Graus
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 )
|
|
|
|
|
VB6 is great --- VB.Net is just greater.
Ask not whether it is useful. Ask what it is useful for.
|
|
|
|
|
Munna Sarfraz Ahmad wrote: But objects are still passed by reference.
Not quite.
Consider the following:
public sub sub1(byref x as collection)
x.add(45)
x=new collection
x.add(23)
end sub
public sub sub2(byval x as collection)
x.add(45)
x=new collection
x.add(23)
end sub
If 'x' is a collection, and I perform sub1(x), then x will now point to a new collection containing the value 23. If a reference to the old collection exists anywhere, the collection pointed to by that reference will now have contain the value 45. If x was the only reference to that collection, it will go bye-bye.
In the second scenario, when I perform sub2(x), then the collection pointed to by 'x' will have whatever it had in it before, along with 45. A new collection will be created and have 23 put into it, but that collection will go bye-bye sometime after the function exits.
|
|
|
|
|
i cant believe you bothered to read that!
|
|
|
|
|
Munna Sarfraz Ahmad wrote: The keyword SET is gone - Since everything in VB.NET is an object.
No, that's not the reason, because that is not true.
There is still a difference between simple variables (value types) and objects (reference types). A reference is however a value type, so whenever you make an assignment, you are copying a value type. As you can't assign the object itself, there is no use for the Set keyword.
Munna Sarfraz Ahmad wrote: We can also create a class destructor, which is equivalent to Class_Terminate event in VB 6.0, by adding a sub-procedure called Finalize to our class.
The memory management in .NET doesn't use reference counting, so the Finalize method is not a destructor. It's not equivalent to the Class_Terminate event. If you need to control the life cycle of an object, the class should implement the IDisposable interface.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi All,
I need a control that work like a Combobox, but accepted more than one selection like a Listbox control.
Someone help me?
Thanks,
|
|
|
|
|
Why not just use the listbox? is there some reason you dont want to use it?
|
|
|
|
|
Thank you Kevin,
Because I want a control that open a dropdown list when got the focus, and close it when lost the focus like a Combobox, but accepted more than one selection like a Listbox control.
|
|
|
|
|
Why not on the show the listbox when your textbox gets focus and hide the listbox when you textbox looses focus?
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
Thank you Steve Jowett,
OK. It is a good idea.
But I have a little problem. My experience in VS User Control construction is very poor. So I'm looking for something already done. A VB or C# User Control that I can downloaded and put in my forms.
Thanks
|
|
|
|
|
Hi to all,
I am trying to dynamically create label controls on TableLayoutPanel control, so that i have wrote following code. but at the debugging time it will genarate all the lables, but it doesnt show that panel as output. I checked its visible property its always false.... any body know why this happened?
Public Sub fillTablePanel(ByVal ds As DataSet, ByVal TableName As String)
FormContent.TablePanel.Parent = FormContent
FormContent.TablePanel.Visible = True
FormContent.TablePanel.BackColor = System.Drawing.SystemColors.ActiveCaptionText
FormContent.TablePanel.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.OutsetPartial
Dim tRows, tCols As Integer
FormContent.TablePanel.ColumnCount = ds.Tables(TableName).Columns.Count
For tCols = 0 To ds.Tables(TableName).Columns.Count - 1
FormContent.TablePanel.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100 / (ds.Tables(TableName).Columns.Count)))
Next
FormContent.TablePanel.RowCount = ds.Tables(TableName).Rows.Count
For tRows = 0 To ds.Tables(TableName).Rows.Count - 1
FormContent.TablePanel.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100 / (ds.Tables(TableName).Rows.Count)))
Next
ReDim FormContent.lblHeader(ds.Tables(TableName).Columns.Count)
For tCols = 0 To ds.Tables(TableName).Columns.Count - 1
FormContent.lblHeader(tCols) = New Label()
FormContent.TablePanel.Controls.Add(FormContent.lblHeader(tCols), tCols, 0)
FormContent.lblHeader(tCols).Text = Convert.ToString(ds.Tables(TableName).Columns.Item(tCols))
FormContent.lblHeader(tCols).Location = New Point(FormContent.TablePanel.Location.X, 10)
FormContent.lblHeader(tCols).Visible = True
FormContent.lblHeader(tCols).AutoSize = True
Next
FormContent.TablePanel.Location = New System.Drawing.Point(221, 46)
FormContent.TablePanel.Name = "TablePanel"
FormContent.TablePanel.RowCount = 2
Dim x As Integer = ds.Tables(TableName).Columns.Count
Dim y As Integer = ds.Tables(TableName).Rows.Count
ReDim FormContent.lblContent(x * y)
Dim ctr As Integer = 0
For tRows = 0 To ds.Tables(TableName).Rows.Count - 1
For tCols = 0 To ds.Tables(TableName).Columns.Count - 1
FormContent.lblContent(ctr) = New Label()
FormContent.TablePanel.Controls.Add(FormContent.lblContent(ctr), tCols, tRows)
FormContent.lblContent(ctr).Text = Convert.ToString(ds.Tables(TableName).Rows(tRows).Item(tCols))
FormContent.lblContent(ctr).Location = New Point(FormContent.TablePanel.Location.X, 100)
FormContent.lblContent(ctr).Visible = True
FormContent.lblContent(ctr).AutoSize = True
ctr += 1
Next
Next
FormContent.TablePanel.Size = New System.Drawing.Size(386, 93)
FormContent.TablePanel.TabIndex = 5
FormContent.TablePanel.Visible = True
End Sub
Pravin
|
|
|
|
|
I have only used the flowcontrol and in that case I created a user control for each set of data, placed all the management for the cotrol inside the user control and loaded 1 copy of the user control per record into the flowcontrol. That strategy may be relevant to the table control!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
please i have upgraded a .net project from 1.1 to 2.0 the application is running but the refences hold yelloe flags even i added them again and the application is generating an error while in debugging mode and working normally if i double click the .exe file.
Thanks
Mohammad Al Hoss
|
|
|
|
|
Could you post the debug information or errors that occur when you upgraded. Theoretically their shouldn't be too many problems.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios
Discounted or Free Software for Students:
DreamSpark - downloads.channel8.msdn.com
MSDN Academic Alliance - www.msdnaa.com
|
|
|
|
|
|
Kumaran21cen wrote: using C#.net.
Wrong forum.
If you are using Stream to save the file, then Stream.Length can solve the purpose.
|
|
|
|
|
I hav created a project in VB.Net where i have to create a setup wizard dat should work only for 30 days or 15 days what i am suppose to do. if the trial period get's over they should not be able to reinstall the setup again.in that particular pc any body help....
|
|
|
|
|
Hello all,I am wondering anyone can guide me how to read tiny text column for MySQL using VB.NET Datareader.
The following are the code I used to read the column:
Public Function StartupData(ByVal strKeySet as String)
Dim test as string
Dim MyTinyText as string
Try
ObjComm = New OdbcCommand("SELECT SettingValue FROM pos_setting WHERE LocationCode='KK' AND SettingKey='ADD_BINLOG'", ObjConn)
ObjConn.Open()
ObjRead = ObjComm.ExecuteReader()
If ObjRead.Read Then
test = ObjRead.Item(0)
MessageBox.Show("This is " & test & "")
Else
MessageBox.Show("No value")
End If
ObjConn.Close()
Catch ex As Exception
Finally
Objconn.Close
End Try
And my database table as follow:
---------------------------------------------
Field Type Collation Null Key Default Extra Privileges Comment
------------ ----------- ----------------- ------ ------ ------- ------ ------------------------------- -------
LocationCode varchar(10) latin1_swedish_ci NO PRI select,insert,update,references
SettingKey varchar(10) latin1_swedish_ci NO PRI select,insert,update,references
SettingValue tinytext latin1_swedish_ci YES (NULL) select,insert,update,references
I'm wondering why its keep showing there is no data read inside the datareader? Actually there is a '0' inside the SettingValue for that row of data but I'm wondering why it doesn't read it? Anyone can give me a solution for this?
Thanks for reading~!
modified on Wednesday, May 28, 2008 6:59 PM
|
|
|
|
|
Do you need the Not in If Not ObjRead.Read Then ?
If you are only wanting to get one value you could use objComm.ExecuteScalar() instead.
|
|
|
|
|
I'm sorry,I forgot to remove the not while testing it. Actually I want to check if the row exist in the database,then it will do something. But eventhough it exited this row but it doesn't run. Can anyone give me a solution how to check the tinytext content in MySQL database column? Am I used the right way?
Thank you~!
|
|
|
|
|
You can still do ExecuteScalar(). If no matching row is found nothing will be returned.
You might want to consider using the MySQL .Net Connector[^]. It has MySqlDbType.TinyText as a database type.
|
|
|
|
|
I am writing an email program, which will send mails to a number of email addresses in one go. But at the same time i want to save all the outgoing messages in my outlook express sent items folder. I am able to send mails in a loop but don't know how to save them in the Outlook Express Sent Items folder.
Is there any way to achieve the above functionality. Any code added will greatly help. I am using VB.Net 2005.
Thanks in advance.
Sekhar
|
|
|
|
|