Click to See Complete Forum and Search --> : Inheriting in DLLs?


simmerheli
November 16th, 2008, 03:54 PM
Hi?

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


...

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". :ehh:

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

Thanks

-Blake

TheCPUWizard
November 16th, 2008, 04:09 PM
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).

simmerheli
November 16th, 2008, 05:25 PM
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:
public static string Write(int i)
{
return i.ToString();
}

would be exposed.

However:
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

simmerheli
November 16th, 2008, 05:42 PM
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. :confused:

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

BigEd781
November 16th, 2008, 05:53 PM
You could just declare a new FileStream object in the static method.

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

darwen
November 16th, 2008, 06:06 PM
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


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


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.


Form form = new Form();
// >> type >> form. - this'll bring up the instance methods of Form


Darwen.

TheCPUWizard
November 16th, 2008, 06:14 PM
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)


class File : FileStream // notice the lack of an EXCPLICIT public qualfier!{
...
}

simmerheli
November 16th, 2008, 08:24 PM
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

TheCPUWizard
November 16th, 2008, 08:33 PM
Blake,

Glad you got it working. :wave:

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.