MessageBox.Show error in C# [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to compile this code but it gives me 2 errors about MessageBox.
It's a simple program with 3 variables. plus 2 Text boxes (box1, box2) and a button (btn).
I'm trying to add numbers in box1 with box2 and show the result as a variable named "outcome" in a Message Box.
Error 1
The best overloaded method match for
'System.Windows.MessageBox.Show(string)' has some invalid arguments
Error 2
Argument 1: cannot convert from 'method group' to 'string'
Here's the code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
int str1 = int.Parse(box1.Text);
int str2 = int.Parse(box2.Text);
int outcome;
outcome = 0;
outcome = str1 + str2;
MessageBox.Show(outcome.ToString);
}
}

Change
MessageBox.Show(outcome.ToString);
to
MessageBox.Show(outcome.ToString());
Method group means the method, think of it as a function pointer. You can't print the method ToString, you want to print the resulting string and you get that by evaluating the method with ().

Try this for the first exception it's a method you miss () it's not a property
MessageBox.Show(outcome.ToString());

Typo:
use () after ToString ----> outcome.ToString()

Related

C# using an IF statement to check if user enters correct DateTime date which is a birthday [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a Customer class, and I used the DatTime constructor and set the Date of Birth(bday).
In the main program I have an instance of the Customer class and would like to check if user enters the same birthday that was was originally set in the class.
public class Customer
{
public string Name;
public int PinNumber = 2321;
public double AccBalance = 250.00;
public DateTime bday = new DateTime(1875,05,22);
main program
person1.bday = Convert.ToDateTime(Console.ReadLine());
if (person1.bday == 1875,05,22)
{
Console.WriteLine("");
}
I get an error saying ''==' operator cannot be be applied to operands of type 'DateTime' and 'int'
You need to convert the three integers in your if statement to a DateTime:
if (person1.bday.equals(new DateTime(1875,05,22)) {
Console.WriteLine("");
}
When doing a comparison the data type on both end should be same
You should create a date object when doing comparison in your code
if (person1.bday == new DateTime(1875,5,22))
{
Console.WriteLine("");
}

C# Operator '+' cannot be applied to operand of type 'bool' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have searched for the answer to this question. I found similar sorts of questions, but those do not resolve the issue I have.
The last line of code is where I get the error: Operator '+' cannot be applied to operand of type 'bool' Here is the code:
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
employee oEmployee = new employee();
oEmployee.LoginEvent += new LoginEventHandler(oEmployee_LoginEvent);
oEmployee.Login(txtName.Text, txtPassword.Text);
}
void oEmployee_LoginEvent(string loginName, bool status)
{
MessageBox.Show("Login Status: ", + status); Here is where the issue is
}
Because of the comma, you are printing two values, the second of which ( + status ) is a unary prefixed + operator applied to a bool value. There is no such operator, so the error.
You can get rid of the comma ("Login Status: " + status.ToString()) in which case the + is a concatenation operator, or you can get rid the plus "Login Status: ", status.ToString()) and print two strings.

error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Trying to put an integer data from database(Linq to sql) into a label getting this error exception:
left-hand side of an assignment must be a variable property or
indexer
Code:
protected void Page_Load(object sender, EventArgs e)
{
DataClassesDataContext data = new DataClassesDataContext();
var visit = (from v in data.SeeSites where v.Date == todaydate select v).FirstOrDefault();
int seennow = visit.See; // On This line I can put data in seenow variable, no problem
Convert.ToInt64(lblSeeNow.Text) = visit.See; // exception error appears here
}
Try:
if (visit.See != null) {
lblSeeNow.Text = visit.See.ToString();
}
You cannot assign something to a function result. In your case lblSeeNow.Text is of type String hence usage of ToString(); method of your Int value.
You need to use
lblSeeNow.Text = visit.See.ToString();
Convert.ToInt64(lblSeeNow.Text) = visit.See;
As you mentioned, this is the issue.
Convert.ToInt64 is a method. But you're trying to save a value to it.
You can't.
Just do this
lblSeeNow.Text = visit.See.ToString();
I think you want
lblSeeNow.Text = visit.See.ToString();
You can't assign anything to
Convert.ToInt64(lblSeeNow.Text)
because it evaluates to a number.
Convert.ToInt64(lblSeeNow.Text) isn't a variable. It takes the value in lblSeeNow.Text and converts it to a long. There isn't a variable to store stuff in anymore.
You probably want this:
lblSeeeNow.Text = visit.See.ToString();
You should convert the integer to string, also add a check for being sure that visit is not null
lblSeeNow.Text = visit != null ? visit.See.ToString() : string.Empty

Two errors in a simple code, Invalid token '=' , a 'field' but is used like a 'type' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I got two errors in my simple code. I googled but cann't find the answer. Seems something related with counstructor?
Here are errors:
Invalid token '=' in class, struct, or interface member declaration
'WindowsFormsApplication1.Form1.a' is a 'field' but is used like a 'type'
below is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int a = 1;
a = 2; //I got errors after i wrote this line, cannot assign value?
private void button1_Click(object sender, EventArgs e)
{
int a = 1;
a = 10;
decimal myDecimalValue = 10;
int myIntValue = (int) myDecimalValue;
MessageBox.Show(myDecimalValue.ToString() );
}
}
Seriously, can you tell me the reason for negative?
My question is not clear? or forbidden to ask certain questions?? Before I asked this question, I gooled a lot web pages but no answer for it.
You can only use assignment in a class definition if it is part of member initialization, as in int a = 1;, or in a method/constructor/property body, so if you want to assign a value right after initializing it, you'd have to put it in the constructor:
public Form1()
{
a = 2;
InitializeComponent();
}
I'm not positive, but it looks like that line is not inside a method. If you are trying set it as 2, would it be beneficial to initialize it as 2?
Int a = 2

"A namespace cannot directly contain members such as fields or methods" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

Categories

Resources