✅ Cannot figure out what I am doing wrong.

I am currently taking classes for C#, and im kind of throwing things around with casts and things, in this I am not allowed to use an array to finish the assignment, could maybe put me in the right direction without giving me the correct awnser. The comments to the side are just reminders for myself to go off of(so don't worry about them if they are wrong.)
82 Replies
Angius
Angius17mo ago
Why are you trying to subtract null from number...? And you should have only one StringBuilder Before the loop
Moods
Moods17mo ago
Okay first off, you can return the StringBuilder.ToString() directly ya know :)
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
oh idk actually i got a little annoyed so i through something in there, i knew that wouldn't work
Moods
Moods17mo ago
You don’t need a result string
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
ahh okay alright,ill try that
Angius
Angius17mo ago
If you want to turn a number to a letter, you probably want to be adding to 'A' instead
MODiX
MODiX17mo ago
Angius
REPL Result: Success
new {
A = (char)('A' + 0),
B = (char)('A' + 1),
C = (char)('A' + 2),
// etc
}
new {
A = (char)('A' + 0),
B = (char)('A' + 1),
C = (char)('A' + 2),
// etc
}
Result: <>f__AnonymousType0#1<char, char, char>
{
"a": "A",
"b": "B",
"c": "C"
}
{
"a": "A",
"b": "B",
"c": "C"
}
Compile: 403.041ms | Execution: 65.320ms | React with ❌ to remove this embed.
Moods
Moods17mo ago
Hmmm, no. How you would go about this is like this. Each character has some numerical equivalent (I think Unicode or ASCII I forgor) If you’re given a starter char
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
were woudl I apply this exactly? in the new SB?
Moods
Moods17mo ago
What you would need to do is get its equivalent number in whatever system, then you add one to said number and add the char cast(can you do this?) to string builder in a loop
Angius
Angius17mo ago
In the loop
Moods
Moods17mo ago
How do I use the bot thingy, im not sure if something like (char)90 would work
Angius
Angius17mo ago
That's how you generate consecutive characters !e [code here]
Moods
Moods17mo ago
Thanks
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
yes but like new would throw a sythax no?
Angius
Angius17mo ago
It was just for demonstration sake
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
since new does not exist yet, so im asking how to build that.
Angius
Angius17mo ago
To show you how math on chars works
MODiX
MODiX17mo ago
Moods
REPL Result: Success
char foo = (char) 97;
Console.WriteLine(foo);
char foo = (char) 97;
Console.WriteLine(foo);
Console Output
a
a
Compile: 588.636ms | Execution: 26.996ms | React with ❌ to remove this embed.
Moods
Moods17mo ago
Okay yeah that’s how I’d go about it Get the number from the starter, increment, char cast and append to your stringbuilder in a loop and return it
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
okay I tried that, but it was throughing an error for me fo some reason saying I can not convert a char to an int. ill try it again, maybe i spelled it wrong
Moods
Moods17mo ago
Tbf, I converted an int to a char, and im surprised that even worked…
Angius
Angius17mo ago
Why? Chars are integers Like, literally
Moods
Moods17mo ago
Ehh I wasn’t too certain I’ve tried it out before but I wasn’t 100% it’d work
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
waiti i just noticed that says a, how does that work? is that because 97 = a and visual studio just knows that or something?
MODiX
MODiX17mo ago
Moods
REPL Result: Failure
char foo = ‘a’;
int bar = (int)foo;
Console.WriteLine(bar);
char foo = ‘a’;
int bar = (int)foo;
Console.WriteLine(bar);
Exception: CompilationErrorException
- Invalid expression term ''
- Unexpected character '‘'
- ; expected
- ; expected
- Unexpected character '’'
- The name 'a' does not exist in the current context
- Invalid expression term ''
- Unexpected character '‘'
- ; expected
- ; expected
- Unexpected character '’'
- The name 'a' does not exist in the current context
Compile: 539.812ms | Execution: 0.000ms | React with ❌ to remove this embed.
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
cause a is not to anything, im lost on that.
Moods
Moods17mo ago
My quotes got harold
Angius
Angius17mo ago
It's because of how char is implemented in C# A char is a wrapper around int, basically It's a number the character has on the ASCII table
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
alright, so char variable = cast of char to its number... hmm
Moods
Moods17mo ago
Ok I confirmed 97 is the ascii code for ‘a’
MODiX
MODiX17mo ago
Angius
REPL Result: Success
string.Join(" ", Enumerable.Range(0, 100).Select(i => (char)i))
string.Join(" ", Enumerable.Range(0, 100).Select(i => (char)i))
Result: string
       

                  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
       

                  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c
Compile: 581.362ms | Execution: 65.243ms | React with ❌ to remove this embed.
Moods
Moods17mo ago
There’s still ‘A’ too but the gist is characters are numbers You’d just have to increase by one and cast and append in the loop Also banning arrays is a weird restriction
Pobiega
Pobiega17mo ago
is it?
Angius
Angius17mo ago
Idk, an array isn't necessary there
Pobiega
Pobiega17mo ago
there is no array needed here or any collections at all
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
okay but to set it up, now i have my variables but how to allow it to be sit that 65('A') can be stringed with 122('z')?
Angius
Angius17mo ago
It would just be a mostly useless intermediary step if anything
Moods
Moods17mo ago
I mean they’re using stringbuilder so I don’t really see the point
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
cause i need to get like that kind of range which is what im lost on
Angius
Angius17mo ago
You have the StringBuilder there, no?
Pobiega
Pobiega17mo ago
You have the starter. You know how to do math on chars. You have the number. Combine the above and you're good
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
so i++ can count up, also yes I have StringBuilder but my teacher told me that would be good to use, i sadly do not know how it works.
Pobiega
Pobiega17mo ago
stringbuilder builds strings
MODiX
MODiX17mo ago
Angius
REPL Result: Success
var sb = new StringBuilder();
foreach (var i in Enumerable.Range(40, 50))
{
sb.Append((char)i);
}
sb.ToString()
var sb = new StringBuilder();
foreach (var i in Enumerable.Range(40, 50))
{
sb.Append((char)i);
}
sb.ToString()
Result: string
()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXY
()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXY
Compile: 554.971ms | Execution: 51.162ms | React with ❌ to remove this embed.
Pobiega
Pobiega17mo ago
you create it, append to it, then ToString to get the result
Moods
Moods17mo ago
You just need to know the methods, but the basics is it’s a way to have a mutable representation of a string You really just need Append and ToString
Angius
Angius17mo ago
You could also not use a stringbuilder ¯\_(ツ)_/¯
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
okay imma take a nother ss, tell me if i set this up right alright?
MODiX
MODiX17mo ago
Angius
REPL Result: Success
var result = "";
foreach (var i in Enumerable.Range(40, 50))
{
result += (char)i;
}
result
var result = "";
foreach (var i in Enumerable.Range(40, 50))
{
result += (char)i;
}
result
Result: string
()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXY
()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXY
Compile: 496.988ms | Execution: 60.908ms | React with ❌ to remove this embed.
Moods
Moods17mo ago
You mean like adding to an empty string instead?
Angius
Angius17mo ago
lgtm As demonstrated, yes
Moods
Moods17mo ago
Wouldn’t that be slower than appending in this scenario? Or maybe since we know its range it doesn’t matter
Angius
Angius17mo ago
Looks Good To Me
Angius
Angius17mo ago
It would be slower, yes, since each += would allocate a new string
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
so with the appeal how would i set that up?
Angius
Angius17mo ago
But does it matter in this case?
Moods
Moods17mo ago
Yeah fair enough it wouldn’t matter in this scenario
Angius
Angius17mo ago
I'd say it's time you try some stuff out and see if you can figure it out
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
okay, sounds like a plan
Angius
Angius17mo ago
If you get stuck again, come back and we'll take a look
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
alright, thank you guys im so still really confused on where im going with this. so i desided ot but an enumerial rage, from null to del, or 0, 127, but what to put into the appeal still has me very lost, is it going to be some kind of parameter, variable or a method, i wouldn't think a method since my method is string. But I can't find any referecnes online, they only give me arrays.
Pobiega
Pobiega17mo ago
??? Nothing in that message makes sense Do you know what a loop is in programming?
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
I made a enumerable range, setting it form 0, 127, thats the same as null to del correct? And as well as that, i looked on the internet for a reference of appends and i got nothing.
Pobiega
Pobiega17mo ago
You don't need to range anything "null to del" ??? what Your task is very straight forward, You're given a starter character you should just generate an N long string based on that starter dont worry about null, del, Enumerable.Range...
Pobiega
Pobiega17mo ago
so bakc to my question, do you know what a loop is?
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
Yes, it continues to count up or down one + at time, starting at your first part in your for() the int i = (where you start) ((forgot the name of what its called)) and goes until the i < the item you need to finish with, and the will count up or down depending on if you ++ or -- your index.
Pobiega
Pobiega17mo ago
okay, thats more or less accurate its a bit more flexible than that, but it works for this exercise Look at this: !e Console.WriteLine((char)('A'+1));
MODiX
MODiX17mo ago
Pobiega
REPL Result: Success
Console.WriteLine((char)('A'+1));
Console.WriteLine((char)('A'+1));
Console Output
B
B
Compile: 461.113ms | Execution: 23.128ms | React with ❌ to remove this embed.
Pobiega
Pobiega17mo ago
so given A, we can easily get B and so forth
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
okay so since 'A' is my starting value, i will plus it by 126 to get lowercase z?
Pobiega
Pobiega17mo ago
why are you trying to get lowercase z? and your starter isnt fixed, its given by a method parameter
Pobiega
Pobiega17mo ago
F̸̈͑o̵̦̔r̷̈̇g̶̍̀ȍ̵͒t̵̿̓i̸͝n
thats my end value, thats why im trying to do it. okay my logic is really off, its really hard to explain because i don' t understand how to write it.
Pobiega
Pobiega17mo ago
You should re-read the description
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server