Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Silex

C# newbie question: referencess

This topic is 5224 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a form class that has a method to print to a text box in one file. Then I have this second file with another class that I'm trying to compile into a library. In the second file (library), I want to call the method to print to the text box that's in the form file. Both the form class and library class are in the same namespace. How do I get around them being in different files so the library class can call the form class' text box printing method? Thanks. [edited by - Silex on June 6, 2004 9:39:08 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hmm, from what I've gathered in my limited c# programming experience, references are dll's. The form is something I want to execute. The class that needs it's text box writer function is supposed to be a helper for the form class, not the other way around. Is there more to references than I thought?

[edited by - Silex on June 6, 2004 8:45:18 PM]

Share this post


Link to post
Share on other sites
A little bit of friendly advice: before starting a thread that looks like blatant flamebait, read your "my first programming book" again.

Share this post


Link to post
Share on other sites
I didn''t realize anyone would take such an obviously generic flamebait title seriously, even less that once getting here and reading the post they would fail to realize the actual post had nothing criticizing the language. I guess that was a little naive of me.

As far as my question is concerned, I don''t believe it''s as simple as the posters so far have understood it to be. Let me try to clarify:

Files:
server.cs -> server.exe
TaskRunner.cs -> TaskRunner.dll

where ServerDriver is dependant on taskserver.dll, and taskserver needs to use a method defined in the in ServerDriver.cs

So nagromo tells me I just need to add a reference. fair enough, I didn''t try that as a first thought because compiling the "main" program twice, once as an exe and once as a dll seemed wasteful and assumed there was a better way of doing it. Now I just tried doing it and referencing server.dll in TaskRunner, but when I try to compile TaskRunner I get the following:

"An object reference is required for the nonstatic field, method, or property..."

What gives?


Share this post


Link to post
Share on other sites
quote:
Original post by Silex
I have a form class that has a method to print to a text box in one file. Then I have this second file with another class that I'm trying to compile into a library. In the second file (library), I want to call the method to print to the text box that's in the form file. Both the form class and library class are in the same namespace.

It doesn't matter if they're in the same namespace. Are they in the same project? Each project compiles to a unique assembly. So if you need cross-project references, the service needs to be compiled into a class library (DLL file), and the client needs to reference that DLL.

For instance, you have your form class that has this print method. Let's just call it PrintForm. You then have another class, you need to use this printing service. Let's call it INeedToPrintClass. If they're in the same project, then it's no problem, the compiler can handle it. However, if they aren't in the same project, the project with PrintForm needs to be compiled to a class library, and you must add a reference to this library in the INeedToPrintClass project.

Alternatively, you don't need to make any sort of class library. You could compile PrintForm into a regular executable assembly and use reflection in INeedToPrintClass to gain access to the PrintForm type. However that's more complicated.

quote:
The class that needs it's text box writer function is supposed to be a helper for the form class, not the other way around. Is there more to references than I thought?

No. As a matter of fact, .NET has managed to simplify everything by making the assembly an atomic unit of functionality. I think the problem here is terminology, so I'll try to avoid confusion. If the project being compiled to a class library (let's call it A) needs to "know about" a type in another project (call it B), then that other project also needs to be a class library, and you add a reference to class library B in class library A (via the References folder, which also has references to System, System.Drawing, etc., whatever else you use). Now, if types in both class libraries (or if one's a class library, and the other is an executable assembly) needs to know about each other, then they should really be in the same class library. I think this is what you're trying to do, which is a good indicator that you need to redesign your library structure.

Hopefully I understood your question.

[edited by - Zipster on June 6, 2004 10:52:46 PM]

Share this post


Link to post
Share on other sites
Zipster,

Here''s the thing: I have a server app and a client app. These are two separate exe''s. They communicate through a class that you''ve called INeedToPrintClass that they both need to "know about." So I don''t think it''s possible for me to add INeedToPrintClass into the server''s project, because then I wouldn''t be able to reference it in the client app (or could I?).

So I''m pretty sure this is a case where they need to know about each other. I haven''t read much about reflection yet; can it handle this problem? I''ll look into it.

Share this post


Link to post
Share on other sites
You could use reflection, but it''s not a very graceful solution, since you have to work on generic type/method terms to call methods and access properties. But luckily you don''t have to use it at all.

What you want to do is create a tertiary project, call it "Common Functionality" or whatever. Place all common types into this project, things that both the client and server use. Compile it into a class library. Then, add a reference to this class library in both your server and client applications.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!