简体   繁体   English

如何在C#中显示非静态方法的返回值

[英]How to display the return value from a non static method in C#

I am working with a non static class and want to display the answer of the method to the console window. 我正在使用一个非静态类,并且想要在控制台窗口中显示方法的答案。

When I change the method to static and call from Main() an error stating "Object reference not set to an instance of an object" appears. 当我将方法更改为static并从Main()调用时,出现错误,指出“对象引用未设置为对象的实例”。

http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(EHNullReference);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(EHNullReference);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd =真

Why can't you call a non-static method from a static method? 为什么不能从静态方法调用非静态方法?

According to this article I need to create an instance of the object using the "new" keyword. 根据本文,我需要使用“ new”关键字创建对象的实例。 My understanding is that you have to create an object for a class and not a method. 我的理解是,您必须为类而不是方法创建对象。

http://msdn.microsoft.com/en-us/library/ms173110.aspx http://msdn.microsoft.com/en-us/library/ms173110.aspx

So, I created a new object but it does not return the result. 因此,我创建了一个新对象,但它不返回结果。

    GetSingleAsset Foo = new GetSingleAsset();
    Console.WriteLine(Foo);

The output just gives the name of the method. 输出仅给出方法的名称。

How can I see the return value of this non static method? 如何查看此非静态方法的返回值?

public Asset GetSingleAsset()
    {
        var memberId = Oid.FromToken("Member:20", _context.MetaModel);
        var query = new Query(memberId);
        var nameAttribute = _context.MetaModel.GetAttributeDefinition("Member.Name");
        var emailAttribute = _context.MetaModel.GetAttributeDefinition("Member.Email");

        query.Selection.Add(nameAttribute);
        query.Selection.Add(emailAttribute);

        var result = _context.Services.Retrieve(query);
        var member = result.Assets[0];

        LogResult(member.Oid.Token,
            GetValue(member.GetAttribute(nameAttribute).Value),
            GetValue(member.GetAttribute(emailAttribute).Value));

        return member;
    }

You need to do this: 您需要这样做:

NameOfYourClass instanceOfClass = new NameOfYourClass();
Console.WriteLine(instanceOfClass.NameOfMethod());

The "answer of the method to the console window" - in this case, is an object - and if you attempt to Console.WriteLine() an object, you will only see the object type written out to the Console - not its values. “方法到控制台窗口的答案”(在这种情况下是一个对象),并且如果您尝试将Console.WriteLine()作为一个对象,则只会看到写到控制台的对象类型,而不是其值。

Yes, Robert is correct - GetSingleAsset() is a method, not a class - because it returns a value of 'Asset' type - a class's constructor will have no return - and Patrick is correct if you want to see (via Console output) what the return is from that particular method - if it wasn't an object. 是的,Robert是正确的-GetSingleAsset()是一个方法,而不是一个类-因为它返回的值为'Asset'类型的值-类的构造函数将没有返回值-如果您想查看Patrick的信息,则是正确的(通过控制台输出)该特定方法的返回结果是什么-如果它不是对象。

However, since 'Asset' is an object itself, if you simply did a Console.WriteLine(Asset) it would show you what the type of Asset deviates from .. not its values. 但是,由于“资产”本身就是对象,因此,如果您仅做一个Console.WriteLine(Asset),它将显示资产的类型偏离..而不是其值。 For example, "System.Collection.Asset" would be printed and not the values that you are interested in. 例如,将打印“ System.Collection.Asset”,而不打印您感兴趣的值。

You will need to put a breakpoint in the code at the point of class instantiation and look through the Locals Window to see the values that type 'Asset' contains and print out exactly the values that you are interested in ... chances are what values you are actually interested in are contained within another object within the Asset class .. perhaps a for loop is in order here. 您将需要在类实例化时在代码中放置一个断点,并通过Locals Window查看“ Asset”类型所包含的值,并准确打印出您感兴趣的值...可能是什么值您实际上感兴趣的是包含在Asset类内的另一个对象中..也许这里有一个for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM