Asked
4 years, 5 months ago
Viewed
5k times
I am working on a project that creates a simple perimeter and area calculator based on the values the user inputs. (For finding window perimeter and glass area). However, I’m stuck with 4 errors… all of which are CS0103. Can anyone help me fix these errors or clean up my code. I’m trying to separate everything into methods so I would like to keep the code in that general format.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
double totalLength, totalWidth, windowPerimeter, glassArea;
//display instructions
DisplayInstructions();
// ask for width
totalWidth = AskDimension();
//ask for lenght
totalLength = AskDimension();
// calculate window Perimeter
windowPerimeter = (2 * totalWidth) * (2 * totalLength);
//calculate the area of the window & display output
glassArea = totalWidth * totalLength;
//calculate and display outputs
Console.WriteLine("the lenth of the wood is " + windowPerimeter + " feet");
Console.WriteLine("the area of the glass is " + glassArea + " square feet");
Console.ReadKey();
Console.ReadKey();
}
//display instructions
public static void DisplayInstructions()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("This app will help you calculate the amount of wood and glass needed for your new windows!");
Console.WriteLine(" ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("*Note* Please enter a height/width value between 0.01 - 100, all other values will cause a system error.");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(" ");
}
//ask for width
public static double AskDimension()
{
double totalWidth;
const double MAX_HEIGHT = 100.0;
const double MIN_Height = 0.01;
string widthString;
Console.WriteLine("please enter your height of the window");
widthString = Console.ReadLine();
totalWidth = double.Parse(widthString);
if (totalWidth < MIN_Height)
{
Console.WriteLine("you enter vaule less than min width n" + "using minimum one");
totalWidth = MIN_Height;
}
if (totalWidth > MAX_HEIGHT)
{
Console.WriteLine("you enter value grater than Max heightn" + "using maximum one");
totalWidth = MAX_HEIGHT;
}
return AskDimension();
}
//ask for height
public static double AskDimension(string dimension)
{
double totalLength;
const double MAX_HEIGHT = 100.0;
const double MIN_Height = 0.01;
string heightString;
Console.WriteLine("please enter your height of the window");
heightString = Console.ReadLine();
totalLength = double.Parse(heightString);
if (totalLength < MIN_Height)
{
Console.WriteLine("you enter vaule less than min width n" + "using minimum one");
totalLength = MIN_Height;
}
if (totalLength > MAX_HEIGHT)
{
Console.WriteLine("you enter value grater than Max heightn" + "using maximum one");
totalLength = MAX_HEIGHT;
}
return AskDimension();
}
//calculate and display outputs
public static double AskDimesnion(string windowPerimeter,
string glassArea,
string widthString,
string heightString)
{
windowPerimeter = 2 * (totalWidth + totalLength);
glassArea = (totalWidth * totalLength);
Console.WriteLine("the lenth of the wood is " + windowPerimeter + " feet");
Console.WriteLine("the area of the glass is " + glassArea + " square feet");
Console.ReadKey();
return AskDimension();
}
}
}
Screenshot of errors in the method:
dymanoid
14.7k4 gold badges36 silver badges64 bronze badges
asked Dec 11, 2018 at 16:30
7
Your totalWidth
variable is not defined anywhere in your scope. It should be defined somewhere. Depending on where you exactly want to define it, you can do it internally in your AskDimension method or more globally. It seem by your logic it should be an input parameter of your method. Also you have other errors in your code. Your method is called AskDimesnion but you are calling it in your code by AskDimension (hopefully the correct method name…)
For variable scope, you can check Microsoft’s own documentation
answered Dec 11, 2018 at 16:36
Alfredo A.Alfredo A.
1,6873 gold badges30 silver badges43 bronze badges
1
The initial problem was that your variables weren’t class scoped and also the calculations seem a bit crazy to me. But from pasting your code in to my editor I’ve had a quick tidy up and come out with the following:
using System;
namespace TestConsoleApplication
{
class Program
{
static double _totalLength, _totalWidth, _windowPerimeter, _glassArea;
static void Main(string[] args)
{
DisplayInstructions();
_totalWidth = AskDimension("Height");
_totalLength = AskDimension("Width");
_windowPerimeter = (2 * _totalWidth) + (2 * _totalLength);
_glassArea = _totalWidth * _totalLength;
Console.WriteLine("The length of the wood is " + _windowPerimeter + " feet");
Console.WriteLine("The area of the glass is " + _glassArea + " square feet");
Console.ReadKey();
}
private static void DisplayInstructions()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("This app will help you calculate the amount of wood and glass needed for your new windows!");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("*Note* Please enter a height/width value between 0.01 - 100, all other values will cause a system error.");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
}
private static double AskDimension(string dimensionType)
{
const double maxDimension = 100.0;
const double minDimension = 0.01;
Console.WriteLine($"Please enter your {dimensionType} of the window");
var dimensionString = Console.ReadLine();
var dimension = double.Parse(dimensionString);
if (dimension < minDimension)
{
DisplayDimensionErrorAndExit("Min");
}
else if (dimension > maxDimension)
{
DisplayDimensionErrorAndExit("Max");
}
return dimension;
}
private static void DisplayDimensionErrorAndExit(string dimensionToShow)
{
Console.WriteLine($"You entered a value less than {dimensionToShow} dimension");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
Environment.Exit(0);
}
}
}
If a user enters invalid input the program will terminate, apart from that the calculations work and the correct output displayed.
If you’ve got any questions about it at all just ask me
answered Dec 11, 2018 at 17:01
CoopsCoops
2613 silver badges7 bronze badges
2
- Remove From My Forums
-
Вопрос
-
Hi,
I’m hoping this is going to be an easy one for someone. I’m getting the following error when compiling a simple program in Visual Studio 2015 (Community Edition)
Error CS0103 The name ‘File’ does not exist in the current context.
Now, I can get this same code to compile successfully in Visual Studio 2013 (Express) Desktop
The line I suspect the compiler is choking on is this one:
StreamReader reader = new StreamReader(File.OpenRead(@»C:UsersxxxxxxxDesktoptest.csv»));
So the question being; why do I get this error in Visual Studio 2015 (Community Edition) and not in Visual Studio 2013 (Express) Desktop. The code is exactly the same and has the following at the top:
using System.IO;
Is there a setting in Visual Studio 2015 (Community Edition) that would need changing ?
Any help with this would be greatly appreciated.
Thanks in adv.
Ответы
-
Thanks for your reply. I tried doing that before I posted to this forum, but the error remains in Visual Studio 2015 (Community Edition).
I’ve managed to work this out.
I edited the project.json file and removed the following from the frameworks:
«dnxcore50»: {
«dependencies»: {
«System.Collections»: «4.0.10-beta-23019»,
«System.Console»: «4.0.0-beta-23019»,
«System.Linq»: «4.0.0-beta-23019»,
«System.Threading»: «4.0.10-beta-23019»,
«Microsoft.CSharp»: «4.0.0-beta-23019»
}
}and just left the following:
«dnx451»: { }
Now the project compiles with no errors.
Thanks for you time.
-
Изменено
22 августа 2015 г. 10:55
-
Помечено в качестве ответа
levens2m
22 августа 2015 г. 10:56
-
Изменено
Permalink
Cannot retrieve contributors at this time
description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
Compiler Error CS0103 |
Compiler Error CS0103 |
07/20/2015 |
CS0103 |
CS0103 |
fd1f2104-a945-4dba-8137-8ef869826062 |
Compiler Error CS0103
The name ‘identifier’ does not exist in the current context
An attempt was made to use a name that does not exist in the class, namespace, or scope. Check the spelling of the name and check your using directives and assembly references to make sure that the name that you are trying to use is available.
This error frequently occurs if you declare a variable in a loop or a try
or if
block and then attempt to access it from an enclosing code block or a separate code block, as shown in the following example:
[!NOTE]
This error may also be presented when missing thegreater than
symbol in the operator=>
in an expression lambda. For more information, see expression lambdas.
using System; class MyClass1 { public static void Main() { try { // The following declaration is only available inside the try block. var conn = new MyClass1(); } catch (Exception e) { // The following expression causes error CS0103, because variable // conn only exists in the try block. if (conn != null) Console.WriteLine("{0}", e); } } }
The following example resolves the error:
using System; class MyClass2 { public static void Main() { // To resolve the error in the example, the first step is to // move the declaration of conn out of the try block. The following // declaration is available throughout the Main method. MyClass2 conn = null; try { // Inside the try block, use the conn variable that you declared // previously. conn = new MyClass2(); } catch (Exception e) { // The following expression no longer causes an error, because // the declaration of conn is in scope. if (conn != null) Console.WriteLine("{0}", e); } } }
C# Compiler Error
CS0103 – The name ‘identifier’ does not exist in the current context
Reason for the Error
You will usually receive this error when you are trying to use a identifier name in C# that doesnot exist with-in the current scope. One of the simple example of this error is when you declare a variable with-in a try block and try to access it in the catch block.
In the below example, the variable i is declared inside the try block, so its scope is restricted to the try block. When you try accessing it in the catch block is when you will receive the error.
using System; public class DeveloperPublish { public static void Main() { try { int i = 1; int j = 0; int result = i / j; } catch(DivideByZeroException) { string message = i + " is divivided by zero"; Console.WriteLine(message); } } }
Error CS0103 The name ‘i’ does not exist in the current context ConsoleApp1 C:UsersSenthilsourcereposConsoleApp1ConsoleApp1Program.cs 16 Active
Solution
To fix the error move the declaration of the identifier that is causing the error to a higher scope so that it is available when it is accessed.
For example, to fix the error in the above code snippet, we just need to move the declaration of the identifier outside of the try block.
using System; public class DeveloperPublish { public static void Main() { int i = 1; try { int j = 0; int result = i / j; } catch(DivideByZeroException) { string message = i + " is divivided by zero"; Console.WriteLine(message); } } }
UPDATE * My error had to do with the computer I was using and not being part of a FIPS validated algorithm. That error only appeared after I closed Visual Studio and tried to compile the default blank WPF form. I did the exact same thing on a personal computer and it compiled as expected. *
I’m familiar with creating windows form using C#. I looked into methods to change the look of my forms, similar to using skins, and was told that it would be easier if I I used WPF….ok.
In an effort to become familiar with WPF, I picked up a book, «MASTERING_WINDOWS_PRESENTATION_FOUNDATION» and it was slow moving with a discussion on MVVM and data binding (new topics to me). I felt the I could learn the difference between WPF and Windows Forms much faster if I first tried to create a simple WPF application. Then, as I read, I could see how something done in a very familiar way using Windows Forms, is done using WPF.
Unfortunately, I’m stuck right out the box!
Using VS2017, I created a new WPF App (.NET Framework) I then added a text box and a button. I created a name for both as this does not appear to be automatically created like with Windows Forms. I then double click on the button and a method block is created in the MainWindow.xaml.cs file. I proceed to add text to the textbox
(txtbox_1.Text="Hello;"
).
I noticed a few things:
1.) The InitializeComponent();
call in the MainWindow()
method is underlined and corresponds to the CS0103 Error
2.) Intellisense did not recognize the textbox. I typed out the full name and it created an error when I was done. (Same CS0103 Error)
I looked through stackoverflow but found articles about Xamarin. I’ve heard of this as a way to write code for Android but do not know how it relates to what I’m trying to do.
What am I missing?
Here is my XAML file:
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="btn_browse" Content="Button" HorizontalAlignment="Left" Margin="524,91,0,0" VerticalAlignment="Top" Width="75"/>
<TextBlock x:Name="txtbox_1" HorizontalAlignment="Left" Margin="134,93,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="20" Width="363"/>
</Grid>
</Window>
And here is my MainWindow.xaml.cs file:
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void SimpleMethod()
{
txtbox_1.Text = "Hello";
}
}
}