Results 1 to 27 of 27

Thread: The .NET Tip Thread

  1. #1

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  2. #2

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. x = x + 2
    2. 'the above can now be typed as
    3. x += 2
    4. 'This goes for all 4 mathimatical operators
    5. x *= 3 ' is the same as x = x * 3
    6. x /= 2 ' x = x / 2
    7. 'and so on...

  8. #8

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Very Very cool. Overloading is the term I believe. An example of polymorphism.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. Friend WithEvents Button1 As System.Windows.Forms.Button
    Notice the WithEvents keyword.

    Best regards

  11. #11

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.

  13. #13

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Ahh I get it now....Duh! Must be all the crack I am smoking.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. 'you may import the System.IO namespace to avoid
    2. 'typing the complete namespace name
    3. [b]'Please notice that we don't use the WithEvents keyword here
    4. Private fsw As System.IO.FileSystemWatcher[/b]
    5.  
    6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.     fsw = New System.IO.FileSystemWatcher()
    8.     [b]'add the event handler for the Changed event
    9.     AddHandler fsw.Changed, AddressOf FileSystemChanged[/b]
    10.     'The path property can be a local path, a mapped drive or any
    11.     'qualified UNC path
    12.     fsw.Path = "c:\"
    13.     fsw.IncludeSubdirectories = True
    14.     'If you set the Filter property to *.* it will watch
    15.     'all files that have an extention (like MyFile.dat)
    16.     'but not files that lack one (like SYSTEM)
    17.     'To filter ALL files set this property to an empty string
    18.     fsw.Filter = "*.txt"
    19.     'setting the NotifyFilter here is redundant since the default
    20.     'values are LastWrite + FileName + Directory
    21.     'But I set it anyway for demonstration purposes
    22.     fsw.NotifyFilter = IO.NotifyFilters.FileName Or IO.NotifyFilters.LastWrite
    23.     'set the EnableRaisingEvents to activate the watch
    24.     fsw.EnableRaisingEvents = True
    25. End Sub
    26. Private Sub FileSystemChanged(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
    27.     MsgBox(e.FullPath() & " has been changed", MsgBoxStyle.Information)
    28. End Sub
    Best regards

  15. #15

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Short Circuting: AndAlso

    You can now short circuit an If statement using AndAlso instead of And.

    VB Code:
    1. If a = 2 AndAlso B = 4 Then
    2. ....

    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by kovan
    ithought vb7 auto short circuited?
    No it doesn't. Try this and you'll see:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If test1() And test2() Then
    3.         MsgBox("Done")
    4.     End If
    5. End Sub
    6.  
    7. Private Function test1() As Boolean
    8.     MsgBox("Test1")
    9.     Return False
    10. End Function
    11.  
    12. Private Function test2() As Boolean
    13.     MsgBox("Test2")
    14.     Return True
    15. 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:
    1. If test1() AndAlso test2() Then
    Only Test1() will be called.

    Best regards

  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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 following
    VB Code:
    1. MsgBox Form1.Text1.Text
    But 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:
    1. Public Property TextBoxContent() As String
    2.     Get
    3.         TextBoxContent = TextBox1.Text
    4.     End Get
    5.     Set(ByVal Value As String)
    6.         TextBox1.Text = Value
    7.     End Set
    8. 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:
    1. MsgBox Form1.TextBoxContent
    Have fun

  19. #19
    zchoyt
    Guest

    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!!!!

  20. #20

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    just moving back to the top for newer people to see until we add some new tips.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  21. #21
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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

  22. #22

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  23. #23
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    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

  24. #24

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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..
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  25. #25
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    Opacity doesn't work with NT4.

  26. #26
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.

  27. #27

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Well then smack me around and call me Susan..well in any case..it should work on 2000 and XP and any furture OS'
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width