|
-
Nov 6th, 2001, 04:11 PM
#1
The .NET Tip Thread
I decided to start posting some tips and info on some of the new things and code tips for .NET for those wanting to learn and are unsure of the new additions and changes.
Todays Tip: What is this Imports thing I see in the following code?
Code:
Imports System.Web.UI.WebControls
Dim myLabel As Label
Dim myBox As TextBox
...
...
The easiest way to explain Imports is, think of it as a With...End With statement from Vb 6 and under.
What it does is provide a shortcut to classes/collections/objects available in the Namespace Imports provides
So in the above code its keeps your from having to type that namespace multiple times
Without it, your code would look like this:
Code:
Dim myLabel As System.Web.UI.WebControls.Label
Dim myBox As System.Web.UI.WebControls.TextBox
I hope this clears this up for anyone confused by Imports since you WILL be seeing it alot.
Feel free to reply with any questions and I will do my best to answer them
Last edited by Cander; Nov 8th, 2001 at 10:43 AM.
-
Nov 8th, 2001, 10:00 AM
#2
That is pretty darn cool..god I love .NET!!! One change though.
I would assume you could Inherit that class instead of using:
Public WithEvents
Last edited by Cander; Nov 8th, 2001 at 11:02 AM.
-
Nov 8th, 2001, 10:43 AM
#3
Control Inheritance
How many times have you wanted to make an extended user control that takes an existing control and only adds a little extra functionality to it, only to find that it is a real pain in the butt having to create all the events and properties from scratch to appear in your control?
Now it is so much easier thanks to the marvelous addition of Inheritance in VB .NET!!!
Now when you wnt to extend ..lets say a textbox and want to keep all its orginal uses but only add maybe code to allow number only..just Inherit the orginal TextBox
Code:
Imports System.Windows.Forms
Inherits TextBox
And that is it. Just add the extra code you need. It will already inherit the events/properties/methods of the original.no need to rewrite all that!!
ADDITIONAL Control Creating Info: We wont go into details on these, but you can also still create composite controls(controls made up of more than 1 control), and you can even create your own form control using GDI+ to draw the control, but this is much more difficult of course since you must fully program it.
-
Nov 8th, 2001, 10:51 AM
#4
Multi Threading A Class
Yes1 You can now multithread in VB. Lets see small snippetof code on how to multh thread a class in your program putting each instance into an array
Code:
Imports System.Threading
Dim myThreads() As Thread
myThread(newarrayindex) = New Thread(AddresOf classname)
Easy aye? Notice in the myThread = New..... line that we no longer use Set since in .NET everything is treated as an Object.. Set is gone.
newarrayindex is not something in .NET , i just used that show what you would put in the parenthesis
-
Nov 8th, 2001, 11:09 AM
#5
ByVal sender As System.Object??? WHAT THE??
By now you have probably seen the sender parameter in events and wondered what in the world it is. Well in .NET, controls can now share events. Because of this sender becomes a reference to the object that called the event. So if TextBox1 fired the event sender will be TextBox1. Therefor eif you want to change the text of that textbox in that event, you can just say
Code:
sender.Text = "Blah"
I will cover sharing events later.
-
Nov 9th, 2001, 10:18 AM
#6
Setting a variable when you declare it.
A not all that spectacular but highly requested feature in VB .NET is the ability to assign a value to a variable when it is declared. Now you can do it.
Code:
Dim myString As String = "Hello"
Again not all that special but since it is a new feature, I added it to the tips.
-
Nov 9th, 2001, 10:24 AM
#7
Here's an other easy tip.
In VB.Net we finally got the same assignment operaters that C/C++ and Java developers have had all the time.
VB Code:
x = x + 2
'the above can now be typed as
x += 2
'This goes for all 4 mathimatical operators
x *= 3 ' is the same as x = x * 3
x /= 2 ' x = x / 2
'and so on...
-
Nov 9th, 2001, 12:18 PM
#8
Multiple Subs/Functions with same name/different parameters?
Yes you can now do this highly requested addition to VB .NET.
Lets say you have 2 subs that have the same name, but the parameters are different
Code:
Sub Test(myString As String)
End Sub
Sub Test(myInteger As Integer)
End Sub
When you call the sub test, depending on what type of value you pass into it, it WILL run the correct Subroutine.
-
Nov 9th, 2001, 08:54 PM
#9
PowerPoster
Very Very cool. Overloading is the term I believe. An example of polymorphism.
-
Nov 12th, 2001, 04:30 AM
#10
Originally posted by Cander
That is pretty darn cool..god I love .NET!!! One change though.
I would assume you could Inherit that class instead of using:
Public WithEvents
The WithEvents keyword is just there so we can sink the events raised by the class. Inherite from it will not raise the events for you.
To see an other example using WithEvents simply start a new Windows application.
Put a command button on the form and double click it and type some code in the click event (of the command button).
Now look at the code the Form Designer has generated and you'll find the declaration of the button which would look something like this:
VB Code:
Friend WithEvents Button1 As System.Windows.Forms.Button
Notice the WithEvents keyword.
Best regards
-
Nov 13th, 2001, 09:46 AM
#11
Actually Inherit does get the events , as you can see from my Tip on creating controls. I suppose it depends what you need.
Inherits also has a down side of that you can only inherit 1 class per class I believe. Unless this has changed
-
Nov 13th, 2001, 10:30 AM
#12
Yes you inherite the events but you don't sink them.
Inheritance is used when you create a new class.
You would still need to create an object of the new class and use WithEvents to sink the events.
-
Nov 13th, 2001, 10:45 AM
#13
Ahh I get it now....Duh! Must be all the crack I am smoking.
-
Nov 14th, 2001, 07:56 AM
#14
When we're in the discussion of handling events I thought I could show you a new approach VB.Net has to this.
Using WithEvents is probably the easiest way and also well known from earlier versions of VB.
But you may also add and remove event handles as you wish using the AddHandler and RemoveHandler statements.
Using the AddHandler you declare an object variable the normal way. Unlike a WithEvents variable, this can be a local variable in a procedure.
Then use the AddHandler statement to specify the name of the event sender and the AddressOf statement to provide the name of your event handler.
By using this you can call the event handler whatever you like and you don't have to follow the traditional ObjectName_EventName() syntax.
Here's a rewritten example of the earlier posted code that uses the FileSystemWatcher class.
Notice that I don't use the WithEvents keyword when I declare the object.
Instead I've added a call to AddHandler in the Form_Load event.
If you want to use the the other events raised by the FileSystemWatcher class you have to make new calls to the AddHandler statement.
VB Code:
'you may import the System.IO namespace to avoid
'typing the complete namespace name
[b]'Please notice that we don't use the WithEvents keyword here
Private fsw As System.IO.FileSystemWatcher[/b]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fsw = New System.IO.FileSystemWatcher()
[b]'add the event handler for the Changed event
AddHandler fsw.Changed, AddressOf FileSystemChanged[/b]
'The path property can be a local path, a mapped drive or any
'qualified UNC path
fsw.Path = "c:\"
fsw.IncludeSubdirectories = True
'If you set the Filter property to *.* it will watch
'all files that have an extention (like MyFile.dat)
'but not files that lack one (like SYSTEM)
'To filter ALL files set this property to an empty string
fsw.Filter = "*.txt"
'setting the NotifyFilter here is redundant since the default
'values are LastWrite + FileName + Directory
'But I set it anyway for demonstration purposes
fsw.NotifyFilter = IO.NotifyFilters.FileName Or IO.NotifyFilters.LastWrite
'set the EnableRaisingEvents to activate the watch
fsw.EnableRaisingEvents = True
End Sub
Private Sub FileSystemChanged(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
MsgBox(e.FullPath() & " has been changed", MsgBoxStyle.Information)
End Sub
Best regards
-
Nov 19th, 2001, 11:56 AM
#15
Short Circuting: AndAlso
You can now short circuit an If statement using AndAlso instead of And.
VB Code:
If a = 2 AndAlso B = 4 Then
....
what this does is, if the first expression before the AndAlso returns false, then it will not evaluate the next expression after the AndAlso and will got straight to the Else statement.
-
Nov 19th, 2001, 01:18 PM
#16
Frenzied Member
ithought vb7 auto short circuited?
-
Nov 20th, 2001, 03:54 AM
#17
Originally posted by kovan
ithought vb7 auto short circuited?
No it doesn't. Try this and you'll see:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If test1() And test2() Then
MsgBox("Done")
End If
End Sub
Private Function test1() As Boolean
MsgBox("Test1")
Return False
End Function
Private Function test2() As Boolean
MsgBox("Test2")
Return True
End Function
As you will see both message boxes "Test1" and "Test2" will be displayed even though Test1() returns false.
If you replace the call in Form1_Load() into this:
VB Code:
If test1() AndAlso test2() Then
Only Test1() will be called.
Best regards
-
Nov 20th, 2001, 08:05 AM
#18
Since this question was up in another thread and I suspect it to become a fairly common question I desided to post it here in the Tip thread.
How can I read the property values of a control that resides in another form?
Let's assume you have a TextBox on Form1 and you need to get the value from that TextBox in Form2.
In VB6 you could simply do the followingBut it's considered bad practise to access controls, that resides on other forms, directly.
You should always consider controls to be private.
In VB.Net this has been enforced and you can't access the TextBox as in the VB6 example above.
What you should do is add your own property procedure to wrap around the controls different properties and methods.
So in Form1 (that is the container for out TextBox) we could add code simular to this:
VB Code:
Public Property TextBoxContent() As String
Get
TextBoxContent = TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property
Now we can use code simular to the following in another form to set or get the text from the TextBox on Form1
VB Code:
MsgBox Form1.TextBoxContent
Have fun
-
Nov 20th, 2001, 09:45 PM
#19
Text File I/O
I had a hard time finding information on I/O to text files. I use text files quite a bit at work.
In VB6 you could create a text file like this and output to it like this:
Open "c:\text.txt" for output as #1
Dim TempString as String
TempString = "Write a line to the file"
Print #1, TempString
Close #1
And you could read a text file like this:
Open "c:\text.txt" for input as #1
Dim TempString as String
Line Input #1, Tempstring
Msgbox TempString
Close #1
In VB.net you can create a file like this:
Dim fFileStream as FileStream = New FileStream("c:\text.txt", File.Create)
fFileStream.Close
Write to a text file like this:
Dim fStreamWriter as StreamWriter = New StreamWriter("c:\test.txt")
fStreamWriter.Writeline("This line is being written in the file")
fStreamWriter.Close()
And Read a line from a text file like this:
Dim FStreamReader as StreamReader = New StreamReader("c:\test.txt")
msgbox(fStreamReader.Readline())
fStreamReader.Close()
Always be sure to use the close method when you are done with using a file!!!!
-
Dec 4th, 2001, 01:14 PM
#20
just moving back to the top for newer people to see until we add some new tips.
-
Dec 4th, 2001, 05:56 PM
#21
Originally posted by Cander
just moving back to the top for newer people to see until we add some new tips.
By saying this I'm aware that I'm doing the same thing, but here goes:
Even though this thread could be considered to be very interesting to other people it isn't actually of more interest then any other post.
I say this because you or no one else around here can say that one post is more interesting then the other one.
It all depend on what you are looking for.
So, to keep it short, don't "bump" any post please.
All posts, including this one, is saved so it will make this site a little slower.
A lot of post makes it a lot slower.
This is OK though because that's what this forum is here for, but we can all help on keeping up the speed by not doing any unnecessary posting.
So please let us all keep this forum as clean as possible.
Best regards
-
Dec 5th, 2001, 01:20 PM
#22
If it was not for your avatar JA, i would hate you right now...
jk
I still think the bump was justified.
Anyway..got a new tip
Numerical data type changes.
There has been a change in the data types in VB .NET to coincide with how C++ and other language use them
In VB6 we had a 16 bit data type called Integer and a 32 bit data type called Long
But now in .NET we have
Long = 64 bit
Integer = 32 bit
Short = 16 bit
So integer becomes short..long becomes integer and the new 64 bit data type takes the name Long.
-
Dec 7th, 2001, 06:24 AM
#23
Hyperactive Member
Form Opacity
I'm not sure how useful this is as a tip, but there is now an "Opacity" property availble for a form. This means that a form can become semi transparent.
The only use I can think of is if you had a "Find" dialog box and wanted to be able to view the underlying form...
anyway - I tested it out with the scroll event of a progressbar (TrackerBar)
Code:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Me.Opacity = TrackBar1.Value / 100
End Sub
It may only work with windows2000
-
Dec 7th, 2001, 09:36 AM
#24
yes it is useful since it is a new thing in the .NET framework. And since it is a part of the framework, it will work on any Windows OS that has the framework installed..
-
Dec 7th, 2001, 09:39 AM
#25
Hyperactive Member
Opacity doesn't work with NT4.
-
Dec 7th, 2001, 11:22 AM
#26
Originally posted by Cander
yes it is useful since it is a new thing in the .NET framework.
And since it is a part of the framework, it will work on any Windows OS that has the framework installed..
You shouldn't depend on that the framework methods will work on all systems.
The truth is that many of the framework methods are simply wrappers around the Win API.
So some methods will only work on the OS that support it.
But the good news is that you don't have to trap for errors becuase of this.
-
Dec 7th, 2001, 12:30 PM
#27
Well then smack me around and call me Susan..well in any case..it should work on 2000 and XP and any furture OS'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|