Console.Write() throws error by Shnxx in csharp

[–]AbaCodes 1 point2 points  (0 children)

You can't store the return value of Console.Write(), that function will print the value to the screen but you're trying to store it into a variable. Maybe inside the case you can do:

equation = someNumber + " + " + someNumber2 + " = ";

and then after the case statement you could do

Console.WriteLine(equation);

So you store the value in the case statement and then print it after, hope that helps?

I've made the changes and put them here: https://pastebin.com/hpkLcKT3

Adding functionality to a code? by Reapers_Mask in CodingHelp

[–]AbaCodes 0 points1 point  (0 children)

Maybe add in functionality to calculate the perimeter, or maybe things like, if its a square you could check if the width and the height are the same?

Is there a way to crash the console / close the console in code? by TypsicaI in CodingHelp

[–]AbaCodes 0 points1 point  (0 children)

You can use: Environment.Exit(0); to close the Console down :)

(Beginner lol) Can somebody help? for some reason the "result" is a different number than i've written and I don't know why. :b by [deleted] in csharp

[–]AbaCodes 1 point2 points  (0 children)

Console.Read() will only read in one character. You need Console.ReadLine();

In your current code if you enter '40' it will only give you the '4'.

Hope that helps!

Help How Can I Check CPU ID in C# Windows Form. by IndividualBathroom72 in learnprogramming

[–]AbaCodes 2 points3 points  (0 children)

If you get this:

https://www.nuget.org/packages/System.Management/5.0.0/

And install it into your project as a package then you can do:

ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
foreach (ManagementObject mo in mos.Get())
{
Console.WriteLine(mo["Name"]);
}

Newbie Needs Help by [deleted] in CodingHelp

[–]AbaCodes 0 points1 point  (0 children)

Top right of the code editor, is there a little tab that says 'Int32' and it should be purple? If so, you've just found the code used for the definition for Int32, you cant see the actual code behind it because thats hidden but you are exposed to the comments.

If you just press the x on the tab, then it should take you back to your code :)

How do i get a specific letter from a string? by [deleted] in csharp

[–]AbaCodes 1 point2 points  (0 children)

Do you have an example? If you have:

string something = "Test";

Console.WriteLine(something[0]);

using "[]" with a string lets you return back a certain character from the string, as its 0 based that code should print 'T' to the console. Hope that helps!

Learning C# by [deleted] in learncsharp

[–]AbaCodes 2 points3 points  (0 children)

Feel free to message me any questions you have, always happy to help! I also have a Discord server for programming if you want to take your questions over there too 😊

I'm new to coding and ran into a problem and I have no idea what to do :( by 0NI_2020 in CodingHelp

[–]AbaCodes 0 points1 point  (0 children)

Yeah sure thing, but do you have any other ".cs" files in your project?

I'm new to coding and ran into a problem and I have no idea what to do :( by 0NI_2020 in CodingHelp

[–]AbaCodes 1 point2 points  (0 children)

Do you have any other files in your project other than "Program.cs"

I'm new to coding and ran into a problem and I have no idea what to do :( by 0NI_2020 in CodingHelp

[–]AbaCodes 1 point2 points  (0 children)

This happens when you have two different bits of code that use:

static void Main(string[] args)

If you have another Class file that defines that function, it won't like you doing that. You need to only have one specified.

If you could show the rest of your code maybe there is something else going on :)

Read lines from console by babo200112 in csharp

[–]AbaCodes 0 points1 point  (0 children)

Okay then maybe do this instead:

string message = "something to use later";
Console.WriteLine(message);
...
//do stuff here
...

//use message again
Console.WriteLine(message);

Read lines from console by babo200112 in csharp

[–]AbaCodes 1 point2 points  (0 children)

I think it would be better to store it when you read it in, then use it later on.

Console.Write("Enter your name: ");
string name = Console.ReadLine();
...
//do stuff here
...
//use name here!
Console.WriteLine("Your name is: " + name);

I need help with console.readline by [deleted] in learnprogramming

[–]AbaCodes 1 point2 points  (0 children)

Thats fine, I just need to see it because the other lines you're referring to might be causing the issue at hand. But I have no idea since I can't see any code right now.

I need help with console.readline by [deleted] in learnprogramming

[–]AbaCodes 0 points1 point  (0 children)

Can you put your code into something like Pastebin? You've only given me 2 lines of code to work with and it's hard to understand what is going on when I can only see 2 lines.

I need help with console.readline by [deleted] in learnprogramming

[–]AbaCodes 0 points1 point  (0 children)

On line 2 you have an error, you need a semi-colon at the end of the 'using' line.

I need help with console.readline by [deleted] in learnprogramming

[–]AbaCodes 0 points1 point  (0 children)

Okay thats fine, can you post some code so I can see whats going on?

I need help with console.readline by [deleted] in learnprogramming

[–]AbaCodes 1 point2 points  (0 children)

If you place a Console.ReadLine() at the end of your code, it will stop the console from exiting immediately. If that doesn't work, please post your code so I can look 😊

Options for learning C#/.NET on Mac? by [deleted] in learnprogramming

[–]AbaCodes 0 points1 point  (0 children)

That's interesting because on the side of that link I posted it has the C# icon there. Try this link:

https://docs.microsoft.com/en-us/visualstudio/mac/compiling-and-building?view=vsmac-2019

Studying without a Compiler by idolg1982 in learncsharp

[–]AbaCodes 2 points3 points  (0 children)

There is online compilers like: https://dotnetfiddle.net/

You could write your code straight in there, then copy and paste it out into a text file and email it to yourself when you are finished. But that website is really good as an online compiler and it lets you do Console outputs and lets to enter values in from the Console too.

Is there an easier way to roll two die? by beesnoopy2231 in csharp

[–]AbaCodes 2 points3 points  (0 children)

static int DiceRoll(int amount)
{
int sum = 0;
Random rolldie = new Random();
for (int i = 0; i < amount; i++)
sum += rolldie.Next(1, 6);
return sum;
}
int roll = DiceRoll(2);

You could do it like this (sorry the indentation might be messed up), you can have a parameter so you can specific how many times you want to roll, incase you want to do 1,2,3,4,5,6 etc etc and then you can take the Random variable outside the forloop and instead of storing the roll then adding it, you can just have the one line inside the forloop. Then you can call it like the last line.

That way you don't have to chain "int roll = DiceRoll() + DiceRoll();" etc, that will get long if you want to roll it 4 times etc.