"As New" method - subroutine access - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

"As New" method - subroutine access

PsightoplazmPsightoplazm Posts: 332Member ✭✭
I have a program that I've created a template form and am dimensioning an array of these forms like so...

Dim X(200) as New fTemplateForm

so X(0) through x(200) are now all clones of this form. I would like to write some public subroutines in the template form that will be executable and accessable from outside the form object of each of these clones.

So if that sentence was complicated - Here is what does not work:

call X(1).MySubroutine(arg1, arg2)

I appreciate the help - thx



><//~Psightoplasm`~

Comments

  • infidelinfidel Posts: 2,900Member
    : I have a program that I've created a template form and am dimensioning an array of these forms like so...
    :
    : Dim X(200) as New fTemplateForm

    Does this actually instantiate 200 form objects? I'm a bit surprised if so.

    : so X(0) through x(200) are now all clones of this form. I would like to write some public subroutines in the template form that will be executable and accessable from outside the form object of each of these clones.
    :
    : So if that sentence was complicated - Here is what does not work:
    :
    : call X(1).MySubroutine(arg1, arg2)
    :
    : I appreciate the help - thx

    Is MySubroutines declared as Public?


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    Of course :P


    : : I have a program that I've created a template form and am dimensioning an array of these forms like so...
    : :
    : : Dim X(200) as New fTemplateForm
    :
    : Does this actually instantiate 200 form objects? I'm a bit surprised if so.
    :
    : : so X(0) through x(200) are now all clones of this form. I would like to write some public subroutines in the template form that will be executable and accessable from outside the form object of each of these clones.
    : :
    : : So if that sentence was complicated - Here is what does not work:
    : :
    : : call X(1).MySubroutine(arg1, arg2)
    : :
    : : I appreciate the help - thx
    :
    : Is MySubroutines declared as Public?
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :


    ><//~Psightoplasm`~

  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2006-6-8 13:12:46[/red][/b][hr]

    Hi,

    This is easier to do as a CLASS definition.

    I can do it in VB.Net but i would need to look it up for you so as to do it in standard VB.

    You might like to try a book from your library for this?

    [b][red]
    Download for FREE VB Express edition 2005 from>>
    http://msdn.microsoft.com/vstudio/express/vb/

    to run the code below. The CLASS definition is called anyPerson.vb
    [/red][/b]
    [b]
    A CLASS subroutine is accessible from every FORM.

    I could email you this entire projext as a ZIP file if you download and install the EXPRESS edition.

    It will not spoil your VB6 or existing setup as
    it installs to a separate directory. :-)


    [/b]
    Here is an example in VB.Net i have done with comments.>>
    [code]

    Private myDirectory() As anyPerson

    Private myDetails As New anyPerson("Fred", "Bloggs", "myRoad", "Acklam", _
    "Middlesbrough", "TS5 1DD", "England", "01642 111111")
    Private Sub showFirstName()
    'myDirectory(1).first_Name = "Fred"
    'myDirectory(1).sur_Name = "Bloggs"
    txtOutput.Text = myDirectory(1).first_Name & myDirectory(1).sur_Name
    'etcetera.
    End Sub
    Private Sub btnStart_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnStart.Click
    'Set up a response variable.
    Dim response As String
    'Show the existing stored details in a textbox on the screen.
    detailsToScreen()
    'Get a user response if needed.
    response = InputBox _
    ("Do you want to change the details of your name and address etc?", _
    "Change your personal details?", "N", 300, 500)

    'If the user answers "Y" or "y" for yes then get their new details.
    If response.ToUpper = "Y" Then
    'The 2 lines below (which aren't comments are really all one line of code)
    'puts firstName into myDetails.
    'The rest of the line is a prompt to the user.

    'The part with the question mark "?" changes the BLUE TITLE area of the INPUT BOX.

    'By putting "myDetails.first_Name" within the INPUTBOX it GETS the existing value
    'to show the user what it is, setting the INPUTBOX screen position to
    '300 pixels across and 500 down from the top of the screen.
    myDetails.first_Name = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "First name please?", myDetails.first_Name, 300, 500)

    'As above but for SurNAme.
    myDetails.sur_Name = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Surname please?", myDetails.sur_Name, 300, 500)

    'As above but for AddressLine1 input.
    myDetails.yourAddressLine1 = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Address line 1 please?", myDetails.yourAddressLine1, 300, 500)

    'As above but for AddressLine2 input.
    myDetails.yourAddressLine2 = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Address line 2 please?", myDetails.yourAddressLine2, 300, 500)

    'As above but for Location input.
    myDetails.yourLocation = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Your city/town/village location please?", myDetails.yourLocation, 300, 500)

    'As above but for Country input.
    myDetails.yourCountry = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Your country please?", myDetails.yourCountry, 300, 500)

    'As above but for Post/ZipCode input.
    myDetails.yourPostOrZipCode = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Your post/zip code please?", myDetails.yourPostOrZipCode, 300, 500)

    ''As above but for TelephoneNumber input.
    myDetails.yourTelNumber = InputBox("Click on OK or press ENTER to CONFIRM please.", _
    "Your telephone number please?", myDetails.yourTelNumber, 300, 500)

    'Confirm details.
    detailsToScreen()
    Else
    detailsToScreen()
    End If
    End Sub
    Private Sub detailsToScreen()
    txtOutput.Text = "Your details are as follows...." & NewLine & NewLine
    txtOutput.AppendText(myDetails.first_Name & NewLine)
    txtOutput.AppendText(myDetails.sur_Name & NewLine)
    txtOutput.AppendText(myDetails.yourAddressLine1 & NewLine)
    txtOutput.AppendText(myDetails.yourAddressLine2 & NewLine)
    txtOutput.AppendText(myDetails.yourCountry & NewLine)
    txtOutput.AppendText(myDetails.yourLocation & NewLine)
    txtOutput.AppendText(myDetails.yourPostOrZipCode & NewLine)
    txtOutput.AppendText(myDetails.yourTelNumber & NewLine)
    txtOutput.AppendText(NewLine & NewLine)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnExit.Click
    End
    End Sub
    [/code]

    [b][blue] The CLASS definition is as follows:>>[/blue][/b]
    [code]
    Public Class anyPerson
    Private firstName, surName As String
    Private address1, address2 As String
    Private location, postOrZipCode As String
    Private country, telNo As String
    Public Sub New(ByVal first_name As String, ByVal sur_name As String, ByVal addr1 As String, ByVal addr2 As String, ByVal loc As String, ByVal code As String, ByVal countr As String, ByVal num As String)
    firstName = first_name
    surName = sur_name
    address1 = addr1
    address2 = addr2
    location = loc
    postOrZipCode = code
    country = countr
    telNo = num
    End Sub
    Property first_Name() As String

    Get
    Return firstName
    End Get
    Set(ByVal Value As String)
    firstName = Value
    End Set
    End Property
    Property sur_Name() As String
    Get
    Return surName
    End Get
    Set(ByVal Value As String)
    surName = Value
    End Set
    End Property
    Property yourAddressLine1() As String
    Get
    Return address1
    End Get
    Set(ByVal Value As String)
    address1 = Value
    End Set
    End Property
    Property yourAddressLine2() As String
    Get
    Return address2
    End Get
    Set(ByVal Value As String)
    address2 = Value
    End Set
    End Property
    Property yourLocation() As String
    Get
    Return location
    End Get
    Set(ByVal Value As String)
    location = Value
    End Set
    End Property
    Property yourPostOrZipCode() As String
    Get
    Return postOrZipCode
    End Get
    Set(ByVal Value As String)
    postOrZipCode = Value
    End Set
    End Property
    Property yourCountry() As String
    Get
    Return country
    End Get
    Set(ByVal Value As String)
    country = Value
    End Set
    End Property
    Property yourTelNumber() As String
    Get
    Return telNo
    End Get
    Set(ByVal Value As String)
    telNo = Value
    End Set
    End Property
    End Class
    [/code]



    Regards,

    Dr M.


  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2006-6-8 13:24:13[/red][/b][hr]
    Hi,

    Express edition and VB.Net 2003 or 2005 versions have interactive typing help.

    The words "pop-up" as you type. :-)

    You can then use the TAB key to auto-complete a word.

    See the following link for proof of this and proof that a CLASS
    is available in ALL forms.>>

    http://i13.photobucket.com/albums/a272/u-might-want-this/Class-proof-of-use-from-any-form.jpg


    Regards,

    Dr M.



  • DrMartenDrMarten Posts: 748Member
    Here is another example but it won't work unless i emailed to you all of the project as the card image or picture files are in the BIN folder of this project i have.

    [code]
    Private theDeck As New DeckOfCards
    Private displayPaper As Graphics
    Private aCard As Card


    Private Sub btnHearts_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnHearts.Click
    'Pass appropriate suit and drawing area detail
    'to DeckOfCards class method DisplaySuit
    theDeck.DisplaySuit("hearts", displayPaper)
    End Sub
    Private Sub btnClubs_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnClubs.Click
    'Pass appropriate suit and drawing area detail
    'to DeckOfCards class method DisplaySuit
    theDeck.DisplaySuit("clubs", displayPaper)
    End Sub
    Private Sub btnDiamonds_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnDiamonds.Click
    'Pass appropriate suit and drawing area detail
    'to DeckOfCards class method DisplaySuit
    theDeck.DisplaySuit("diamonds", displayPaper)
    End Sub
    Private Sub btnSpades_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnSpades.Click
    'Pass appropriate suit and drawing area detail
    'to DeckOfCards class method DisplaySuit
    theDeck.DisplaySuit("spades", displayPaper)
    End Sub
    Private Sub btnLoad_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnLoad.Click
    'Declare local variables required
    'Card details
    Dim cardName As String
    Dim cardImage As Image
    Dim theSuits() As String = {"hearts", "clubs", "diamonds", "spades"}
    'For Loop control variables
    Dim theSuit As Integer
    Dim theRank As Integer

    'For each suit
    For theSuit = 0 To 3
    'For each card in the suit
    For theRank = 1 To 13
    'Identify card name.
    'Example: On FIRST inner loop - theSuit = 0; theRank = 1
    '1.ToString = "1"; theSuits(0).Substring(0,1) = "h"
    'Gives: "1" & "h" & ".png" = 1h.png
    cardName = theRank.ToString & theSuits(theSuit).Substring(0, 1) & ".png"
    'Get the required image from file
    cardImage = Image.FromFile(cardName)
    'Create a new instance of card.
    'Example: On SECOND inner loop:
    ' cardImage = 2h.png
    ' theSuits(theSuit) = theSuits(0) = hearts
    ' (theRank - 1) * 51 = 51 [cards X value]
    ' 5 [cards Y value]
    aCard = New Card(cardImage, theSuits(theSuit), (theRank - 1) * 51, 5, theDeck)
    Next
    Next
    'Display array list size
    lblListCount.Text = "List Count: " & theDeck.Count
    End Sub
    Private Sub btnRemove_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnRemove.Click
    'Remove all cards from array list
    theDeck.RemoveCards(theDeck)
    'Display array list size
    lblListCount.Text = "List Count: " & theDeck.Count
    'Repaint drawing area
    displayPaper.Clear(Color.White)
    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnExit.Click
    'Terminate program
    End
    End Sub
    [/code]

    The CLASS definitions are:>>
    [code]
    ' This is card.vb
    Public Class Card
    Private theCards As DeckOfCards
    Private cardImage As Image
    Private cardSuit As String
    Private cardValue As Integer
    Private cardX As Integer
    Private cardY As Integer
    Private cardWidth As Integer
    Private cardHeight As Integer

    Public Sub New(ByVal newCardImage As Image, _
    ByVal newCardSuit As String, _
    ByVal newCardX As Integer, _
    ByVal newCardY As Integer, _
    ByVal newCards As DeckOfCards)
    cardImage = newCardImage
    cardSuit = newCardSuit
    cardX = newCardX
    cardY = newCardY
    cardWidth = 48
    cardHeight = 70
    theCards = newCards
    theCards.Add(Me)
    End Sub
    'Methods
    Public Sub DisplaySuit(ByVal theSuit As String, _
    ByVal paper As Graphics)
    'Receives drawing area details from deckofcards class code
    Dim myImage As Image = cardImage
    'If card is of required suit
    If Me.cardSuit = theSuit Then
    'Display card on screen (within picture box)
    paper.DrawImage(myImage, cardX, cardY, Me.cardWidth, Me.cardHeight)
    End If

    End Sub
    Public Sub RemoveCards(ByVal theCards As DeckOfCards)
    theCards.Remove(Me)

    End Sub
    End Class
    [/code]

    [code]
    'This is DeckOfCards.vb
    Public Class DeckOfCards
    Private cardList As ArrayList
    'Constructor
    Public Sub New()
    cardList = New ArrayList
    End Sub
    'Methods
    Public Sub Add(ByVal aCard As Card)
    'Adds an individual card to the array
    cardList.Add(aCard)
    End Sub
    Public Sub Remove(ByVal aCard As Card)
    'Removes an individual card from the array
    cardList.Remove(aCard)
    End Sub
    Public Sub DisplaySuit(ByVal theSuit As String, _
    ByVal paper As Graphics)
    'Passes each card and drawing area details to card class
    Dim index As Integer = 0
    While index < cardList.Count
    CType(cardList.Item(index), Card).DisplaySuit(theSuit, paper)
    index = index + 1
    End While
    End Sub
    Public Sub RemoveCards(ByVal theCards As DeckOfCards)
    Dim index As Integer
    Dim theCount As Integer
    theCount = cardList.Count - 1
    For index = theCount To 0 Step -1
    CType(cardList.Item(index), Card).RemoveCards(theCards)
    Next
    End Sub
    Public ReadOnly Property Count() As Integer
    Get
    Return cardList.Count
    End Get
    End Property
    End Class
    [/code]

  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    My original need was to work with a subroutine that was local to a particular dimensioned form because I would be able to write generic code to work with the controls that are local to the form where the code is. Sorry for my lack of explanation - I guess I always just thought you guys were mind readers :)

    Ya, I'm not looking to make a class reference or anything like that. And I am using a stripped version VB6 because I can compile it to run with out any additional installation (.net requires additional downloads and/or installs) - Which I could use C++ for, but this particular project will be MUCH quicker for me in VB. (Since the VBRUN.DLL is included with the base install of xp)

    If there isn't a way to invoke a public subroutine from an "arrayed" template form I can write more intelligent code, but if anyone else knows a way to do this it would be very much appreciated.

    Thanks again, guys, for your help.

    ><//~Psightoplasm`~

Sign In or Register to comment.