1

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.

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

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. 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

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;
    }
Community
  • 1
  • 1
Jonathan Kittell
  • 7,163
  • 15
  • 50
  • 93
  • 1
    `GetSingleAsset` is a method, not a class. `Foo` should be of the type of the class that contains `GetSingleAsset` and you should write out `Foo.GetSingleAsset()` instead of just `Foo`. – Robert Rouhani Feb 15 '14 at 21:20

2 Answers2

4

You need to do this:

NameOfYourClass instanceOfClass = new NameOfYourClass();
Console.WriteLine(instanceOfClass.NameOfMethod());
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

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.

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.

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. For example, "System.Collection.Asset" would be printed and not the values that you are interested in.

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.

Bret
  • 2,283
  • 4
  • 20
  • 28