CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2004
    Posts
    61

    Arrow Inheriting in DLLs?

    Hi?

    I'm creating a DLL that is inherting from FileStream.

    Code:
    ...
    
    public class File : FileStream
    {
        ...
    
         // Read in bytes from a FileStream and puts them into a string object.
         public string ReadString(int size)
         {
                byte[] array = new byte[size];
                if(base.Read(array, 0, size) >= 0)
                {
                     // Work with the array here.
                }
         }
    }
    This compiles but when I use the DLL in an application, I cannot see the ReadString method.

    If I attempt to make this method static (as in normal DLLs), I'm presented with the following error:

    "keyword 'base' is not availble in a static method".

    How can I (or even can I) inherit from FileStream in a DLL and make my own methods available?

    Thanks

    -Blake

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Inheriting in DLLs?

    Based on the code shown, there should be no problem. Are you suure you are referencing the appropriate assembly (Assembly, is the proper term to use, rathern than DLL when talking .NET code).

    If you can reduce the assembly containing the code to something minimal yet complete, I can tak a look at it.

    Additionally YOU can look with wither the OBjectBrowser (built into Visual Studio) or use a tool like Reflector (formerly by Lutz Roeder, now maintained by Red Gate Software).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2004
    Posts
    61

    Arrow Re: Inheriting in DLLs?

    Thanks Wizard,

    I've perused my DLL in the ObjectBrowser and the only resources that seem to be exposed are those from FileStream, none of my own methods.

    I've created several DLLs in the past and used them in my applications. I've always declared the methods I've wanted to expose in my DLLs as public static. If I don't include the static declaration, the methods are not exposed.

    For example:
    Code:
    public static string Write(int i)
    {
        return i.ToString();
    }
    would be exposed.

    However:
    Code:
    public string Write(int i)
    {
        return i.ToString();
    }
    would not.

    Both would compile just fine, however.

    So I assume I must decare my methods as public static.

    Now I'm attempting to do the same thing, but inherit from FileStream in my DLL. However, as soon as I attempt to use a FileStream base method (such as base.Write...) I'm presented with the error indicating that I cannot reference the inherited base member in a static method.

    Is there some way I can expose my own method that uses the base class in a DLL that inherits from FileStream?

    Thanks

    -Blake

  4. #4
    Join Date
    Jan 2004
    Posts
    61

    Re: Inheriting in DLLs?

    After thinking about this for a minute, I now understand why I cannot access the base class of an intereted resource in a static method. I've assuming that its because the base class itself is not static.

    So my question...
    Do you know if there is a way to expose my own method in a DLL where that method uses the base, inherted class (in this case FileStream)?

    public int MyReadMethod(byte[] array, int count)
    {
    retrurn base.Read(array, 0, count);
    }

    Thanks tons

    Blake

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Inheriting in DLLs?

    You could just declare a new FileStream object in the static method.

    Code:
    public int MyReadMethod(byte[] array, int count)
    {
        return new FileStream(path, mode).Read(array, 0, count);
    }

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Inheriting in DLLs?

    I've created several DLLs in the past and used them in my applications. I've always declared the methods I've wanted to expose in my DLLs as public static. If I don't include the static declaration, the methods are not exposed.
    I hate to disagree with you, but if this was the case (i.e. you could only expose static methods in assemblies) none of the .NET framework dlls would work.

    Are you creating instances of your objects and then using them e.g. in dll

    Code:
    namespace MyFileExample
    {
        public class File : FileStream
        {
            ...
        
             // Read in bytes from a FileStream and puts them into a string object.
             public string ReadString(int size)
             {
                    byte[] array = new byte[size];
                    if(base.Read(array, 0, size) >= 0)
                    {
                         // Work with the array here.
                    }
             }
        }
    }
    in app/dll referencing dll containing the above code

    Code:
    MyFileExample.File file = new MyFileExample.File(); // or whatever
    file.ReadString(100);
    If you type "File." then intellisense will only show you the static methods : it'll show you the instance methods if you create an instance and then reference it e.g.

    Code:
    Form form = new Form();
    // >> type >> form. - this'll bring up the instance methods of Form
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Inheriting in DLLs?

    An important thing to remember is that the default visibility for classes is INTERNAL (not visible outside the assembly).

    You must declare the CLASS as PUBLIC for it to be exposed. While the original posted code does show this, the following coude would NOT be visisble, and would match all of the prior information (except the code originally posted)

    Code:
    class File : FileStream   // notice the lack of an EXCPLICIT public qualfier!{
       ...
    }
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Jan 2004
    Posts
    61

    Arrow Re: Inheriting in DLLs?

    Thanks all for your response.

    Thanks Darwen. I was attempting to access my methods via non-instantiation (which would require me to declare them as static).

    Thanks Wizard. I have declared it public.

    I've changed things now and it works well.

    Thanks again for everyone's help.

    -Blake

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Inheriting in DLLs?

    Blake,

    Glad you got it working.

    But do you see how a "complete" posting would have removed barriers to initially getting an answer.

    Your post's phrasing indicated that this was happening "when" using the code from the Assembly (dll).

    The implication is that the code worked properly if the .cs file was included in the EXE assembly.

    Obviously this is not true, as an instance member requires an instance in all cases. The fact that your code was in a different assembly had NOTHING to do with the issue.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured