[Hugo Ferreira][Positronic Dreams]
All your code are belong to us!
🔒 Locked
C# & Classes
Advertisement
Original Post
Hi all,
im a complete n00b in C#, although i code rather well in C++.
I created a small class in C#, but couldnt use it like:
myclass abc;
i had to do something like:
myclass abc = new myclass;
then i could use it.
is this standard practice?
if im responsible for creating the class instance, do i also have to kill it?
thanx in advance,
Hey,
C# does not allow you to use variable''s, etc with out initializing them. It is perfectly normal.
C# has what''s called Garbage Collection. It will take care of any memory that you are no longer using. So, if you made a new instance and then set it equal to null, the GC would see that you are done with the old memory and mark it as finalizeable.
I could be wrong, but that is my understanding. I''ve only recently gone to C# from C++ as well
Good Luck!
Michael Bartman
CEO, Lead Programmer
Dark Omen Studios
C# does not allow you to use variable''s, etc with out initializing them. It is perfectly normal.
C# has what''s called Garbage Collection. It will take care of any memory that you are no longer using. So, if you made a new instance and then set it equal to null, the GC would see that you are done with the old memory and mark it as finalizeable.
I could be wrong, but that is my understanding. I''ve only recently gone to C# from C++ as well
Michael Bartman
CEO, Lead Programmer
Dark Omen Studios
quote:
I could be wrong, but that is my understanding
nah, you''re not wrong.
C# handles memory for you internally with a garbage collecter. You dont need to be deleting anything
:::: [ Triple Buffer ] ::::
I think the real reason why you cant do what you did is that Classes in C# have to be created on the heap. Thus
MyClass newclass = new MyClass();
However, structs in C# are allocated on the stack so (without testing myself) you may be able to do
MyStruct struc;
as I''m fairly certain you can do
int i;
etc.. without initialising the variable. though you will get a warning or possiblly and error if you dont initialise and try to use it.
MyClass newclass = new MyClass();
However, structs in C# are allocated on the stack so (without testing myself) you may be able to do
MyStruct struc;
as I''m fairly certain you can do
int i;
etc.. without initialising the variable. though you will get a warning or possiblly and error if you dont initialise and try to use it.
You can use structs in that manner, but none of the variables within the struct will be initialized to a default if I remember correctly. I do know that there is a catch, and I am pretty positive that is it.
Michael Bartman
CEO, Lead Programmer
Dark Omen Studios
[edited by - MichaelBartman on September 22, 2003 4:33:27 AM]
Michael Bartman
CEO, Lead Programmer
Dark Omen Studios
[edited by - MichaelBartman on September 22, 2003 4:33:27 AM]
quote:
Original post by pentium3id
Hi all,
im a complete n00b in C#, although i code rather well in C++.
I created a small class in C#, but couldnt use it like:
myclass abc;
That line in C# is pretty much equivalent to myclass* abs; in C++. You only declared a pointer to the object - you didn''t initialize the pointer to anything.
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
Going off-Topic now:
Does anybody have any info on using a C++ DLL in C#?
Your links, thoughts?
Does anybody have any info on using a C++ DLL in C#?
Your links, thoughts?
[Hugo Ferreira][Positronic Dreams]
All your code are belong to us!
The thing to google for is "P/Invoke".
Well, im loading Dlls, in a plug-in way.
I''m trying to use LoadLibrary.
Any pointers on this?
can''t find any tuts on this online yet...
I''m trying to use LoadLibrary.
Any pointers on this?
can''t find any tuts on this online yet...
[Hugo Ferreira][Positronic Dreams]
All your code are belong to us!
Do you really need to do that with C++ DLLs? .NET has much better facilities for accomodating plugins, as long as the plugins themselves are also implemented in a .NET language. Basically define an interface for your plugins to implement. Your plugin(in a dll) has a class that implements this interface. Then your host app loads the DLL using Assembly.LoadFrom, and then creates an instance of the class that implements the plugin interface(either use a predefined name for the class, or search the assembly for classes implementing the interface).
If you really need to use C++ DLLs, I suggest you take a look at http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinteroperatingwithunmanagedcode.asp
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
If you really need to use C++ DLLs, I suggest you take a look at http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinteroperatingwithunmanagedcode.asp
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
Yea, i really need an "any-dll" aproach, cause with the dllImport thing, i have to specify the dll.
Well, with plug-ins, you don''t know before-hand what the plugin name is...
Any website providing info on this stuff?
All this work for some prettier GUI is starting to sound way too much...
Thanx for all the tips guys,
Well, with plug-ins, you don''t know before-hand what the plugin name is...
Any website providing info on this stuff?
All this work for some prettier GUI is starting to sound way too much...
Thanx for all the tips guys,
[Hugo Ferreira][Positronic Dreams]
All your code are belong to us!
as far as I know, dynamic loading of dlls with loadlibrary only works for managed dlls - you may need to also provide a C# wrapper for the C++ DLL...
"I woke up sweating and clutching my pillow. Thankfully the powerful and reassuring visage of Optimus Prime staring back at me from my pillow case served to ease my frayed nerves. Like the giant robotic father I never had he always knows just what to say" - Gabe, Penny-Arcade
Alexandre Moura
"I woke up sweating and clutching my pillow. Thankfully the powerful and reassuring visage of Optimus Prime staring back at me from my pillow case served to ease my frayed nerves. Like the giant robotic father I never had he always knows just what to say" - Gabe, Penny-Arcade
Alexandre Moura
Ok, I made a mistake (haven''t really played with this but...) - What you may be able to do is creating a C++ managed DLL, and use loadlibrary there to load the C++ plug in DLL - the C# program would reference the C++ Managed DLL...
"I woke up sweating and clutching my pillow. Thankfully the powerful and reassuring visage of Optimus Prime staring back at me from my pillow case served to ease my frayed nerves. Like the giant robotic father I never had he always knows just what to say" - Gabe, Penny-Arcade
Alexandre Moura
"I woke up sweating and clutching my pillow. Thankfully the powerful and reassuring visage of Optimus Prime staring back at me from my pillow case served to ease my frayed nerves. Like the giant robotic father I never had he always knows just what to say" - Gabe, Penny-Arcade
Alexandre Moura
Who are you kidding? .NET for all intents and purposes is a new platform that is able to interface with the older WinAPI but can''t for many different reasons.
You should seriously reevaluate your tools or requirements if you really need unmanaged plugins and managed code. I don''t see why you''d want to mix the two much if you didn''t have to.
You should seriously reevaluate your tools or requirements if you really need unmanaged plugins and managed code. I don''t see why you''d want to mix the two much if you didn''t have to.
C# is NOT made to deal with unmanaged code primarily, it is NOT a replacement for C++ in that sense. C# is meant for writing managed .NET applications only. You can however easily write a C++ application which uses both managed and unmanaged sections and have them interact with each other (and BTW, the whole concept of managed, means that, to a certain extent, unmanaged DLLs and functions are unavailable - or at least not DIRECTLY available (without P/Invoke type calls)). And as mentioned, you CAN have managed C# deal with managed C++ with in turn deals with unmanaged c++ ... because C++ (unlike C#) has more built in facilities for dealing with unmanaged code directly.
Any reason why you can''t just P/Invoke LoadLibrary and GetProcAddress?
IE:
However, I still don''t get why you need to use unmanaged DLLs for the plugins, when it''s so much easier and more elegant to do it like I described in the earlier post. LoadLibrary and GetProcAddress are very primitive mechanisms in comparison to Assembly.Load(From).
You could take a look at this: http://ankhsvn.com:8088/viewcvs/trunk/Client/Configuration.cs?rev=79&root=colorize&view=markup. It does dynamic loading and creation of language and formatter plugins based on attributes applied to them.
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
IE:
[DllImport( "kernel32.dll" )]
private extern static IntPtr LoadLibrary( string name );
// delegate that matches the function definition in the exported
private delegate IntPtr InitializeDelegate( string param );
[DllImport( "kernel32.dll" )]
private extern static InitializeDelegate GetProcAddress( IntPtr module, string procName );
public static void Main()
{
IntPtr module = LoadLibrary( "coolplugin.dll" );
InitializeDelegate func = GetProcAddress( module, "Initialize" );
// call the DLL function
func( "someParam" );
}
However, I still don''t get why you need to use unmanaged DLLs for the plugins, when it''s so much easier and more elegant to do it like I described in the earlier post. LoadLibrary and GetProcAddress are very primitive mechanisms in comparison to Assembly.Load(From).
You could take a look at this: http://ankhsvn.com:8088/viewcvs/trunk/Client/Configuration.cs?rev=79&root=colorize&view=markup. It does dynamic loading and creation of language and formatter plugins based on attributes applied to them.
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
I am stuck with this problem.
1. I have to load a C++ dll into my C# code.
2. The method i want to access from DLL is
GetName(&strCStringArray)
This method takes a pointer to CStringArray class, and populates the Array of strings.
I dont know how to go about it..... if using DllImport (PInvoke) to load this C++ DLL...how to write the prototype..
Please help....
Tough times don''''t last,Tough people Do.
1. I have to load a C++ dll into my C# code.
2. The method i want to access from DLL is
GetName(&strCStringArray)
This method takes a pointer to CStringArray class, and populates the Array of strings.
I dont know how to go about it..... if using DllImport (PInvoke) to load this C++ DLL...how to write the prototype..
Please help....
Tough times don''''t last,Tough people Do.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement