C
C#2y ago
arukovic

❔ ✅ How can I make a variable any type?

How can I make it so a variable has a type 'Any'?
30 Replies
Hugh
Hugh2y ago
You could make it an object? (The ? is part of the type) You’d have to cast it whenever you wanted to get at the actual value
arukovic
arukovicOP2y ago
If i were to add two vars with the type object? would it work? just curious
Thinker
Thinker2y ago
Simple: you don't catsip
arukovic
arukovicOP2y ago
i'll just have to go with object? for now then
Thinker
Thinker2y ago
Why do you think you want this?
Ezlanding
Ezlanding2y ago
No, as the type object? doesn’t have a + operator defined
Thinker
Thinker2y ago
Because there may very well be a better alternative.
arukovic
arukovicOP2y ago
you know any? ah i see
Ezlanding
Ezlanding2y ago
Give context to what your doing
Thinker
Thinker2y ago
What are you trying to do?
arukovic
arukovicOP2y ago
tryna accept input from the user but i dont know the type that user will be entering, and i want to perform actions on number given by the user
Thinker
Thinker2y ago
If you accept user input it will always be a string
arukovic
arukovicOP2y ago
type as in i know it'll not be a string
Thinker
Thinker2y ago
At least using Console.ReadLine
Ezlanding
Ezlanding2y ago
As in from Console.ReadLine? That returns a string
arukovic
arukovicOP2y ago
i did, i also tried Convert.ToInt32(Console.ReadLine()); but that is strict to int not any datatype
Thinker
Thinker2y ago
yes...? Console.ReadLine() returns a string. You can parse that string into other data types (ex. numbers), but you still have to know its type. There is no way to just say "figure out what this string means" You have to be strict and specify explicitly what you expect it to be.
arukovic
arukovicOP2y ago
is that always the case?
Thinker
Thinker2y ago
Yes. Even if you use object here, you won't get around this.
arukovic
arukovicOP2y ago
we always will have to know the type of data?
Thinker
Thinker2y ago
A string is a string. An int is an int. Yes
arukovic
arukovicOP2y ago
alr ty
Thinker
Thinker2y ago
As mentioned, C# is a statically typed language. Types always have to be known.
arukovic
arukovicOP2y ago
no wonder now i know can i close this post or something now?
Thinker
Thinker2y ago
ye /close
arukovic
arukovicOP2y ago
alr
Ezlanding
Ezlanding2y ago
Something you can do is check to see if the string only contains numeric chars, and if so convert. Aka int32.TryParse() https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-7.0#system-int32-tryparse(system-string-system-int32@)
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
mindhardt
mindhardt2y ago
Better avoid Convert, because $tryparse
MODiX
MODiX2y ago
The TryParse pattern is considered best practice of parsing data from a string: - a TryParse method returns true or false to inform you if it succeeded or not, so you can use it directly in a condition, - since C# 7 you can declare a variable that will be used as an out argument inline in an argument list, - it forces you to check if the out argument contains valid data afterwards, Avoid: Convert.ToInt32 — it's a bad choice for parsing an int. It exists only for backwards compatibility reasons and should be considered last resort.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(Note: Convert does contain useful conversion methods: To/FromBase64String, To/FromHexString, ToString(X value, int toBase), ToX(string? value, int fromBase)) Avoid: int.Parse — you have to use a try/catch statement to handle invalid input, which is a less clean solution.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Accord
Accord2y 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