Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 5th, 2008, 11:48 AM
Registered User
 
Join Date: Oct 2005
Location: Rockville, MD, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to build class structure for collection output

Based on reference dll example VB code, I try to created a some class
as
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class myclass
    Public m_name As String
    Public m_guid As String
    Public m_fstatus As Char

    Public Property name() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property

    Public Property guid() As String
        Get
            Return m_guid
        End Get
        Set(ByVal value As String)
            m_guid = value
        End Set
    End Property

    Public Property fstatus() As String
        Get
            Return m_fstatus
        End Get
        Set(ByVal value As String)
            m_fstatus = value
        End Set
    End Property

    Public Function getname() As Collection
        Dim tname As New Collection
        tname.Add(m_name)
        MsgBox("name-" & m_name)
        Return tname
    End Function
    Public Function getid() As Collection
        Dim tid As New Collection
        tid.Add(m_guid)
        MsgBox("id--" & m_guid)
        Return tid
    End Function
    Public Function getflag() As Collection
        Dim tflag As New Collection
        tflag.Add(m_fstatus)
        MsgBox("flag-" & m_fstatus)
        Return tflag
    End Function
    Public Sub getvideo()

        'test function code
        For i As Integer = 1 To 10
            m_name = "jimmy" & i
            m_guid = "1111" & i
            If m_guid Is DBNull.Value Then
                m_fstatus = "O"
            Else
                m_fstatus = "A"
            End If
        Next

    End Sub

End Class


I passed build dll without error.
I call reference this dll in form and call calss in procedure as

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim classObject As New myclass
        Dim fname As New Collection
        Dim fid As New Collection
        Dim fflag As New Collection

        fname = classObject.getname()
        fid = classObject.getid()
        fflag = classObject.getflag()
  Try
            If fname.Count <> 0 Then
                For i As Integer = 1 To 10
                    MessageBox.Show(fname(i))
                    Debug.Print("show: " & CStr(fname(i)))
                Next
            Else
                Debug.Print("no show: ")
            End If
        Catch ee As NullReferenceException
            MessageBox.Show("No Error code: 010001 " & vbCrLf & ee.Message)
            Beep()
            Exit Sub
        End Try
    End Sub



But I can call myclass.getneme with a error as "Collection index must be in the range 1 to the size of the collection."
note: there are no value at first loop.
How to build a class with output as classname.functionname in collection
*******************************
Example code:
Visual Basic example

Dim objEntityList As ListEntityProxy
Dim oEntity As Object

Set objEntityList = UIProxy.ListEntityProxy
For Each oEntity In objEntityList
    Debug.Print oEntity.Name
    Debug.Print oEntity.type
    Debug.Print oEntity.phone
Next

classname.function
ListEntityProxy as collection
UIProxy as class instance name
ListEntityProxy as property


jimusa
__________________
jimusa
Reply With Quote
  #2 (permalink)  
Old August 5th, 2008, 02:21 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If you are going to do this:
Code:
Public Class myclass
    Public m_name As String
    ...

    Public Property name() As String
        Get
            Return m_name
            ...

make m_name Private—you are exposing m_name through a property, 
not through it being Public.
Have you tried adding code in the DLL project that will use this class, then running that in the IDE to see what's happening in the Class?
Reply With Quote
  #3 (permalink)  
Old August 7th, 2008, 09:30 AM
Registered User
 
Join Date: Oct 2005
Location: Rockville, MD, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Brian,
Thanks for your help.
I made some changes . But I still can not get result as example.
 mycollection = myObject.getmy() syntax is not working
Please review my codes.
Thanks,

JIm
****************
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class myinfo
    Private m_name As String
    Private m_guid As String
    Private m_fstatus As Char

    Public Property name() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property

    Public Property guid() As String
        Get
            Return m_guid
        End Get
        Set(ByVal value As String)
            m_guid = value
        End Set
    End Property

    Public Property fstatus() As String
        Get
            Return m_fstatus
        End Get
        Set(ByVal value As String)
            m_fstatus = value
        End Set
    End Property

    Public Function getname() As Collection
        Dim tname As New Collection
        tname.Add(m_name)
        MsgBox("name-" & m_name)
        Return tname
    End Function
    Public Function getid() As Collection
        Dim tid As New Collection
        tid.Add(m_guid)
        MsgBox("id--" & m_guid)
        Return tid
    End Function
    Public Function getflag() As Collection
        Dim tflag As New Collection
        tflag.Add(m_fstatus)
        MsgBox("flag-" & m_fstatus)
        Return tflag
    End Function
    Public Sub getmy()
        'test function code
        For i As Integer = 1 To 10
            m_name = "jimmy" & i
            m_guid = "1111" & i
            If m_guid Is DBNull.Value Then
                m_fstatus = "O"
            Else
                m_fstatus = "A"
            End If
        Next

    End Sub

End Class

HEREi create a call class as
Imports vidlist2
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myObject As New myinfo
        Dim fname As String
        Dim fid As String
        Dim fflag As String
        Dim mycollection As New Collection
        mycollection = myObject.getmy()
        ' point to property to output
        Dim myreturn As Object
        For Each myreturn In mycollection
            fname = myreturn.name
            fid = myreturn.guid
            fflag = myreturn.fstatus

            MessageBox.Show(fname)
            MessageBox.Show(fid)
            MessageBox.Show(fflag)
        Next
        'Else
        ' MessageBox.Show("no data show: ")
        'End If
        'Catch ee As NullReferenceException
        'MessageBox.Show("No Error code: 010001 " & vbCrLf & ee.Message)
        'Beep()
        ' Exit Sub
        'End Try

    End Sub
End Class

jimusa
Reply With Quote
  #4 (permalink)  
Old August 7th, 2008, 04:49 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

.getmy is a SUB (a method, not a property)—it doesn't return anything for referencing with mycollection.

Since you intend (apparently) to fill mycollectio after creating it, do not create it with the New keyword in Form1_Load.

BTW: Code is stuff. "My codes" is mot how to refer to it, but rather, "My code."
"Cannot" is one word. "Can not" means "able to not"; "cannot" means "not able to."
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
return collection by function in class jimusa Visual Basic 2005 Basics 1 July 30th, 2008 03:18 PM
function return collection in class by VB jimusa Visual Studio 2005 1 July 30th, 2008 03:12 PM
Collection Class chapter 5 eggspencer BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 November 27th, 2006 03:46 PM
urgent doubt between class & structure lgeetha C# 5 February 24th, 2005 08:27 AM



All times are GMT -4. The time now is 09:53 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.