Function won't return any value [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I have a problem that I think might be really easy to solve but since I'm a C# noob I can't understand what I'm doing wrong. I have two functions: addValues() and showMessage(). My problem is in addValues(). I have two MessageBox that show exactly what they're supposed to show, but in the showMessage() function the values are not being received, it always tells me that the day and dias.Count are 0. What am I doing wrong? Thank you!
On Form1:
public List<Despesas> dias = new List<Despesas>();
public struct Despesas
{
public double transportes;
public double alimentacao;
public double vestuario;
public double agua;
public double luz;
public double educacao;
}
On Class Management:
class management : Form1
{
int day=0;
public double addValues(double transportes, double alimentacao)
{
Despesas dia = new Despesas();
try
{
dia.transportes = transportes;
dia.agua = agua;
dias.Add(dia);
}
catch
{
MessageBox.Show("Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show("Count " + dias.Count);
day++;
MessageBox.Show("" + day);
return day;
}
public void showMessage()
{
MessageBox.Show("Day " + day);
MessageBox.Show("Count: " " + dias.Count);
for (int i = 0; i < day; i++)
{
MessageBox.Show("Agua: " + dias[i].agua + "\nTransportes: " + dias[i].transportes);
}
}

In the comments, you mention that you actually have two instances of the management class.
Changes to one instance of an object do not propagate to other instances of that object (unless it was modified on a static member, but thats a bit different).
This holds true even if you modify a base class member, as your code does. This is because instantiation of a derived class also instantiates a new base class object.
The solution is to just use one management object instance, and pass it around as needed. You do this just like any other type:
public void Foo(management myClass)
{
...
}
A few other notes:
management is not a very good name for a class, as its not very descriptive. Also, class names in C# should be PascalCase, so it should be Management
Inheritance is probably not the right relationship between management and Form1. Is management really a "type of" or "is a" Form1?

Related

How to pass information from one void to another? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm new to coding and I have a simple problem that I cannot find the solution anywhere for.
All I would need is to pass information from one void to another.
I have tried to find a solution but many solutions go on for passing information from class to class and script to script, and that's not exactly what I need and none have worked.
The Code right now is something like this:
public static void Main(string[] args){
GetVoid.DoCheck();
}
public void GenerateInformation(){
DateTime StoredDateTime;
//Code that does something
StoredDateTime = //Date generated from the rest of the code not included
}
public void DoCheck(){
DateTime CheckedDateTime;
GenerateInformation();
CheckedDateTime = StoredDateTime;
//Rest of the code for that void
}
My end goal is for the date so simply be stored in the other DateTime but I get the error
The name 'StoredDateTime' does not exist in the current context.
As far as I know this is because it does not know what StoredDateTime is, and to fix this I would need to pass on the information from one void to the other.
But how would I do that?
void mean:
not valid or legally binding.
completely empty.
So, you cannot expect from void method to pass you result. You need to change your method signature and implement return.
public static DateTime GetDate()
{
var dateVal = DateTime.Now;
return dateVal;
}
then
public static void Main(string[] args){
var dateRetult = GetDate();
Console.WriteLine(dateResult);
}

Capturing return value from helper class c# [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 5 years ago.
Improve this question
I've spent the better part of an hour reading over linked items on Google about this topic; however, maybe it just is not sinking in. What do I need to do to capture the return value from a helper class in C#? This is the code:
protected void Page_Load(object sender, EventArgs e)
{
HelperClass.Calculate(a, b);
}
public static string Calculate(string a, string b)
{
string value = string.Empty;
// inner code workings
return value;
}
I know I'm missing something but I cannot for the life of me determine what. Any help would be appreciated! Thank you!
var x = HelperClass.Calculate(a, b);
Just like any other function, which returns a value?
protected void Page_Load(object sender, EventArgs e)
{
string value = HelperClass.Calculate(a, b);
}
public static string Calculate(string a, string b)
{
string value = string.Empty;
// inner code workings
return value;
}

Is there any difference in Console.WriteLine("{0}, var); and MessageBox.Show("{0},var) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is there any error in the following statement?
var str = "Hello how are you.";
MessageBox.Show("{0}",str);
My problem was in the following code where I was not getting it correctly. The code below is part of learning process. In the MessageBox the 'designation' variable is not coming as I stated above! I originally posted it due to my ignorance that when I test something I can use the MessageBox like Console.WriteLine().
abstract class Employee //Abstract class
{
public virtual void WhichCoEmployee()
{
MessageBox.Show("I am employed in XYZ Corporation as its {0}", designation); //My problem was in this line.
//designation varaiable was not received in
//placeholder {0} for display in MessageBox.Show.
}
public void Designation(string desig)
{
designation = desig;
}
public string designation { get; set; }
}
class CEO : Employee //Inheritance
{
public void Name()
{
MessageBox.Show("My name is Satheeshkumar K");
}
}
private void button2_Click(object sender, EventArgs e)
{
CEO ceo = new CEO(); //Initializing the CEO class.
ceo.Name();
ceo.Designation("CEO");
ceo.WhichCoEmployee();
}
Subsequenly I have rectified the MessageBox.Show problem by changing the
the Message Box code.
MessageBox.Show("I am employed in XYZ Corporation as its " + designation);
That worked fine. I have nothing more to say on this. Being a member of the stackoverflow has really helped me to learn some things due to the help from other members.
EDIT:
Simply change
MessageBox.Show("I am employed in XYZ Corporation as its {0}", designation);
to
MessageBox.Show("I am employed in XYZ Corporation as its " + designation);
BTW, you need to call this method somewhere for the code to run!
No. Your two lines seems to be perfectly correct according to the syntax.
But it seems that what you are trying to achieve is something different than what you have done.. Here's the thing: You just called this overloaded version of Show method of MessageBox class, which accepts two strings.
public static DialogResult Show(string text, string caption);
Hence in your case, a message box with "{0}" text and "Hello how are you." caption will be shown.
However, Console.WriteLine("{0}", var); is completely different. It is used to print output to the console. It also has many overloaded varieties, and in this case, it will replace {0} with the value of var variable. Hence, "Hello how are you." will be outputted to the Console.

Getting CS1513 } Expected, but all braces are there [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'm a novice programmer, so forgive me if this is something obvious. I've checked all the braces and find matching pairs in all cases here. The code compiles fine without this snippet. Any ideas?
protected bool Bullish(int ConsecutiveBullishBars)
{
private int howmanybars = ConsecutiveBullishBars - 1;
private bool IsMarketBullish = false;
while (howmanybars >= 0)
{
if (Close[howmanybars] > KeltnerChannel(Offset, Period)[howmanybars])
{
IsMarketBullish = true;
}
else
{
IsMarketBullish = false;
}
howmanybars--;
}
return IsMarketBullish;
}
Here is the full code: http://pastebin.com/aHbzqKbw
It doesn't make any sense to mark local method variables as private. That is what is causing your errors.
Why the compiler is giving you an } expected error, I'm not sure. I'm guessing that the compiler is assuming that private int howmanybars is being interpreted as a private instance field definition, which cannot be declared inside a method. So it is telling you that it expects the Bullish method to end before the declaration.
protected bool Bullish(int ConsecutiveBullishBars)
{
int howmanybars = ConsecutiveBullishBars - 1;
bool IsMarketBullish = false;
while (howmanybars >= 0)
{
if (Close[howmanybars] > KeltnerChannel(Offset, Period)[howmanybars])
{
IsMarketBullish = true;
}
else
{
IsMarketBullish = false;
}
howmanybars--;
}
return IsMarketBullish;
}

Calling method from within the main in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have practised in C++ . It is a solution for 8-Queens that outputs all the 92 possible solutions.
C++ code example: What makes this loop so many times?
Then I have written it in C#. Here it is, But I have an error at the very end.
int[,] state = new int[8, 8];
solve_state(state, 0); // Error: an object reference is required for non-//static field,method
}
}
}
}
Try declaring the solve_state method as static.
// ↓
private static void solve_state(int[,] state, int count)
{
// method implementation here
}
It looks like you declared the solve_state as an instance (i.e. non-static) method. However, you cannot call an instance method without a reference to an instance of the parent class. Instead, make the solve_state method static, like this:
public class Program
{
public static void Main(string[] args)
{
...
int[,] state = new int[8, 8];
solve_state(state, 0);
...
}
private static void solve_state(int[,] state, int x)
{
...
}
}
Further Reading
static (C# Reference)

Categories

Resources