βœ… Passing a potential NULL value, is it allowed?

namespace Calculator.Controller
{
internal class CalculatorController
{

public static void Run(string[] input)
{
if (input == null)
{
InputHandler.zeroinputargument(input);
}
else
{
InputHandler.twoinputarguments(input);
}
}
}
}

namespace Calculator.View
{
public class InputHandler
{
// These two methods are used to determine if the array of input had
public static void zeroinputargument(string[] input)
{

}
public static void twoinputarguments(string[] input)
{

}
}
}
namespace Calculator.Controller
{
internal class CalculatorController
{

public static void Run(string[] input)
{
if (input == null)
{
InputHandler.zeroinputargument(input);
}
else
{
InputHandler.twoinputarguments(input);
}
}
}
}

namespace Calculator.View
{
public class InputHandler
{
// These two methods are used to determine if the array of input had
public static void zeroinputargument(string[] input)
{

}
public static void twoinputarguments(string[] input)
{

}
}
}
Would this be valid even if the input argument for InputHandler.zeroinputargument(input); and InputHandler.twoinputarguments(input); might be NULL?
17 Replies
Merineth πŸ‡ΈπŸ‡ͺ
As in, what if my method runs argument was NULL. Would everything still work?
Jimmacle
Jimmacleβ€’2mo ago
the code as-is? sure, it doesn't automatically crash just because you passed null it will crash if you try to access members of a null variable, like accessing elements of the array or calling a method on the array
canton7
canton7β€’2mo ago
It rather depends on what zeroinputargument does with its input parameter
Merineth πŸ‡ΈπŸ‡ͺ
Ahhh zeroinputargument wont use input parameter might as well remove it :p :catlaugh: Not sure what i was thinking there, my bad Quick question
internal class Calculator
{
static void Main(string[] args)
{
// Create an instance of the Class CalculatorController according to description of assignment.
CalculatorController controller = new CalculatorController();
// Call the method run() according to description of assignment.
CalculatorController.Run(args);
}
}
}
internal class Calculator
{
static void Main(string[] args)
{
// Create an instance of the Class CalculatorController according to description of assignment.
CalculatorController controller = new CalculatorController();
// Call the method run() according to description of assignment.
CalculatorController.Run(args);
}
}
}
If this program was started where no arguemnts were specified would the string[] be NULL ?
Scarlet
Scarletβ€’2mo ago
No It would be an empty array
Merineth πŸ‡ΈπŸ‡ͺ
Aaah that makes sense So if I wanted to check for that I’d aim for something like if (args.Length == 0)
Scarlet
Scarletβ€’2mo ago
Actually if you pass no arguments then the array will have one item The item is the file path to the program
Merineth πŸ‡ΈπŸ‡ͺ
Interesting I’ll remake it and see You are a legend @a coding witch. It indeed comes out to 0!
Scarlet
Scarletβ€’2mo ago
Great There is another way to get the arguments
Merineth πŸ‡ΈπŸ‡ͺ
By any chance, do you also know if i were to do string text = Console.Readline(); And the user inputs 2 3 + would the 2 and 3 become also strings? Are there types that identify each one as is? For example 2 is int, 3 is int, space bar is string and + is string?
Jimmacle
Jimmacleβ€’2mo ago
ReadLine only gives you one string, and that string is everything the user typed before hitting enter you'll have to do some string processing/parsing to turn that one string into individual numbers/symbols/etc $tryparse will be relevant
MODiX
MODiXβ€’2mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Merineth πŸ‡ΈπŸ‡ͺ
Interesting "Avoid int.Parse if you do not know if the value parsed is definitely a number." Isn't this unavoidable tho?
Jimmacle
Jimmacleβ€’2mo ago
no it's saying that if you aren't 100% sure the input string is a number, use TryParse instead for user input you won't ever know for sure, but for other applications like reading files another program generated you can make assumptions
Merineth πŸ‡ΈπŸ‡ͺ
ooh i see
Jimmacle
Jimmacleβ€’2mo ago
basically, you can use TryParse in an if instead of Parse in a try/catch and it's a bit nicer to look at and technically more efficient code
Merineth πŸ‡ΈπŸ‡ͺ
Hmm
namespace Calculator.Model
{
public class CalculationHandler
{
public static void zeroinput(string text)
{
// We take the string of text and split it based on the ' ' space betwen each +,-,*,/, Int or Float.
string[] parts = text.Split(' ');
// Now we define foreach and every index of parts and determine it's type.
for (int i = 0; i < parts.Length; i++)
{

}
}
}
}
namespace Calculator.Model
{
public class CalculationHandler
{
public static void zeroinput(string text)
{
// We take the string of text and split it based on the ' ' space betwen each +,-,*,/, Int or Float.
string[] parts = text.Split(' ');
// Now we define foreach and every index of parts and determine it's type.
for (int i = 0; i < parts.Length; i++)
{

}
}
}
}
I'm not too familiar with TryParse, woudl you be able advise me how it works? foreach might be more appropriate here but i've never used it before. Either way, i want to Parse every item inside the string[] parts now to it's appropriate type Ok nvm i decided on an if, else if statement to work around it thanks :catlove:
Want results from more Discord servers?
Add your server