C
C#

help

What is this type of Overloading?

Sstigzler10/22/2022
I'm converting from vb to c#. What's the correct term for the line public RelayCommand(Action<object> execute) : this(execute, null) in this overloading? It's very helpful:
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

public RelayCommand(Action<object> execute) : this(execute, null)
{

}
}
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

public RelayCommand(Action<object> execute) : this(execute, null)
{

}
}
TTvde110/22/2022
Constructor overload? Where one constructor calls the other
Sstigzler10/22/2022
hey, thanks for the response. It was more the c# shortcut - i.e. putting the : this(execute,null) on the end, rather than typing RelayCommand(execute, null) in the method body. Having thought about it, maybe it doesn't have a name. I just need to find all these handy c# shortcuts. I thought it might have one as it's similar to the syntax for Inherits where you put a colon after the Class name e.g. : INotifyPropertyChanged
Eero10/22/2022
it's not a shortcut. it would be impossible to put that into the body
Sstigzler10/22/2022
@Ero ? So replacing
public RelayCommand(Action<object> execute) : this(execute, null)
{

}
public RelayCommand(Action<object> execute) : this(execute, null)
{

}
with
public RelayCommand(Action<object> execute)
{
RelayCommand(execute, null);
}
public RelayCommand(Action<object> execute)
{
RelayCommand(execute, null);
}
wouldn't work?
DDurabilis10/22/2022
Nope
Sstigzler10/22/2022
Oh well - I know I'm fgoing to get a null reference exception if that's what you're meaning?
DDurabilis10/22/2022
You just can't call a constructor from within a constructor, it can only be called with new and with the : this(<args>) notation
Sstigzler10/22/2022
ah! Well that I would never have known! Thanks a bunch. What's the best reference for all these types of c# idiosyncrasies? That would have driven me potty if I'd have gotten stuck with that. Is there a site? I've done quite a bit of reading on the basics, but it's stuff like this that's going to catch me out
DDurabilis10/22/2022
I don't know of a resource for all idiosyncracies but you can read up on constructor chaining in C#
Sstigzler10/22/2022
Ha! and that answers the original question. "Constructor Chaining" Thanks again.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Rendering and updating a colormapI'm developing a library related to real-time image generation. To test it, I'm trying to make an apClose discord with buttonhow to close discord with button?FunctionsI'm wandering if it's possible to get a function to return more than one value of different data typwhy doesnt my console application let me make Main() class?it says "Program does not contain a static "Main" method suitable for an entry point"arry with loopshow to Check if the array is equal. Two arrays are equal if their sizes are same, and elements with EF Core - Generic repository update methodI have a generic repository which contains my general CRUD operations interacting with my DbContext.How to set connection strings for remote serverI'm uploading my project for the first time to another server accessible from the net. I set my secrAccessing data from another projectHello, so I have 2 projects, one is an app that contains some data and the other project is a web apkeyword this is not valid in a static propertywhat do i put here instead of this. ?Maui - layout above grid layout [Resolved]Hi! I have a grid layout created, but I was wondering if there was any way that I could create anothRunning PowerShell to install MSIX from .NET MAUII am trying to implement a custom auto-updating solution, as the Appinstaller program has issues witCant connect to Redis DatabaseAny idea, why I cant connect to my local Redis database? When I try to connect via redis cli, it worHow do I get all DbSets from a Context ?I have multiple DbSets where I query each set and parse the results like this: ``` var result1 = _Blazor Authentication passes GetAuthenticationStateAsync() three timesSo I've been trying to find a solution, and I don't even know where to start anymore. But here goes.System.Text.Json.Serialize returns emtpy mapHey! 👋 Any idea, why my System.Text.Json-Serialize returns an empty map? ``response = new Response(MAUI Webview target=_blank href redirects not working on androidI have an MAUI app that is essentially a `WebView` that hosts a wordpress website. My problem is tha❔ SQL Server | Can i use 'Case When' in Where Clause?i ususally do it by create the query into the string like this ```sql set @sql = 'select * from persin vs2022, what is the shortcut for commenting xaml?titlechanging system(OS) font using codethere's an app called `Microangelo On Display` and it supports changing the system font. i want to kbyte[] does not equal a matching byte[], but the hex values match.I have two byte arrays, one is 4 bytes long containing a known sequence, the other is file data. I a