| | |
using functions
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 6
Hello,
I just started laerning c# and I made this program that took the dimension of a box(house), and then use a function to get the volume of the box_house. One problem that I encountered was that I dont know how to make a string into an int. Here is a link to the code http://www.nomorepasting.com/paste.php?pasteID=33513.
If anyone can help that would be great.
Thanks
I just started laerning c# and I made this program that took the dimension of a box(house), and then use a function to get the volume of the box_house. One problem that I encountered was that I dont know how to make a string into an int. Here is a link to the code http://www.nomorepasting.com/paste.php?pasteID=33513.
If anyone can help that would be great.
Thanks
ReadLine returns a string, and you can use the Parse method of int to convert a string representing a valid integer into an int value:
C# Syntax (Toggle Plain Text)
using System; class main { public static void Main() { Console.Write("Enter an integer: "); string s = Console.ReadLine(); try { int n = int.Parse(s); n *= 2; Console.WriteLine("Times two: " + n); } catch (Exception) { Console.WriteLine("Invalid integer"); } Console.ReadKey(); // Pause } }
New members chased away this month: 4
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 6
OK, I am still getting a problem with the function, here is the updated code:
/* * Date: 3/5/2005 * house.cs */ using System; namespace BackGround_Process { class BackGround_Process { int house(int length, int width, int height) { int Dimension = 0; Dimension = length * width * height; return Dimension; } public static void Main() { int length, width, height; length = 0; width = 0; height = 0; Console.WriteLine("What are the dimensions of your house: "); Console.WriteLine("What is the length of your house? "); string l = Console.ReadLine(); length = int.Parse(l); Console.WriteLine("The lenght of your house is {0}. ", length); Console.WriteLine("What is the width of your house? "); string w = Console.ReadLine(); width = int.Parse(w); Console.WriteLine("The width of your house is {0}. ", width); Console.WriteLine("What is the height of your house? "); string h = Console.ReadLine(); height = int.Parse(h); Console.WriteLine("The height of your house is {0}. ", height); Console.Write("The volume of your house is: "); int house(length, width, height); } } }
First, house needs to be static unless you plan on calling it on an object. Second, any call to a function does not include the return type or the function call will be seen as a function declaration. Try this:
C# Syntax (Toggle Plain Text)
using System; namespace BackGround_Process { class BackGround_Process { static int house(int length, int width, int height) { return length * width * height; } public static void Main() { Console.Write("What is the length of your house? "); int length = int.Parse(Console.ReadLine()); Console.WriteLine("The lenght of your house is {0}. ", length); Console.Write("What is the width of your house? "); int width = int.Parse(Console.ReadLine()); Console.WriteLine("The width of your house is {0}. ", width); Console.Write("What is the height of your house? "); int height = int.Parse(Console.ReadLine()); Console.WriteLine("The height of your house is {0}. ", height); Console.Write("The volume of your house is: "); Console.WriteLine(house(length, width, height)); } } }
New members chased away this month: 4
![]() |
Similar Threads
- VB's Left, Right, Mid Functions in C++? (C++)
- User defined functions (C++)
- Double Linked Lists and Functions required (C++)
- How to write FNVAL functions (Java)
- I dont see any difference between these 2 functions, DO YOU? (C)
- access Digital Camera Functions (C++)
Other Threads in the C# Forum
- Previous Thread: Username / password using C#
- Next Thread: Geometric Art using Triangles (C#)
Views: 15206 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






