Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
|
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2007, 03:58 PM
Authorized User
 
Join Date: Dec 2006
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default Creating a library of subroutines

I have multiple (8-10) subroutines that I call over and over in my application to fill combo boxes.
Right now, the routines are in each form's .vb code file.
I want to have one set of routines that I can call from anywhere in the application.
I have tried creating a 'library' by putting all the subroutines in one .vb file and making them all public shared. I added (byref sender) to each one and converted all the references in each one from me to sender.

It didn't work. The combo boxes didn't get populated.

Any clues to get me on the correct path?

Thanks,
Karen

 
Old July 5th, 2007, 09:01 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Couple of things. Does the class (.vb) file exist in the same namespace as your project? Without going into a lengthy detail if you created the class file inside of a folder it will not belong to the default namespace it will belong to: (This is the behavior in C# and I haven't worked in VB in a long time, but I am assuming the behavior is the same)

[namespace].[foldername]

and you will need to add an import statement to access your class (or use the fully qualified name: namespace.foldername.[classname] )

Also, is the CLASS declared as public or protected?

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old July 6th, 2007, 06:51 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

As with everything in this forum there are always multiple options available. Here is mine:

1.) Create a module for your application.
2.) Create a public function that passes a combo box
    from your form as a parameter and returns a combo box
    as a value.

Example:

Module Module1

    Public Function fncFillComboBox(ByVal cbo As ComboBox) As ComboBox

        '* Simply filling the combobox with numbers.
        For intX As Integer = 0 To 5
            cbo.Items.Add(intX.ToString)
        Next

        cbo.SelectedIndex = 0

        '* Return the combobox to your calling form.
        fncFillComboBox = cbo

    End Function

End Module

Assuming a simple example form with a button and a combo box
here is sample code to fill your combo box:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.ComboBox1 = Module1.fncFillComboBox(Me.ComboBox1)
    End Sub

End Class

You could easily change the example function to pass not only the combo box but also an SQL statement that could be processed within the function.




Best Regards,
Earl Francis
 
Old July 6th, 2007, 07:22 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Nicely done Earl ^^

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old July 6th, 2007, 02:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:if you created the class file inside of a folder it will not belong to the default namespace it will belong to: (This is the behavior in C# and I haven't worked in VB in a long time, but I am assuming the behavior is the same)
No, this behavior is not consistent between C# and VB.

A new class created in a folder in C# using Visual Studio will result in the class being contained in the project's root namespace plus the folder name as Doug offered. However, you can MOVE a class file into a folder and the class will not take on the folder name as another "branch" in its namespace. This behavior is strictly a VS.NET "feature" when you add a class file.

In VB.NET, be default class files (regular classes as well as web forms and similar entities) do not include a namespace declaration. Therefore, they simply take the project default namespace as their namespace.

Quote:
quote:Public Function fncFillComboBox(ByVal cbo As ComboBox) As ComboBox
A combobox is an object. Even when passing it "ByVal" in VB, you are still just passing the value of the object reference so you don't need to return anything from this type of method.

When you DO need to return a value, it's cleaner to use the "Return" keyword instead of the old vb style:

    <function name>=<return value>

just use:

    Return <return value>

-Peter
 
Old July 6th, 2007, 02:58 PM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Peter,
 
Quote:
quote:A combobox is an object. Even when passing it "ByVal" in VB, you are still just passing the value of the object reference so you don't need to return anything from this type of method.
Quote:

When you DO need to return a value, it's cleaner to use the "Return" keyword instead of the old vb style:
Point taken. It does clean up the code.



Best Regards,
Earl Francis





Similar Threads
Thread Thread Starter Forum Replies Last Post
use of class library Theone84 C# 1 July 26th, 2008 02:19 AM
creating smart tag library Medes VS.NET 2002/2003 0 April 16th, 2006 02:56 PM
creating smart tag library Medes Visual Studio 2005 0 April 10th, 2006 03:29 PM
Class Library ~Bean~ ASP.NET 1.x and 2.0 Application Design 3 October 17th, 2005 07:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.