Learning how to create a sub catagorie and struggling

hi
ok my assignment is to Adding a Sub procedure. Write a Sub procedure DisplaySquare to display the solid square. The size ( the length of each side) should be specified by the Integer param-eter size. The character that fills the square should be specified by the String parameter fillCharacter. Use a For Next statement nested within another For Next statement to create the square. The outer For Next specifies what row is currently being displayed. The inner For Next appends all the characters that form the row to a display String. Use the multiline TextBox provided to display the square. For example, if size is 8 and fillCharacter is #, the application should look similar to Fig. 13.29.

So i have to go by the size and then what charactor i entered but there are alot of charactors and this is the most confusing assignment yet.
Here is what I got so far:
Public Class DisplaySquareForm

    Private Sub displaySquareButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displaySquareButton.Click
        ' Declare Variables
        Dim size As Integer
        Dim fill As String

        size = Val(sizeTextBox.Text)
        fill = fillTextBox.Text

        ' Call DisplaySquare
        DisplaySquare(size, fill)
    End Sub
End Class ' DisplaySquareForm

'DisplaySquare
Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)

    'Declare Variables
    Dim size As Integer
    Dim fill As String

    If size Then.......

Im so lost any help please
goldfingerpunkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...so in DisplaySquare() you need to:

    "Use a For Next statement nested within another For Next statement to create the square. The outer For Next specifies what row is currently being displayed. The inner For Next appends all the characters that form the row to a display String."

Start with the outer loop.  You have the number of rows passed in to your sub via the "size" variable.

Write for us a simple "For Next" loop using the "size" variable that would iterate that number of times.

*insert your outer loop here*

Now how would you make an inner loop (inside the outer "For Next" loop you just wrote) that would iterate that same number of times for the columns?

*show us your outer loop with the inner loop inside*
0
goldfingerpunkAuthor Commented:
are you talking about in the beggining displaysquare() to put the for next inside of the paranthesis
0
goldfingerpunkAuthor Commented:
dude im sorry i have no clue what that means at all for some reason i cant figure it out.
For size..... then im stuck
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes...you need to put the code between the "Private Sub" line and the "End Sub" line:

    Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)

        ' ... just start with ONE loop for the rows using "size" here! ...

    End Sub
0
CodeCruiserCommented:
Because its an assignment, we are not going to provide you the full code for your own good.

You need to create a loop which starts at 0 and goes upto number of rows - 1.

Do you know how to write a for loop?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you need help with the syntax then check out the Example section here:
http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
0
goldfingerpunkAuthor Commented:
'DisplaySquare
Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)

    'Declare Variables
    Dim size As Integer
    Dim fill As String

    For sizecounter As Integer = 0 To -1 Step 1

    Next


ok here is what i got so far am i right so far. also its underlined in blue at the top where it says sub displaysquare..... why is it doing that
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need a corresponding "End Sub" somewhere below the declaration for DisplaySquare.

Also, DisplaySquare() must exist INSIDE an existing class, such as the form.

This is valid:
Public Class Form1

    Public Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)

    End Sub

End Class

Open in new window


While this is not:
Public Class Form1

End Class

Public Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)

End Sub

Open in new window


Looking at your For...Next loop:

    For sizecounter As Integer = 0 To -1 Step 1

    Next

This would make "sizecounter" range from 0 (zero) to -1 moving in a positive direction with a step of 1.

Since -1 is less than zero , we can't get to it moving in a positive step of 1.  Therefore this loop will do nothing.

You need to make the loop move from 1 to "size" (the parameter passed in).

Put a button on a blank form and try this code out to see how a For loop works:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For sizecounter As Integer = 0 To 10 Step 2
            MessageBox.Show("sizecounter = " & sizecounter)
        Next
    End Sub

End Class

Open in new window

0
goldfingerpunkAuthor Commented:
ok i see what your saying about the -1. It doesnt give a specific number to stop at so i just used 100 but here is what i got right now and for some reason its not letting use the outputtextbox as anything which is where all my information goes i even tried adding it above so....
Private Sub displaySquareButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displaySquareButton.Click
        ' Declare Variables
        Dim size As Integer
        Dim fill As String
        Dim output As String

        size = Val(sizeTextBox.Text)
        fill = fillTextBox.Text
        output = outputTextBox.Text
        ' Call DisplaySquare
        DisplaySquare(size, fill)
    End Sub
End Class ' DisplaySquareForm

Public Class DisplaySquare
    Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)
        Dim outputtextbox As String




        For sizecounter As Integer = 0 To 100 Step 1
            outputtextbox = sizecounter * fill

        Next
    End Sub
End Class


hmmm but i do have to figure out a code for the number of rows plus the number of squares wow. I wish i knew how to get it to go into the output textbox
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
K...your form is called "DisplaySquareForm" right?  For the purposes of this simple assignment, the DisplaySquare() method needs to be in the SAME class as the Form.

So rearrange it like this:
Public Class DisplaySquareForm

    Private Sub displaySquareButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displaySquareButton.Click
        ' Declare Variables
        Dim size As Integer
        Dim fill As String
        Dim output As String

        size = Val(sizeTextBox.Text)
        fill = fillTextBox.Text
        output = outputTextBox.Text
        ' Call DisplaySquare
        DisplaySquare(size, fill)
    End Sub

    Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)
        Dim outputtextbox As String

        For sizecounter As Integer = 0 To 100 Step 1
            outputtextbox = sizecounter * fill

        Next
    End Sub

End Class ' DisplaySquareForm

Open in new window


Looking again at your For...Next loop:

        For sizecounter As Integer = 0 To 100 Step 1

        Next

Great.  This would move "sizecounter" from 0 to 100...but we don't want to stop at 100!  The variable "size" has the value we want to stop at.

What could you possibly replace in the "For" line to make it stop at "size" instead of 100?

Again, you just need to change one thing in this line:

        For sizecounter As Integer = 0 To 100 Step 1

Hint: You don't want 100 in there!

*Also, the default Step value is 1 (one) so you don't have to explicitly write it if you don't want to.

These are equivalent:

    For x = 1 to 5 Step 1   <==>   For x = 1 to 5
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
What is the name of the MuliLine TextBox you are supposed to output to?
0
goldfingerpunkAuthor Commented:
outputtextbox
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...so as an example, I can set the text in that box with something like:

    outputtextbox.Text = "Idle_Mind was here!"

I would NOT need to declare a local variable called "outputtextbox" to do this.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You figure out how to fix that For loop so it uses "size" yet?...

*Leaving to go pick up the kids from school.  I'll be back in about an hour.
0
goldfingerpunkAuthor Commented:
0 to infinity or 0 to NaN
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Does this snippet help?  
Dim someValue As Integer = 10
    For x As Integer = 1 to someValue
        MessageBox.Show("x = " & x)
    Next

Open in new window


Now how could you a similar thing with the "size" variable that is passed into DisplaySquare()?
0
goldfingerpunkAuthor Commented:
numeric
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Where are you at goldfingerpunk?...made any progress?
0
goldfingerpunkAuthor Commented:
not really at the moment im looking for the someValue you were hinting at earlier and im going through my book trying to figure out what to put in there to make the number go on forever
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
The "someValue" variable is declared at line number #1 in the snippet.  The whole point of that example was that the For loop was using a variable declared somewhere else as the ending value of the loop structure!

    For x As Integer = 1 to someValue

So instead of having a static value like 100, we are using the value contained within the "someValue" variable (which was ten in the snippet).

In DisplaySquare(), you are receiving the number or rows/columns to display via the "size" variable in the parameters:

    Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)

Thus you can use "size" within that method to control number of rows generate by the outer For loop:

    Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)
        For row As Integer = 1 to size
            MessageBox.Show("Row #" & row)
        Next
    End Sub

*For endless loops you'd use a While/End While or a Do/Loop.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goldfingerpunkAuthor Commented:
oh ok wow see i knew it was going to be something so simple that i should have known that. ugh this is frustrating sometimes but ima keep pushing myself til i get it. so are you also saying that i should put a messagebox.show after 1 to size or am i good with outputtextbox.text = size * fill. And now we are moving on to the other for next inside the for next right?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
The MessageBox is simply there to show to you (the programmer) the mechanics of what it is doing.

Yes...write the inner loop now.

Your snippet of "outputtextbox.text = size * fill" is not going to do what you think it will!  =)
0
goldfingerpunkAuthor Commented:
so i should just get rid of the outputtextbox.text line then
0
goldfingerpunkAuthor Commented:
and im guessing here but it seems that right after the first for line the next line would start with something like ... for fill as string = ...
0
goldfingerpunkAuthor Commented:
nevermind that wasnt right obviously damnit. um well its suppose to be the number of charactors in a row but how do i put that into the multiline textbox??
0
goldfingerpunkAuthor Commented:
how would i get the fill string in a for statment after the first for statment i keep getting error messages like is it suppose to be dim as something different ive never done anything with charactors before so thats why this is so rough
0
goldfingerpunkAuthor Commented:
For fillcounter As Object = size To fill
does that look right at all
0
goldfingerpunkAuthor Commented:
 For sizecounter As Integer = 0 To size Step 1
            For fillcounter As Object = size To sizecounter
                outputTextBox.Text = (sizecounter & fillcounter & _
                                     String.Format("{0:C}", fill) & ControlChars.CrLf)

            Next
        Next
ok im getting somewhere but the second for is still messed up when i debugged it and typed in 8 for the size and # for the charactor all i got in the outputtextbox was 88# no additional lines or anything so what am doing wrong
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Start by clearing the TextBox:

    outputtextbox.Clear()

Now let's make it output something for every row.  We already have the For/Next loop in place so let's change that MessageBox.Show() to something that will output to the TextBox instead:

    Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)
        outputtextbox.Clear()
        For row As Integer = 1 to size
            ' MessageBox.Show("Row #" & row)
            outputtextbox.AppendText("Row #" & row & vbCrLf)
        Next
    End Sub

We won't keep that line but hopefully it will make some lights turn on!

Now how can we use an inner For/Next loop to instead output a series of characters based on the value in "fill"?

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
*Looks like we cross-posted*

What if I told you to use an inner loop that is EXACTLY like your outer loop except that it uses a different variable name than "row"?...and to output only one character at a time per inner loop iteration?
0
goldfingerpunkAuthor Commented:
so i would put fill in instead of row but if i do that all i get is some error message because its dim as a string.
0
goldfingerpunkAuthor Commented:
it wouldnt be fillcounter would it
0
goldfingerpunkAuthor Commented:
Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)



        For sizecounter As Integer = 0 To size Step 1
            For fillcounter As Integer = 0 To size
                outputTextBox.Text = ("#" & sizecounter * fillcounter & vbCrLf)

            Next
        Next
    End Sub
End Class

ok here is what i got so far and somethings not right cause i still get the one row of #64
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's much better and very close!  By starting at zero, though, you are going to end up with one more line than you need...

You're only getting one row because setting the .Text property replaces whatever was already there with the new stuff you specified.

Instead of setting .Text outright, you can use the AppendText() method.

Change it to this and then see if you can figure out what needs to be done to fix it:

        outputTextBox.Clear()
        For sizecounter As Integer = 0 To size Step 1
            For fillcounter As Integer = 0 To size
                outputTextBox.AppendText(fill)
            Next
        Next

An alternative would be:

    outputTextBox.Text = outputTextBox.Text & fill
0
goldfingerpunkAuthor Commented:
ok now i got somewhere i got one line of # all the way across in my outputtextbox so now all i need to do is figure out how to get the # to be in seperate lines of 8 in this example i think it would be 8 across and 8 rows down but i doesnt help me in my book at all and hmmm any hints
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use this line to make it wrap:

    outputTextBox.AppendText(vbCrLf)

Where should it go though?...
0
goldfingerpunkAuthor Commented:
ok here is what i got i finally got one row of 8 but it wont go down 8 rows of 8...
 For sizecounter As Integer = 0 To size
            For fillcounter As Integer = size To size
                outputTextBox.AppendText(fill)

            Next
        Next
0
goldfingerpunkAuthor Commented:
it should go through the size right
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Your For loops were just fine as they were!   (except for starting with zero instead of one)

You need to insert this line:

    outputTextBox.AppendText(vbCrLf)

Somewhere in here:
*leave the below code "as is" and literally put the line above somewhere in-between lines and it will work!
Private Sub DisplaySquare(ByVal size As Integer, ByVal fill As String)
    outputTextBox.Clear()
    For sizecounter As Integer = 1 To size
        For fillcounter As Integer = 1 To size
            outputTextBox.AppendText(fill)
        Next
    Next
End Sub

Open in new window

0
goldfingerpunkAuthor Commented:
no thats not right um i dont know what its suppose to go through the string
0
goldfingerpunkAuthor Commented:
yea im lost
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well...try that line at each of these positions until you figure out which one works:

    1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5
0
goldfingerpunkAuthor Commented:
ok so i put it under the first for line and it works the one problem is i have one blank line in the begging of the multiline textbox
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
So it's wrapping before the first line was output.  Move it to where it will wrap after it has output the required number of characters for a row.  Where is that?
0
goldfingerpunkAuthor Commented:
ahhhhh after the next wow this was the most frustrating assignment thank you soooo much
0
goldfingerpunkAuthor Commented:
Great helper knows what he is doing!!!
0
CodeCruiserCommented:
You are a good teacher Idle_Mind :-)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Woohoo!  Glad you finally solved it!  It seems like some light bulbs turned on during this assignment...

I think the next one may be easier!  =)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.