10 Replies - 2016 Views - Last Post: 11 April 2013 - 07:32 PM Rate Topic: -----

#1 noodle654  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-March 13

Array size in a Structure, ReDim?

Posted 10 April 2013 - 08:49 PM

Hello again,

So my program is a database to store movies. The user inputs the data and it saves it to a textfile. While I am able to look up my last entry, I can not look up earlier entries. I narrowed it down to the fact that I do not have the size of my array declared. However, I cannot figure out how to declare it in a Structure, as it won't let me. I tried using ReDim in a Sub field in my module, but it still didn't work. I have attached my main form as well as my module. Any help on this would be great.


Imports System.IO

Public Class Form1
    Public Sub clearText()
        txtRating.Text = ""
        txtRunningTime.Text = ""
        txtVideoName.Text = ""
        txtYear.Text = ""
    End Sub
    Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearch.Click
        'Empty
    End Sub

    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
        'Empty
    End Sub

    Private Sub mnuFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFile.Click
        'Empty
    End Sub

    Private Sub mnuItemSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
        'Limits for sfd
        With sfdSave
            .Filter = "Text files (*.txt) |*.txt|All Files (*.*)|*.*"
            .InitialDirectory = "C:"
            .Title = "Save File As"
        End With
        'Open Save File dialog box and conduct error check for valid entry
        If sfdSave.ShowDialog() = Windows.Forms.DialogResult.OK Then
            MessageBox.Show(sfdSave.FileName)
        End If


        '''''''''''''''''''''''

    End Sub

    Private Sub mnuReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReport.Click
        
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        'Limits for sfd (copied from mnuSave)
        With sfdSave
            .Filter = "Text files (*.txt) |*.txt|All Files (*.*)|*.*"
            .InitialDirectory = "C:"
            .Title = "Save File As"
        End With
        
        'Declared Variable to hold User Response in Select Case
        Dim Answer As DialogResult = MessageBox.Show("Data has not been saved! Would you like to save your data before closing this application?", "Warning", MessageBoxButtons.YesNoCancel)

        Select Case Answer
            Case Windows.Forms.DialogResult.Yes 'Yes, open sfdSave. No, close form. Cancel, return to form
                sfdSave.ShowDialog()
            Case Windows.Forms.DialogResult.No
                Me.Close()
            Case Windows.Forms.DialogResult.Cancel
                Return
            Case Else
                Exit Sub
        End Select


    End Sub


    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        'Show About form (About.vb)
        frmAbout.Show()
    End Sub

    Private Sub AddItemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAddItem.Click
        'Declared local variable
        Dim myMovies As MovieRecord

        ''

        'Check for empty textboxes, if so show message box, return, and focus
        If txtVideoName.Text = "" Then
            MessageBox.Show("Please Enter a Video Name")
            txtVideoName.Focus()
            Return
        End If
        If txtYear.Text = "" Then
            MessageBox.Show("Please Enter a Year")
            txtYear.Focus()
            Return
        End If
        If txtRunningTime.Text = "" Then
            MessageBox.Show("Please Enter a Running Time")
            txtRunningTime.Focus()
            Return
        End If
        If txtRating.Text = "" Then
            MessageBox.Show("Please Enter a Rating")
            txtRating.Focus()
            Return
        End If

        ''

        'Copy inputs to datstore
        myMovies.g_strVideoName = txtVideoName.Text
        myMovies.g_intYearProduced = txtYear.Text
        myMovies.g_strRunningTime = txtRunningTime.Text
        myMovies.g_strRating = txtRating.Text

        'Open File "Vidoes.txt"
        g_txtFile = File.CreateText("Videos.txt")
        
        'Copy input to file "videos.txt"
        g_txtFile.WriteLine(myMovies.g_strVideoName)
        g_txtFile.WriteLine(myMovies.g_intYearProduced)
        g_txtFile.WriteLine(myMovies.g_strRunningTime)
        g_txtFile.WriteLine(myMovies.g_strRating)
        g_txtFile.Close() ' Close datatable

        'Confirm that the movie was added, clear textboxes, and return focus to VideoName textbox
        MessageBox.Show("Movie Added!", "Confirmation")
        clearText()
        txtVideoName.Focus()

    End Sub
    Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLoad.Click
        'Control ofd Dialog 
        With ofdLoadFile
            .Filter = "Text files (*.txt) |*.txt|All Files (*.*)|*.*"
            .InitialDirectory = Directory.GetCurrentDirectory
            .Title = "Select a File to Open"
        End With

        'Open ofd Dialog Box and conduct error checking for valid inputs
        If ofdLoadFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            MessageBox.Show(ofdLoadFile.FileName)
        Else
            MessageBox.Show("You did not select a file. No file will be loaded")
        End If


    End Sub

    Private Sub FindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFind.Click
        'Declared local variables
        Dim Name As String
        Dim Flag As Integer = 0
        Dim searchmyMovies As MovieRecord

        'Searchbox
        Name = InputBox("Enter Video Name")

        'Open database file
        g_SearchFile = File.OpenText("Videos.txt")
        Try
            While Not g_SearchFile.EndOfStream
                searchmyMovies.g_strVideoName = g_SearchFile.ReadLine()
                searchmyMovies.g_intYearProduced = g_SearchFile.ReadLine()
                searchmyMovies.g_strRunningTime = g_SearchFile.ReadLine()
                searchmyMovies.g_strRating = g_SearchFile.ReadLine()

                If searchmyMovies.g_strVideoName.Equals(Name) Then 'Flag a matching video 
                    Flag = 1
                    Exit While
                End If
            End While

            If Flag.Equals(1) Then
                txtVideoName.Text = searchmyMovies.g_strVideoName.ToString()
                txtYear.Text = searchmyMovies.g_intYearProduced.ToString()
                txtRunningTime.Text = searchmyMovies.g_strRunningTime.ToString()
                txtRating.Text = searchmyMovies.g_strRating.ToString()
                g_SearchFile.Close() 'Close datatable
            Else
                MessageBox.Show("Record Does Not Exist")

            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
        'Display Print Dialog Box
        pdPrint.Print()

    End Sub

    Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage
        'Default Printer Settings
        e.Graphics.DrawString("Video Data", New Font("MS Sans Serif", 12, FontStyle.Regular), Brushes.Black, 10, 10)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub txtVideoName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVideoName.TextChanged
    End Sub
End Class



Module
Imports System.IO


''
''
''
Module Module1
    Structure MovieRecord
        Public g_strVideoName As String
        Public g_intYearProduced As Integer
        Public g_strRunningTime As String
        Public g_strRating As String
    End Structure

    Public g_txtFile As StreamWriter
    Public g_SearchFile As StreamReader





End Module



Is This A Good Question/Topic? 0
  • +

Replies To: Array size in a Structure, ReDim?

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Array size in a Structure, ReDim?

Posted 10 April 2013 - 09:17 PM

Well, I don't see any array in your Structure.

Perhaps you want to have an array of Structures, which would make sense. If so, you need to declare that in your form.

Personally, I would use a List(Of T), which has virtually ALL the functionality of an array, and a number of really nice things an array doen't have.

This post has been edited by lar3ry: 10 April 2013 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 noodle654  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-March 13

Re: Array size in a Structure, ReDim?

Posted 10 April 2013 - 09:26 PM

View Postlar3ry, on 10 April 2013 - 09:17 PM, said:

Well, I don't see any array in your Structure.

Perhaps you want to have an array of Structures, which would make sense. If so, you need to declare that in your form.

Personally, I would use a List(Of T), which has virtually ALL the functionality of an array, and a number of really nice things an arry doen't have.


There is no array in the structure because it VB won't allow me to define the size of the array. I would like to use something easier (or better than an array), but it is for school and I must use an array.

If I try to make g_strVideoName the array size by doing
Public g_strVideoName(9) As String

VB returns "Arrays declared as structure members cannot be declared with an initial size"
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Array size in a Structure, ReDim?

Posted 10 April 2013 - 10:01 PM

View Postnoodle654, on 10 April 2013 - 10:26 PM, said:

There is no array in the structure because it VB won't allow me to define the size of the array. I would like to use something easier (or better than an array), but it is for school and I must use an array.

If I try to make g_strVideoName the array size by doing
Public g_strVideoName(9) As String

VB returns "Arrays declared as structure members cannot be declared with an initial size"

I think you might want to re-read the specification of your project, as you received it from your teacher/professor/instructor.

It soesn't really make sense to have an array in that structure. Even the name says it. It's a MovieRecord, which should be a single movie entry, specifying all the data for a single movie.

If you want to be able to look at all your movies, and you have to use an array, you should make an array of that Structure. Here's a minimal example (using my own names for brevity).

   Dim movies() as MovieRecord

   'then in the Button Code to add a record
   Dim mv as New MovieRecord   ' the New is important, as a Structure has only been defined
                               ' and it needs to be instantiated
   mv.name = txtName.Text
   mv.Runtime = CInt(txtruntime.text)
   mv.year = CInt(txtDate.Text)
   '... etc.
   movies(index) = mv


When you read in your file, you can load up the array in the same way. You only need to Dim mv once in the load Sub, because each time you add it to the array a copy is made of its contents, which will be replaced for each record you put into mv (of course, you can call mv any other name you want).

If this is clear to you, that's great. If not, do not hesitate to ask for clarification.
Was This Post Helpful? 1
  • +
  • -

#5 noodle654  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-March 13

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 10:39 AM

View Postlar3ry, on 10 April 2013 - 10:01 PM, said:

View Postnoodle654, on 10 April 2013 - 10:26 PM, said:

There is no array in the structure because it VB won't allow me to define the size of the array. I would like to use something easier (or better than an array), but it is for school and I must use an array.

If I try to make g_strVideoName the array size by doing
Public g_strVideoName(9) As String

VB returns "Arrays declared as structure members cannot be declared with an initial size"

I think you might want to re-read the specification of your project, as you received it from your teacher/professor/instructor.

It soesn't really make sense to have an array in that structure. Even the name says it. It's a MovieRecord, which should be a single movie entry, specifying all the data for a single movie.

If you want to be able to look at all your movies, and you have to use an array, you should make an array of that Structure. Here's a minimal example (using my own names for brevity).

   Dim movies() as MovieRecord

   'then in the Button Code to add a record
   Dim mv as New MovieRecord   ' the New is important, as a Structure has only been defined
                               ' and it needs to be instantiated
   mv.name = txtName.Text
   mv.Runtime = CInt(txtruntime.text)
   mv.year = CInt(txtDate.Text)
   '... etc.
   movies(index) = mv


When you read in your file, you can load up the array in the same way. You only need to Dim mv once in the load Sub, because each time you add it to the array a copy is made of its contents, which will be replaced for each record you put into mv (of course, you can call mv any other name you want).

If this is clear to you, that's great. If not, do not hesitate to ask for clarification.


Okay this makes a little bit more sense, I think I was confused about what exactly a structure was. I do not understand the movies(index) = mv part of your code...can you explain?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,499
  • Joined: 12-December 12

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 10:48 AM

@lar3ry
You've forgotten to ReDim the array in your example code.
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 01:35 PM

Quote

Okay this makes a little bit more sense, I think I was confused about what exactly a structure was. I do not understand the movies(index) = mv part of your code...can you explain?

OK. A Structure is a collection of variable that do not all have to be of the same type. When you use:

    Structure MovieRecord
        Public g_strVideoName As String
        Public g_intYearProduced As Integer
        Public g_strRunningTime As String
        Public g_strRating As String
    End Structure


you are defining a collection of Objects (in this case, three strings and an integer). What this does is allows you to create an object called MovieRecord that will contain all 4 objects that have been defined.

To create that object, you have to Dim the object, just like any other variable, but in this case, you will also need to use the word keyword New to create the object. So the Diim statement is (as in my example):

   Dim mv as New MovieRecord


of course, you don't have to name yours mv. You could call it Fred if you want. It just gives you a name that lets you refer to it, and what is created is an actual instance of the Structure you defined above. "Under the hood", it will be a chunk of memory that will contain space for three pointers to strings, and space for an Integer.

Now, you can refer to mv (or whatever you called it), and fill in the contents, so:

   mv.g_strVideoName = "Message From Space"
   mv.g_intYearProduced = 1978
   mv.strRating ="Hilarious"
   mv.g_strRunningTime = "105 Minutes"


At this point, you have a filled-in instance of your Structure, and can copy or assign or save it to a file without having to refer to the individual variables inside it. One of the things you can do with it, of course, is to assign it to a List or an Array. So you will Dim an array that will hold the the Structures...

Public movies() As MovieRecord


and after you Dim mv and fill in mv with values, you can assign it to the array...

   movies(0) = mv


and a copy the structure mv will be placed in movies(0).

The example I showed was adding a single entry when a button was clicked, but you can use the same technique within a loop that reads a line at a time, incrementing your index after filling out the structure. Again, in case you didn't catch the significance, you only need to use one instance of the Structure within the loop, because the array receives a copy of the Structure with the current contents.

andrewsw is right. You will have to ReDim the movies() array as you add to it. I didn't mention that because I thought it might distract you from the methodology of using a Structure. I'll leave that to you, but if it's giving you problems, please get back to us and ask your questions.

Edit: for code correction

This post has been edited by lar3ry: 11 April 2013 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,499
  • Joined: 12-December 12

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 01:45 PM

@Lar3ry you must be getting fed up of me, but this is not an array:

Dim movies As MovieRecord


Dim movies() As MovieRecord
ReDim movies(10)
movies(0) = mv

Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 02:44 PM

View Postandrewsw, on 11 April 2013 - 02:45 PM, said:

@Lar3ry you must be getting fed up of me, but this is not an array:

Dim movies As MovieRecord


Dim movies() As MovieRecord
ReDim movies(10)
movies(0) = mv

Right you are! I knew that, and put it doewn to a thinko (like a typo, but in my brain).
And no, I'm not getting fed up. I welcome all corrections to anything I post.

noodle654, I'll be making the correction in an edit of my previous post.

This post has been edited by lar3ry: 11 April 2013 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#10 noodle654  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-March 13

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 06:43 PM

First, thank you for the help! I follow everything you are saying, however when I implement it in my code it still does not save multiple inputs, but everything else still works. When I open the textfile that it creates, every time a newer entry is added the previous one just gets deleted.

Here is what I did:

ReDim g_myMovies(9)

            'Check for empty textboxes, if so show message box, return, and focus
            If txtVideoName.Text = "" Then
                MessageBox.Show("Please Enter a Video Name")
                txtVideoName.Focus()
                Return
            End If
            If txtYear.Text = "" Then
                MessageBox.Show("Please Enter a Year")
                txtYear.Focus()
                Return
            End If
            If txtRunningTime.Text = "" Then
                MessageBox.Show("Please Enter a Running Time")
                txtRunningTime.Focus()
                Return
            End If
            If txtRating.Text = "" Then
                MessageBox.Show("Please Enter a Rating")
                txtRating.Focus()
                Return
            End If

        '
            'Copy inputs to datstore
            g_mv.g_VideoName = txtVideoName.Text
            g_mv.g_YearProduced = CInt(txtYear.Text)
            g_mv.g_RunningTime = txtRunningTime.Text
            g_mv.g_Rating = txtRating.Text
        g_myMovies(0) = g_mv


            'Open File "Vidoes.txt"
            g_txtFile = File.CreateText("Videos.txt")

        'Copy input to file "videos.txt"
        g_txtFile.WriteLine(g_mv.g_VideoName)
        g_txtFile.WriteLine(g_mv.g_YearProduced)
        g_txtFile.WriteLine(g_mv.g_RunningTime)
        g_txtFile.WriteLine(g_mv.g_Rating)
        g_txtFile.Close() ' Close datatable

        'Confirm that the movie was added, clear textboxes, and return focus to VideoName textbox
        MessageBox.Show("Movie Added!", "Confirmation")
        clearText()
        txtVideoName.Focus()



    End Sub


My module contains the Structure and
 Public g_myMovies() As MovieRecord
Public g_mv as New MovieRecord

Was This Post Helpful? 0
  • +
  • -

#11 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Array size in a Structure, ReDim?

Posted 11 April 2013 - 07:32 PM

You are putting the entries into an array. Ask yourself what an array is. Is it a single variable or object, or a number of variables or objects?? If it's a number of variables, how do you access them for putting something in, and taking something out.

Most importantly, what happens if you use the same array index to store all your entries?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1