Newbie Classes & externals

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all

Venturing into the world of C# from MFC. One of the things I used to do in MFC was have a library of classes tha
I would use over & over again. I could just pick & choose which class I wanted to have in a new project, copy the
to the new project folder, add to project, include the new class .h where I wanted to use the class, and I had that class i
the new project. If I wanted the new class to be used by the document, view, mainframe, etc. I could declare the clas
as 'extern' in the app's header, and then every other class that had the app's header could use the same class

How do I do something similar in a C# project? Also, along the same line, if I wanted to have a variable to be use
by everybody, I could again declare it as 'extern' in the app's header and then all classes that included the app'
header could use that external variable, how is this done in C#

Any help, examples, links, etc. appreciated...

TIA

Ray K.
 
Ray,

In .NET, I think that the solution is a little better. Basically, you
would compile your classes into another library. Then, when you want to use
classes from that library, just add a reference to that project from your
project. At that point, the public classes are available to the project.

If you want to have a variable that everything can see, then declare it
as public and static. This variable will be available to the whole
app-domain at that point.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

raykos said:
Hello all,

Venturing into the world of C# from MFC. One of the things I used to
do in MFC was have a library of classes that
I would use over & over again. I could just pick & choose which class I
wanted to have in a new project, copy them
to the new project folder, add to project, include the new class .h where
I wanted to use the class, and I had that class in
the new project. If I wanted the new class to be used by the document,
view, mainframe, etc. I could declare the class
as 'extern' in the app's header, and then every other class that had the
app's header could use the same class.
How do I do something similar in a C# project? Also, along the same
line, if I wanted to have a variable to be used
by everybody, I could again declare it as 'extern' in the app's header
and then all classes that included the app's
 
Ray,

Yes, it means that you would have to actually move that library to the
other PC as well. However, with .NET it is really very simple. You just
have to place the assembly in the same directory as the one that references
it.

Your only other option is to copy the code from each class into your new
application, which would force recompiles of all your apps should you want
to change something in those libraries.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

raykos said:
Hello Nicholas,

Thank you for your reply, maybe because I'm still learning, but doesn't
referencing to a class in a library,
'tie' me to that library? In other words, if I have a reference(link?)
to a class I want to use , and deceide to move
my .exe to another PC, does that mean I would have to also move the
library I'm referenced to? Or does
 
Ray,

Yes, you could do that, absolutely. I'm just saying that if it is
compiled already, then there is no need to insert a new class into your
existing project, rather, you can just set a reference. If you don't have
access to a compiled assembly, there is nothing stopping you from copying
the code from somewhere else, pasting it into a cs file in your project, and
then using it from there.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

raykos said:
Hello Nicholas,

So, if I'm not misunderstanding, I can't just copy a single class(even
if it's a .cs) from someplace else, put it into
a new C# project and say "here it is, use it"? Somehow, I must not be
understanding something correctly;
i.e. if you published an article with a class that I thought was
nicky-neat and wanted to use it(with your permission
 
Back
Top