2

The tutorial application, MusicStore -MVC3 (92 PageNo), created a POCO class like:

public partial class ShoppingCart
    {
        MusicStoreEntities storeDB = new MusicStoreEntities();

        public static ShoppingCart GetCart(HttpContextBase context)
        {
            var cart = new ShoppingCart();
            cart.ShoppingCartId = cart.GetCartId(context);
            return cart;
        }
     }

How can we access static method in partial classes? In my opinion, we cannot access static methods in a partial class. The partial attribute means other parts of the class will be included in the namespace. In this scenario, I do not know where this other partial class is implemented.

My questions about this static method are:

  1. Can we access static methods in partial classes? If so, how?
  2. Where is this partial class implemented in this MusicStore application? I am not able to find the other part of this class's implementation.

Updated: There is no other ShoppingCart class in the models directory. Does anyone know where that partial implementation would be?

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • Can you clarify #2 a bit. It's unclear what you're asking for there – JaredPar Feb 03 '12 at 15:48
  • 1
    You should be able to just as if it were any other class. Have you tried it? If so and it did not work, what error do you get? – D Stanley Feb 03 '12 at 15:49
  • @JaredPar updated. its partial class. i am asking where it will be defined. – Ravi Gadag Feb 03 '12 at 15:50
  • Of course you can! A partial class is exactly the same as a regular class, except that you can add members to the class in another `.cs` file. Please clarify what you exactly are going to do. – Mohammad Dehghan Feb 03 '12 at 15:52
  • 1
    I wouldn't worry to much about finding another counterpart of the partial class. You downloaded code which has changed 12 times since check-in, according to source code history. There could have been an initial intention to have the class shared between 2 or more files but than that idea was dropped or files were merged. If it does bother you that much, remove the `partial` keyword. If the app still runs, happy days. You can also check the source code history on CodePlex and check older version of the code if you want to know when it happened or if it always was like that. – Nope Feb 03 '12 at 16:07

2 Answers2

8

A partial class in C# can definitely access static methods. The partial attribute simply says a class can (not must) be defined accross multiple files and otherwise doesn't affect member lookup.

EDIT Responding to comment in question

A possible explanation for why you can't find the other implementation of ShoppingCart is it may not exist. A partial class is not required to have multiple definitions. The partial only means there may be other parts of the definition.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3
  1. Yes, you can access static methods in partial classes.

    Partial classes are just a way of representing a regular class in multiple source files, often with some of those source files controlled (or generated) by tools.

    You can call ShoppingCart.GetCart(context) anywhere - it's a normal public static method.

  2. It's still not really clear what your second question means, but there doesn't have to be another part at all. It's fine (though unusual) to have a partial class which is only declared in a single file.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Ravi: And what exactly do you mean by "can i know how this partial came"? It's just a source file - it's there, you can see it. Maybe there isn't another part to it, maybe there is... – Jon Skeet Feb 03 '12 at 15:52
  • i searched all files. may be i am missing some thing – Ravi Gadag Feb 03 '12 at 15:58