1

I have a function that saves different variables, but the variables are not always the same type, they consist of ints, strings and a list. Is there was way I can pass a generic parameter to a method without defining its type?

I am trying to turn this code

public static void UpdateUser(User user) {
    DatabaseReference databaseReference = FirebaseDatabase.DefaultInstance.RootReference;
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("userName").SetValueAsync(user.userName);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("hair").SetValueAsync(user.hair);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("face").SetValueAsync(user.face);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("kit").SetValueAsync(user.kit);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("body").SetValueAsync(user.body);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("offlineGamesPlayed").SetValueAsync(user.offlineGamesPlayed);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("onlineGamesPlayed").SetValueAsync(user.onlineGamesPlayed);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("offlineGamesWon").SetValueAsync(user.offlineGamesWon);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("onlineGamesWon").SetValueAsync(user.onlineGamesWon);
    databaseReference.Child("users").Child(AuthenitcationKey()).Child("friends").SetValueAsync(user.friends);
}

into something more like this

public static Task<bool> UpdateUser(string saveName, Var varToSave) {
    DatabaseReference databaseReference = FirebaseDatabase.DefaultInstance.RootReference;
    return databaseReference.Child("users").Child(AuthenitcationKey()).Child(saveName).SetValueAsync(varToSave).ContinueWith((task) => {
        return task.IsCompleted;
    });
}
  • Why not just overload your parameters? – Paul Nov 22 '19 at 12:13
  • 5
    Well yes, it's called generics, but without knowing exactly what you're doing, it's impossible to suggest a solution. – DavidG Nov 22 '19 at 12:13
  • 1
    if you don't want to use generics you can pass a variable of type object and then check and act based on its type inside your method as well – vhr Nov 22 '19 at 12:15
  • 2
    @vhr - You can, but that's a horrible way of doing it! If it's just 'data', then it could simply be passed as a string and written to wherever it needs to go. It all depends on the function of the piece of data. – Paul Nov 22 '19 at 12:22
  • 1
    Thank you vhr, that idea has worked perfectly – BionicLobsters Nov 22 '19 at 12:24
  • 1
    No, don't use @vhr's idea, it's a terrible way to code. – DavidG Nov 22 '19 at 12:26
  • How would you suggest doing it? Im a self taught programmer working with a database for first time, so ill take any advice i can get lol – BionicLobsters Nov 22 '19 at 12:26
  • @DavidG I agree it is not the best way and it's much better to use i.e. generics but it gives you what need and you can use absolutely any type... – vhr Nov 22 '19 at 12:39
  • @vhr Using `goto` statements gives you what you need too, doesn't mean you should EVER use it. Just because you can do something, doesn't mean you should. – DavidG Nov 22 '19 at 12:39
  • 1
    @DavidG not exactly the same case though ;) your example is too extreme :) passing objects is not that bad – vhr Nov 22 '19 at 12:41
  • @vhr Yes, passing `object` isn't bad, but checking type is almost always a bad idea. – DavidG Nov 22 '19 at 12:43
  • @BionicLobsters you can also create your own class (entity) with properties like value, name, etc. then set these properties and pass it to your method – vhr Nov 22 '19 at 12:44
  • 1
    passing `object` is actually in some cases just fine. E.g. in networking we just pass `object` parameters, get it's actual type and serialize both into the `byte[]` with a separator so we can transmit it. On the other side we split at the separator and parse/cast the received `object` into the according type. It all depends on use cases. I heard even people using [`goto`](https://stackoverflow.com/questions/6545720/does-anyone-still-use-goto-in-c-sharp-and-if-so-why) btw. for some cases but that one is in most cases not best practice ^^ – derHugo Nov 22 '19 at 16:47

0 Answers0