-2

I have Some C# Class Like This:

public class Result_Splash
{
    public string id;
    public string version_code;
    public string version_number;
}

public class Link_Splash
{
    public int id;
    public int app_version_id;
    public string link;
    public int provider_type;
    public int order_download;
    public int status;
}

And A Method Like This:

public void SomeClassDebug(???)
{
    Debug.Log(???);
}

So, I Need To Pass My Classes To My Method And Debug Their Field. Is It Possible?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Nastary
  • 345
  • 4
  • 19
  • 1
    you don't pass **class**, you pass an **object** of a class. – Bizhan Jun 16 '19 at 08:15
  • 1
    You should probably read and follow Unity's tutorials. They are great and this is explained – Camilo Terevinto Jun 16 '19 at 08:38
  • @Bizahn *instance ;) `object` is a type itself – derHugo Jun 16 '19 at 08:52
  • @derHugo "[An object is an instance of a reference type](https://stackoverflow.com/a/1216676/107625)". – Uwe Keim Jun 16 '19 at 13:42
  • @UweKeim apparently that is Java specific .. and they say it is often used as synonym .. I just wanted to point out that this naming is confusing because in c# [object](https://learn.microsoft.com/de-de/dotnet/csharp/language-reference/keywords/object) is a type itself so it is [better to call it an instance not object](https://social.msdn.microsoft.com/Forums/vstudio/en-US/fa92d20f-06e9-443d-b594-72b48c86c16b/what-is-difference-between-an-instance-and-object-of-a-class?forum=csharpgeneral) – derHugo Jun 16 '19 at 13:46
  • @derHugo No, this isn't Java specific. – Uwe Keim Jun 16 '19 at 13:48
  • @UweKeim yes the question you linked is. – derHugo Jun 16 '19 at 13:50

3 Answers3

3
Debug.Log();

Expects either string or an object(the base class of everything in c#) as parameter. If you pass in an object the Debug.Log internally will automatically convert it to a string using the ToString method of that object.

The question is what should be logged here?

Ofcourse you could define that "ad-hoc" within the Debug.Log like e.g.

public void SomeClassDebug(Result_Splash result)
{
    Debug.Log(result.id);
}

so you will see the result of result.id.ToString() as log.

In my eyes the better way is in your classes implement/override ToString in order to customize the output it will generate according to your needs. (I'm lazy so I'll show it for the smaller class - the other one is your homework)

public class Result_Splash
{
    public string id;
    public string version_code;
    public string version_number;

    public override string ToString()
    {
        // For example print the values as requested
        return $"id: {id}, version_code: {version_code}, version_number: {version_number}";
    }
}

I used a $ string interpolation which is basically a better way of writing e.g.

return base.ToString() + " id: " + id + ", version_code: " + version_code + ", version_number: " + version_number;

but you can use whatever you want to return the values.

Now you can simply do

public void SomeClassDebug(Result_Splash result)
{
    Debug.Log(result);
}

And when you call it then pass in an instance of that type

var aResult = new Result_Splash()
    {
        id = "Hello",
        version_code = " ",
        version_number = "World!"
    };

SomeClassDebug(aResult);

this should generate a log

id: Hello, version_code = , version_number = World!

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • hey man, thanks for reply. can i use that class as parameter in method and inside of method? i mean with change my parameter (class name) , it change in body of method? – Nastary Jun 16 '19 at 09:25
  • Yes you can .. what you pass is a reference so any change within that method applies to the referenced instance. – derHugo Jun 16 '19 at 18:20
1

There are several options. For instance you may include your method Debug into your class and use Unity JSON serializer (or NewtonSoft, etc) to get all field names and data converted into JSON formatted text automatically:

public class BaseLoggableClass
{    
    public void Debug()
    {
        Debug.Log(ChosenJsonSerializer.Serialize(this));
    }

}

public class Result_Splash : BaseLoggableClass
{
//... your fields here
}

// example of usage
var t = Result_Splash();
... some code ...
t.Debug();

As alternative you may use NLOG logger, setup JSON layout and use File or whatever target to get your data logged.

Anton Norko
  • 2,166
  • 1
  • 15
  • 20
  • This is a terrible use of inheritance. Override `ToString()` and call Debug.Log on the object instead. Serializing it and using inheritance to avoid rewriting code is awful. What happens when someone wants their class to inherit from `MonoBehavior` *and* `BaseLoggableClass`? – Draco18s no longer trusts SE Jun 16 '19 at 18:12
  • @Draco18s as I said: there are several options. There are no silver bullet. Only requirements. – Anton Norko Jun 17 '19 at 19:49
0

Here's how you pass an object (or two) to a method. This is the absolute fundamentals of C#, or any object oriented programming language, so if you don't understand this I suggest trying out learning these fundamentals first.

public void SomeMethod() {
    Link_Splash linkSplash = new Link_Splash {
        id = "123",
    };

    Result_Splash resultSplash = new Result_Splash {
        id = "456",
    }

    SomeClassDebug(resultSplash, linkSplash);
}

public void SomeClassDebug(Result_Splash result, Link_Splash link)
{
    Debug.Log(result.id); // => 456
}
Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32