I'm new to programming and am taking a C# class. I am getting compiler error CS1001 when I try to write this program.
I read the Compiler Error description (link below), but I'm really not getting it. What am I doing wrong?
http://msdn.microsoft.com/en-us/library/b839hwk4.aspx
Here is my source code:
using System;
public class InputMethodDemoTwo
{
public static void Main()
{
int first, second;
InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0}", first);
Console.WriteLine("and second is {0}", second);
}
public static void InputMethod(out first, out second)
// The error is citing the line above this note.
{
one = DataEntry("first");
two = DataEntry("second");
}
public static void DataEntry(out int one, out int two)
{
string s1, s2;
Console.Write("Enter first integer ");
s1 = Console.ReadLine();
Console.Write("Enter second integer ");
s2 = Console.ReadLine();
one = Convert.ToInt32(s1);
two = Convert.ToInt32(s2);
}
}
According to the instructions, I'm supposed to have a method b (InputData) which pulls statements from method c (DataEntry)... Here are the instructions:
The InputMethod()in the InputMethodDemo program in Figure 6-24 contains repetitive
code that prompts the user and retrieves integer values. Rewrite the program so the
InputMethod()calls another method to do the work. The rewritten InputMethod()
will need to contain only two statements:
one = DataEntry("first");
two = DataEntry("second");
Save the new program as InputMethodDemo2.cs."
The InputMethodDemo they are referring to is the same program, except that it calls only one method (the InputMethod) instead of two.
The text I referred to above is "Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell"
Any advice/ help would be greatly appreciated.
This is what you are expected to do:
using System;
public class InputMethodDemoTwo
{
public static void Main()
{
int first, second;
InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0}", first);
Console.WriteLine("and second is {0}", second);
Console.ReadLine();
}
public static void InputMethod(out int first, out int second)
//Data type was missing here
{
first = DataEntry("first");
second = DataEntry("second");
}
public static int DataEntry(string method)
//Parameter to DataEntry should be string
{
int result = 0;
if (method.Equals("first"))
{
Console.Write("Enter first integer ");
Int32.TryParse(Console.ReadLine(), out result);
}
else if (method.Equals("second"))
{
Console.Write("Enter second integer ");
Int32.TryParse(Console.ReadLine(), out result);
}
return result;
}
}
Change
public static void InputMethod(out first, out second)
{
one = DataEntry("first");
two = DataEntry("second");
}
to
public static void InputMethod(out DataEntry first, out DataEntry second)
{
first = DataEntry("first");
second = DataEntry("second");
}
You haven't provided the type of the arguments. Also, your arguments are called first and second, not one and two.
Related
I have been trying to make a simple program to check a person's birthday and if their birthday is the same as their pet, for it to be printed out on the console, or if it's not the same to type out no valid input. I don't know why but the variables are not being taken in saying they aren't properly added or it just says they need a get/set. If anyone could show and explain how it should be done it would be like really awesome and cool and amazing. Here's the code:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
Human human = new Human();
human.name();
human.age();
human.id();
human.birthday();
Robot robot = new Robot();
robot.id();
robot.model();
Pet pet = new Pet();
pet.name();
pet.birthday();
Program program = new Program();
program.BirthdayCheck("","");
}
public static void BirthdayCheck(string userResponse1, string userResponse2)
{
if (userResponse1 == userResponse2)
{
Console.WriteLine("" + userResponse1);
}
else
{
Console.WriteLine("No matching birthday");
}
}
interface IHuman
{
void name();
void age();
void id();
void birthday();
}
interface IRobot
{
void model();
void id();
}
interface IPet
{
void name();
void birthday();
}
class Human : IHuman
{
public string userResponse2;
public string Birthday
{
get { return userResponse2; }
set { userResponse2 = value; }
}
public void name()
{
Console.WriteLine("Citizen name: ");
Console.ReadLine();
}
public void age()
{
Console.WriteLine("Citizen's age: ");
Console.ReadLine();
}
public void id()
{
Console.WriteLine("Citizen's id: ");
Console.ReadLine();
}
public void birthday()
{
Console.WriteLine("Citizen's birthday: ");
userResponse2 = Console.ReadLine();
}
}
class Robot : IRobot
{
public void model()
{
Console.WriteLine("Enter Robot Model: ");
Console.ReadLine();
}
public void id()
{
Console.WriteLine("Enter Robot Id: ");
Console.ReadLine();
}
}
class Pet : IPet
{
public string userResponse1;
public string Birthday
{
get { return userResponse1; }
set { userResponse1 = value; }
}
public void name()
{
Console.WriteLine("Enter pet name: ");
Console.ReadLine();
}
public void birthday()
{
Console.WriteLine("Enter pet birthday: ");
userResponse1 = Console.ReadLine();
}
}
}
}
I had no issues with the interfaces themselves and it does that the information, it just doesn't want to compare the two between them. I don't know if it's just a logical or syntax error, but hopefully, it's at least partially correct. Finished up some syntax errors but the issues still remain. The error message that appears is "Member Program.BitrhdayCheck(string, string) cannot be accessed with an instance reference; qualify it with a type name instead."
As commented above, this looks like a really complicated program to compare something so simple. Assuming that's your choice, I think these two lines in the main method are not quite correct:
Program program = new Program();
program.BirthdayCheck("","");
you really should be calling the BirthdayCheck method directly.
Another issue is, no value is really being passed to this method. So not sure exactly what is it you're comparing.
Here's a fix to your main method that can solve your issue:
static void Main(string[] args)
{
Human human = new Human();
human.name();
human.age();
human.id();
human.birthday();
Robot robot = new Robot();
robot.id();
robot.model();
Pet pet = new Pet();
pet.name();
pet.birthday();
BirthdayCheck(pet.Birthday, human.Birthday); // birthdaycheck is method of the program class, hence does not need to be invoked with a new program object.
// Additionally, you need to pass an actual birthday value to compare instead of blank strings.
Console.ReadKey(); // this step ensures the console does not close after the birthday check
}
public static void BirthdayCheck(string userResponse1, string userResponse2)
{
if (userResponse1 == userResponse2)
{
Console.WriteLine("" + userResponse1);
}
else
{
Console.WriteLine("No matching birthday");
}
}
Here's an output sample of the program after the above mentioned changes:
Citizen name:
w
Citizen's age:
12
Citizen's id:
1
Citizen's birthday:
10/10/1990
Enter Robot Id:
2
Enter Robot Model:
r2d2
Enter pet name:
p2v2
Enter pet birthday:
11/11/1991
No matching birthday
userResponse1 and userResponse2 are out of scope from the BirthdayCheck method. you need to either pass in references to the human/pet objects(like human.userResponse2 == pet.userResponse1) or pass the birthdays themselves in to compare.
Is there a reason why you made this so complicated?
You made 3 classes and 3 interfaces and how do you expect them to work together?
You use Console.WriteLine(); without variables.
Does your code need to make sense? If its just for practice, you should
slow down and try something more simple, like 2 classes and then compare,
then 2 classes and 1 interface and then compare...
you mean
public string BirthdayCheck()
{
you were missing () from the method declaration
I have been working on some TDD Kata and have it nearly finished but was running into the CS1519 error. VS is telling me where the error is occurring but not sure how to go about fixing the solution. This is my first crack at doing TDD of any kind and looking for some general pointers as well.
I've been on stack overflow looking at other threads about talking about CS1519 but none of them (that I could find) seem to answer my exact question. Also, have checked out Stack Exchange for specific TDD Kata questions but needing more explanation.
using System;
using System.Linq;
namespace ChallengeCode
{
public class Calculator
{
public int Main(string number)
{
TestUnits();
if (string.IsNullOrEmpty(number))
return 0;
var numberArray = number.Replace('\n', ',').Split(',');
numberArray = NewMethod(numberArray);
NonNegValidate(numberArray);
var numberArrayInt = numberArray.Select(x => int.Parse(x)).ToArray();
return numberArrayInt.Where(x => x <= 1000).Sum(x => x);
}
private static void TestUnits()
{
throw new NotImplementedException();
}
internal static double Add(string number)
{
throw new NotImplementedException();
}
private static string[] NewMethod(string[] numberArray)
{
if (numberArray[0].StartsWith("//"))
{
var delimiter = Convert.ToChar(numberArray[0].Remove(0, 2).Distinct());
foreach (var delimiters in numberArray)
{
numberArray[1] = numberArray[1].Replace(delimiter, ',');
}
numberArray = numberArray[1].Split(',');
}
return numberArray;
}
private static void NonNegValidate(string[] numberArray)
{
if (numberArray.Any(x => int.Parse(x) < 0))
throw new Exception($"negatives not allowed {string.Join(" ", numberArray.Where(x => int.Parse(x) < 0))}");
}
}
}
Here is the code that I am using for Program.cs
using NUnit.Framework;
using System;
namespace ChallengeCode
{
class TestUnits
{
public void Add_Number_ReturnsSum(int expected, string number)
{
Assert.AreEqual(expected, Calculator.Add(number));
}
public void Add_NegNumber_Throw_An_Exception()
{
const string number = "1\n-2,-3";
var exception = Assert.Throws<Exception>(() => Calculator.Add(number));
Assert.AreEqual("negatives not allowed -2 -3", exception.Message);
}
}
}
More Details:
Screenshot of Code
I think your entry point is expecting an array instead of a single string.
I changed your code to this and it compiled. This is using the first element of your array... but has separate exceptions.
static int Main(string[] number)
{
TestUnits();
if (string.IsNullOrEmpty(number[0]))
return 0;
var numberArray = number[0].Replace('\n', ',')
.Split(',');
}
it's throwing more exceptions for being outside the bounds of this array because I'm not aware of what context this program is being used in. I assume you have another program feeding the string parameter into this one?
The Main method can be declared with or without a string[] parameter that contains command-line arguments.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/
I have class: class_getanswer and class: class_performaction
in class_getanswer i have method:
class class_getanswer
{
static public int capacity()
{
Random block = new Random();
int beta = block.Next(1, 8);
return beta;
}
}
in class with method Main i want that answer to be in function if:
class class_performaction
{
static void Main()
{
int value = 5;
if(class_getanswer.capacity() < value)
{
Console.WriteLine("bla bla bla");
}
}
}
but i get that "if" doesn't exist in this context, why?
There is screenshot of kinda full programm but in different language (lithuanian): https://www.dropbox.com/s/jggwgt738vltawx/functionswt.PNG?dl=0
Please check the case of, if statement. I believe you wrote If(with a capital 'I'), change it to small case 'i'.
I want my small math program to look really sleek, and by this I mean under the Main method I have the following methods:
Greet()
UserInput1()
UserInput2()
Result()
In Greet() I just say "HI", in UserInput1() I want to collect the first number, in UserInput2() I want to collect the second number, and in Result() I want to print the result of UserInput1 + UserInput2. I can collect the number in UserInput 1 and 2 but I can’t seem to send them to Result() without assigning values to them under the Main() function.
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Greet();
firstNumber();
secondNumber();
result(firstNumber, secondNumber);
Console.ReadKey();
}
public static void Greet()
{
Console.WriteLine("Hello, pls insert two numbers");
}
public static int firstNumber()
{
int num01 = Convert.ToInt32(Console.ReadLine());
return num01;
}
public static int secondNumber()
{
int num02 = Convert.ToInt32(Console.ReadLine());
return num02;
}
public static void result( int num01, int num02)
{
Console.WriteLine(num01 + num02);
}
}
}
change this:
result(firstNumber, secondNumber);
to this:
result(firstNumber(), secondNumber());
and remove the calls to the 2 methods in the two lines above.
To call a method without parameters, you need the parentheses without content.
Cannot convert from method group to int
This error message occurs when you attempt to take a method (without invocation) and pass it as a type. The result method is expecting two parameters of type int, but you're attempting to pass it the method, rather than the result of the method invocation.
You need to store the results in a variable, or invoke the methods with the ():
Like this:
static void Main(string[] args)
{
Greet();
var first = firstNumber();
var second = secondNumber();
result(first , second );
Console.ReadKey();
}
or this:
static void Main(string[] args)
{
Greet();
result(firstNumber(), secondNumber());
Console.ReadKey();
}
Call the method like the following, So that the method result will be called with output from the firstNumber() and secondNumber() as well :
result(firstNumber(),secondNumber());
Few more suggestions:
Make the method Greet() to a re-usable one by passing appropriate message and then display it. so that you can use the same for all display operations. the signature of the method will be:
public static void Greet(string message)
{
Console.WriteLine(message);
}
The method Convert.ToInt32() will convert the given input to an integer value only if the input is convertible. else it will throws FormatException. So i prefer you to use int.TryParse for this purpose. Which will help you to determine whether the conversion is success or not. so the Method signature for firstNumber() will be like the following:
public static int firstNumber()
{
int num01=0;
if(!int.TryParse(Console.ReadLine(),out num01))
{
Greet("Invalid input");
}
return num01;
}
Hope that you will change the secondNumber() as well
I am a very very very basic programmer just starting off. I have followed a few tutorials and I am now trying to branch out into my own stuff but I keep getting the same Error that I cant rectify.
static void Main(string[] args)
{
///Call Method
OutWelcome();
///Return the name from getname method
Inputname = getname();
Console.WriteLine(" {0}", Inputname);
}
public static void OutWelcome()
{
Console.WriteLine("Welcome");
}
public static string getname()
{
///Declare Variable
string Inputname;
///Prompt User
Console.WriteLine("Enter your Name: ");
///Get name from user keyboard
Inputname = Console.ReadLine();
///Returns name
return Inputname;
}
}
I keep getting the following error "The name 'Inputname' does not exist in the current context. Line 18, 17 and 19
I know the answer will be so simple its n00b worthy, but I am new and we all have to start somewhere.
Thank you in advance
Sean
Please use the below code :
static void Main(string[] args)
{
///Call Method
OutWelcome();
///Return the name from getname method
string Inputname = getname();
Console.WriteLine(" {0}", Inputname);
}