Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to learn VB .NET from some books and the web. I like to do the practice programs so I'm not just "reading" but "doing" what I'm learning, (or trying to learn). I'm stuck on 2 of them. I'll just post this one for now. I can write it (sort of) just using a bunch of nested if statements and so on however a friend of mine told me that's not exactly the way there wanting it done here per the instructions. So I'm sort of stuck and just need to see how it should look to satisfy the instructions.

 

Write a Console application that lets the user decide if they want to make a Rectangle or a Box. Depending on their answer, prompt them for the necessary values (length and width or length, width and height) and then create an object of that type. Once this is complete, use the ToString method to print a description of the object and then the value of either the area or volume of the object to the Console.

 

Although not required to look this way, your program output might look like:

 

Would you like to create a Rectangle or a Box (r or b): b

Please enter the length: 4

Please enter the width: 23

Please enter the height: 3

 

This object has length: 4 and width: 23 and height: 3

This box has volume: 276

Posted

Hi and welcome,

 

It would help if you posted what you had done already so we can guide you and let you know where/if you are going wrong.

 

Don't worry about getting it wrong we've all had to learn to program here and have all made some embarrassing mistakes along the way.

Posted

Posting the code you have would be helpful; however, your quoted text looks like it should have all the algorithm you need to build this application.

 

What specifically do you need help with?

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

Here is what I have now. I'm having issues however. when I choose case 1 it works just fine. But if I choose case 2 it doesnt give my results. The If Not Object.Reference(B, Nothing) portion doesnt seem to work. It will ask the Case 2 questions but instead give me Case 1 results. I'd also like to figure out how to do a Case "r" if Readline is "r" and a Case "b" if Readline is "b"

 

Module Module1
   Structure Rectangle
       Private e As Boolean
       Shared ReadOnly Property Empty() As Rectangle
           Get
               Empty = New Rectangle
               Empty.e = True
           End Get
       End Property
       Public ReadOnly Property isEmpty() As Boolean
           Get
               Return Me.e
           End Get
       End Property
       Dim X
       Dim Y
   End Structure
   Structure Box
       Private e As Boolean
       Shared ReadOnly Property Empty() As Box
           Get
               Empty = New Box
               Empty.e = True
           End Get
       End Property
       Public ReadOnly Property isEmpty() As Boolean
           Get
               Return Me.e
           End Get
       End Property
       Dim X
       Dim Y
       Dim Z
   End Structure

   Sub Main()

       'Bit different solution  
       Dim R As Rectangle = Rectangle.Empty, B As Box = Box.Empty

       'INPUT  
       Console.Write("Would you like to create a Rectangle or a Box (r or b): ")
       Select Case CInt(Console.ReadLine)
           Case 1
               R = New Rectangle
               Console.Write("Please enter the Length: ")
               R.X = CInt(Console.ReadLine)
               Console.Write("Please enter the Width : ")
               R.Y = CInt(Console.ReadLine)
           Case 2
               B = New Box
               Console.Write("Please enter the Length: ")
               B.X = CInt(Console.ReadLine)
               Console.Write("Please enter the Width: ")
               B.Y = CInt(Console.ReadLine)
               Console.Write("Please enter the Height: ")
               B.Z = CInt(Console.ReadLine)
       End Select

       'OUTPUT  
       Console.ForegroundColor = ConsoleColor.Green
       Console.Write("Description of ")
       If (B.ToString.Equals(String.Empty) = True) Then 'Describe Rectangle  
           Console.WriteLine("Rectangle:")
           Console.WriteLine("Volume = {0:F}", R.X * R.Y)
       Else 'describe Box  
           Console.WriteLine("Box:")
           Console.WriteLine("Area = {0:F}", B.X * B.Y * B.Z)
       End If

       Console.ForegroundColor = ConsoleColor.White
       Console.WriteLine("Press escape to exit")
       Do : Loop Until Console.ReadKey().Key = ConsoleKey.Escape
   End Sub

End Module

  • Administrators
Posted

I would recommend putting the line

Option Strict On

at the top of your source file as it will highlight several potential problems and is good practice anyway.

 

Firstly though I am not sure how the

Select Case CInt(Console.ReadLine)

line works if the user enters a r or b to the question as neither will convert to an integer anyway - just use case "r" and case "b" in the select statements.

 

B.ToString will never evaluate to String.empty unless you deliberately override the ToString method - you would be better off checking if B.IsEmpty = true to decide based on how your code is currently organised.

 

In your question you mention a fragment of code - If Not Object.Reference(B, Nothing) but this doesn't appear in your sample anywhere however an easier way to check would be

If B Is Nothing Then

this will never be true in your case as both rectangle and box are structures and as such can never be Nothing.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...