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}");
}
}
Related
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.
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 4 years ago.
Improve this question
I am integrating MailChimp API using ASYNC and JSON
****1 - Caller Section****
internal class MCReportsOverview
{
internal async Task<ReportOverview_CampaignSpecific> CampaignSpecificOverviewAsync(string campaignId)
{
string endpoint = Authenticate.EndPoint(TargetTypes.reports, SubTargetType.not_applicable, SubTargetType.not_applicable, campaignId);
var jsonTask= await BaseOperation.GetAsync<ReportOverview_CampaignSpecific>(endpoint);
return jsonTask;
}
}
**** 2 - Called Section ****
F11 hangs on the last line and doesn't return value to #1.
The data is all correct. The code for the called function is
public static async Task<T> GetAsync<T>(string endpoint) where T : class
{
string content;
using (HttpClient client = new HttpClient())
{
try
{
Authenticate.ClientAuthentication(client);
content = await client.GetStringAsync(endpoint).ConfigureAwait(false);
}
}
return JsonConvert.DeserializeObject<T>(content); // F11 hangs here and doesnt return to the calling function
}
Function Calling
This is how I am calling #1
MCCampaignsOverview overview = new MCCampaignsOverview();
var yy = overview.GetCampaignByIdAsync("aaaa1111").Result;
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
EDIT: I should have clarified, but I am debugging these two applications in Visual studio. So when I deploy it, one goes to the Rasp Pi (this is the app with the issue), the other is a console app that runs on my local machine.
I'm not sure of how else to phrase this question, but with your help I'm confident that I can turn it into a more generic question. At this point I have my project working, but I am unsure of why the way I previously implemented this string parsing did not work. I would like to understand why.
Setup:
I have a console app which I can write a command into, and it sends to an IoT Hub, which my Raspberry Pi then reads, parses the command, and executes a function.
// Keep listening for messages
private async Task listenForMessageFromDeviceTask()
{
while (true)
{
var msg = await AzureIoTHub.ReceiveCloudToDeviceMessageAsync();
if (msg == null) continue;
Globals.ParseMsg(msg);
}
}
Solution:
Link to the current working class on GitHub
public static void ParseMsg(string msg)
{
// Split msg on whitespace sequences.
// This works great! I only need to retrieve the first word.
string[] firstWordInMsg = Regex.Split(msg, #"\s+");
switch (firstWordInMsg[0])
{
case "tween":
// Do something
break;
case "stop":
// Do something
break;
}
Problem:
Link to the non-working version on GitHub
public static void ParseMsg(string msg)
{
// This will only execute ONE time. I can send 10 messages, and it will
// only receive one. I now have to restart the app, and it can then // receive the next message in the queue.
var first_word = FirstWordFromMessage(msg)
switch (first_word)
{
case "tween":
// Do something
break;
case "stop":
// Do something
break;
}
public string FirstWordFromMsg(string msg)
{
var firstWord = msg.Substring(0, msg.IndexOf(" ", StringComparison.Ordinal));
return firstWord;
}
My understanding:
When I use the first solution, everything plays nice. I can send 10 messages, and the app can read all 10 of them.
With the second implementation, I can only read ONE message before I need to restart the app. At this point, it has 9 more to go through. I then need to quit the app, restart it, and it then has 8 more to go through.
Why would this happen?
You need to iterate over each message contained in words:
public static void ParseMsg(string msg)
{
string[] words = Regex.Split(msg, #"\s+");
foreach (string word in words)
{
switch (word)
{
case "tween":
// Do something
break;
case "stop":
// Do something
break;
}
}
}
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);
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);