process is termianted due to stack overflow exception [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 2 months ago.
Improve this question
trying to run a function that would remove a character from a list of lists but getting a stackoverflow exception
in the program
public static void removeChar(World World)
{
Console.Clear();
Console.WriteLine("what character would you like to remove");
string cName = Console.ReadLine();
World.RemoveChar(cName);
}
in the class world
public void RemoveChar(string cName)
{
RemoveChar(cName);
}
in the class party
public void RemoveChar(string cName)
{
Node<Character> temp = Characters;
while (!temp.HasNext())
{
if (temp.GetNext().GetValue().getName() == cName)
{
temp.SetNext(temp.GetNext().GetNext());
}
temp = temp.GetNext();
}
if (temp.GetValue().getName() == cName)
{
temp = null;
}
}
when trying to remove a chracter im getting a stack overflow error
the error code im getting
side note : this is not a syntax error but an error in cmd incase it isnt clear
the properties of the class world
private string name;
private Node<Party> Partylist;
the properties of the class party
private string pName;
private Node<Character> Characters;

World.RemoveChar() seemingly by your example anyway.
Is just calling itself resulting in an infinite callstack until it overflows.

Related

Why is the next Line of Code Not Executing C#? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 months ago.
Improve this question
I am new to C#. The next line of code is not executing in my small program and I am unable to figure out why.
Main method
static void Main(string[] args)
{
Console.WriteLine("Loan Approval Software");
Customer inputCustomer = new Customer();
inputCustomer.Name = Console.ReadLine();
Console.WriteLine($"Hello {inputCustomer.Name}");
}
Class
public class Customer
{
// property
public string? Name { get; set; }
}
I just ran your code and it worked fine.
Please note this line: inputCustomer.Name = Console.ReadLine();
Will halt code execution until you enter in a name to the CLI.
You can see below it worked just fine. If you are getting some kind of other error or have questions about a specific line feel free to comment, but so far code is running as it should.
EDIT FOR DEMO
static void Main(string[] args)
{
while(true)
{
Console.WriteLine("Loan Approval Software");
Customer inputCustomer = new Customer();
inputCustomer.Name = Console.ReadLine();
Console.WriteLine($"Hello {inputCustomer.Name}");
}
}

How to check error message in C#? [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 5 years ago.
Improve this question
Is that a way to check error message shown after negative values? I can check if the correct exception was thrown, but what if my method won't throw an exception with negative numbers, just WriteLine to Error output stream.
public List<int> MyMethod()
{
...
try
{
//add elements to list
}
catch(Exception e)
{
Error.WriteLine("Element cannot be negative, but other elements are ok");
}
...
}
[TestMethod]
public void TestWithNegatives()
{
try
{
List<int> list = MyMethod();
//there is a negative int in list, so there'll be an error message
}
catch (Exception e)
{
//Can I check here the error message, if there isn't exception thrown in mymethod?
}
}
Since you already handled the exception and it is not rethrown, you cannot handle it again in your test.
But since you know that a message is written to Console.Error, you can check this by redirecting Console.Error to a custom StringWriter and check what was written to it like that:
public void TestWithNegatives()
{
using (StringWriter sw = new StringWriter())
{
Console.SetError(sw);
List<int> list = MyMethod();
// Check output in "Error":
Assert.IsFalse(string.IsNullOrEmpty(sw.ToString()));
}
}

Writing a function to handle exceptions in C# [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is it reasonable to think I can write a void function to take in an Exception and output the stuff that a catch block normally would? Here's an example of my exception catcher (which I would make individual ones for common exceptions I handle):
private void exCatch(Exception ex)
{
MessageBox.Show("ERROR - " + blah blah blah + ex.ToString(), blah blah);
}
Here it is in practice:
try
{
stuff
}
catch (Exception e)
{
exCatch(e);
}
Is this an efficient way to handle exceptions? If this is reasonable, do people do this? It seems like it could speed up your coding not having to copy paste all your exception junk over and over. Thanks for any help!
There is no problem with that at all. In fact, adding functions to reduce code repetition is definitely advisable. Then if you want to change say the MessageBox buttons you change it once and you're done everywhere.
One note is that you should consider only catching certain types of exceptions that you're expecting. If you're catching an exception it should be because you know where it came from and exactly what to do with it. Or else you might be catching something that should be handled at a higher level and your app can get into an invalid state. Here's an example.
ArgumentNullException
FormatException
OverflowException
Are the exceptions that Int32.Parse(string) throws. Lets say you know you wont be passing in Null this is how MSDN shows you should handle the function:
using System;
public class Example
{
public static void Main()
{
string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "007", "2147483647",
"2147483648", "16e07", "134985.0", "-12034",
"-2147483648", "-2147483649" };
foreach (string value in values)
{
try {
int number = Int32.Parse(value);
Console.WriteLine("{0} --> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
}
}
https://msdn.microsoft.com/en-us/library/b3h1hf19(v=vs.110).aspx
Always look up the exceptions that a method can throw and always document those that you are catching and throwing in your methods.

Invoke method, Converting [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 new here. I have a question for you, maybe just easy, but I can't do it well. I have a few fields in my class:
public Player player;
public Run run;
And a code:
public void doit(string method)
{
foreach (var prop in this.GetType().GetFields())
{
foreach (var meth in prop.FieldType.GetMethods())
{
if (meth.Name == method)
{
meth.Invoke(prop, null);
}
}
}
But when I'm trying to run this problem, I have a error while running:
Object does not match target type.
In line:
meth.Invoke(prop, null);
Error appears, because "prop" isn't an Class object.
When i'm trying to do this:
Player testPlayer;
testPlayer = prop;
I have an error:
'System.Reflection.FieldInfo' to 'WindowsFormsApplication.Player'
I tried many things, but nothing work.
Can you help me plz? It's important for me :)
Thanks.
You're trying to invoke the method passing in the actual FieldInfo object, rather than the value of the field.
A simple fix would be:
if (meth.Name == method)
{
meth.Invoke(prop.GetValue(this), null);
}
However, if you're trying to find a method by name, there's an easier way:
public void doit(string method)
{
foreach (var prop in this.GetType().GetFields())
{
// Get the method by name
var meth = prop.FieldType.GetMethod(method);
if (meth != null)
{
meth.Invoke(prop.GetValue(this), null);
}
}
}
Sounds like you need to get the value of that property:
meth.Invoke(prop.GetValue(this), null);

Method won't return some values, but will return others [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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.
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
Improve this question
I have the following method which returns a number:
public int basketContents()
{
int basketContains = basket.Count();
return basketContains;
}
If I run this method in Main it works fine, however when I use it in a different form in the project it returns 0, regardless of what the actual number should be.
I've called it in the other form like this:
Main Main = new Main();
MessageBox.Show("Basket Contents: " + Main.basketContents(), "Information",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
If I set 'basketContains' to a number myself it will display the number fine in either form. However, when I use the count method it doesn't work.
I guess basket is not static. So in this case it will be null if you call it from another form, cause its a new variable with its default value (->empty so Count will be zero)
Main Main = new Main(); // Im a total new Form. I dont know anything, all my propertys are defaultvalues
Note: The answer is not "make it static". There a much better ways depending on your needs (f.e more parameters, events etc)
Example:
public int basketContents(List<string> myBasket)
{
if (myBasket != null)
{
int basketContains = myBasket.Count();
return basketContains;
}
return 0;
}
call it:
Main mytest = new Main();
var temp = new List<string>();
temp.Add("test");
MessageBox.Show("Basket Contents: " + myTest.basketContents(temp), "Information");
If basket is a member variable of class Main then you will need to set it after creating an instance of Main.
Also bad idea to call a variable the same name as a class.
Something like this should work:
Main myMain = new Main();
myMain.basket.Items.Add("Whatever");
MessageBox.Show("Basket Contents: " + Main.basketContents(), "Information",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

Categories

Resources