.NET Framework - C# - Dll questions

Asked By Mr. X. on 23-May-12 09:41 AM
Hello,

In C# ...
1. I have tried to write to Console : "Hello world" by
Console.WriteLine("Hello world"), but I cannot see the  console screen.
2. How can I show a MessageBox in a class Library (dll) instead of console.
Need some samples, please.

Thanks :)




Peter Duniho replied to Mr. X. on 23-May-12 10:22 AM
The usual way to enable that is to set your project type to a console
application, so that Windows knows to create and attach a console window to
your process when it starts.  See your project properties for details, or
simply choose the console application type when creating the project in the
first place.


MessageBox works the same in whatever kind of project type you use it.
it is not all that common to use MessageBox in a console application, but
the usage is the same whether in a console application or a DLL or
something else.

Pete
bradbury9 replied to Mr. X. on 23-May-12 12:00 PM
El mi=E9rcoles, 23 de mayo de 2012 15:41:48 UTC+2, Mr. X.  escribi=F3:
e.=20

To call MessageBox.Show function in a class library you must add a referenc=
e to System.Windows.Forms

It would be wise to provide a sample code of the non working Console.WriteL=
ine, so we can look at it and point out the error.
Mr. X. replied to bradbury9 on 24-May-12 07:21 AM
Well, that is what I have done.
Still on the class library - MessageBox.Show("x");  is not compiled.
I got the message:
Error    1    The name 'MessageBox' does not exist in the current context

I use C# VS 2008.

Thanks :)


El mi?rcoles, 23 de mayo de 2012 15:41:48 UTC+2, Mr. X.  escribi?:

To call MessageBox.Show function in a class library you must add a reference
to System.Windows.Forms

It would be wise to provide a sample code of the non working
Console.WriteLine, so we can look at it and point out the error.
Mr. X. replied to Mr. X. on 24-May-12 07:37 AM
First - it just for debug ...
(it is dll : Class library).
I need somehow to trace the dll (also by messages).

Thanks :)


Well, that is what I have done.
Still on the class library - MessageBox.Show("x");  is not compiled.
I got the message:
Error    1    The name 'MessageBox' does not exist in the current context

I use C# VS 2008.

Thanks :)


El mi?rcoles, 23 de mayo de 2012 15:41:48 UTC+2, Mr. X.  escribi?:

To call MessageBox.Show function in a class library you must add a reference
to System.Windows.Forms

It would be wise to provide a sample code of the non working
Console.WriteLine, so we can look at it and point out the error.
Peter Duniho replied to Mr. X. on 24-May-12 10:21 AM
To use any member of any type, you must:

-- reference the assembly that declares the type, and
-- use the correct name in your code when using the type

If a type exists within a namespace, as most do (including MessageBox), you
must either provide the fully-qualified name (i.e. the full name, including
the namespace) or you must add an appropriate "using" directive to your .cs
file for the namespace in which the type is found).

Given the error message, I suspect you have not added the necessary "using"
directive.  You either need to do that, or use the fully-qualified name of
the type (which is "System.Windows.Forms.MessageBox").

Pete
Mr. X. replied to Peter Duniho on 24-May-12 10:23 AM
The application is class library,
so I put on code :
[DllImport("user32.dll", EntryPoint = "MessageBoxA")]
static extern int MsgBox(int hWnd,
string msg,
string caption,
int type);

and use MsgBox fine.

Thanks, anyway :)





To use any member of any type, you must:

-- reference the assembly that declares the type, and
-- use the correct name in your code when using the type

If a type exists within a namespace, as most do (including MessageBox), you
must either provide the fully-qualified name (i.e. the full name, including
the namespace) or you must add an appropriate "using" directive to your .cs
file for the namespace in which the type is found).

Given the error message, I suspect you have not added the necessary "using"
directive.  You either need to do that, or use the fully-qualified name of
the type (which is "System.Windows.Forms.MessageBox").

Pete
Peter Duniho replied to Mr. X. on 24-May-12 10:30 AM
That's a stupid way to display a message box from managed code.


You're welcome.
Mr. X. replied to Peter Duniho on 24-May-12 10:33 AM
System.Windows.Form is not acceptable (it is not compiled - The computer
does not know this - Framework 3.5).




That's a stupid way to display a message box from managed code.


You're welcome.
Peter Duniho replied to Mr. X. on 24-May-12 10:51 AM
Since "System.Windows.Form" is not what any of us wrote, I am not all that
worried about it.

You should become familiar with http://msdn.microsoft.com/ and especially
http://msdn.microsoft.com/library

There is a wealth of useful information and documentation there, including
detailed reference of the C# language and the .NET Framework libraries.

Pete
Mr. X. replied to Peter Duniho on 24-May-12 11:30 AM
For http://social.msdn.microsoft.com/Search/en-US?query=messagebox&ac=8
(In the same site, and many samples around the internet -
http://social.msdn.microsoft.com/Search/en-US?query=messagebox&ac=8
the namespace is System.Windows.Forms (the only "s" for different) - Not
working neighther).




Since "System.Windows.Form" is not what any of us wrote, I am not all that
worried about it.

You should become familiar with http://msdn.microsoft.com/ and especially
http://msdn.microsoft.com/library

There is a wealth of useful information and documentation there, including
detailed reference of the C# language and the .NET Framework libraries.

Pete
bradbury9 replied to Mr. X. on 24-May-12 12:10 PM
El jueves, 24 de mayo de 2012 16:23:33 UTC+2, Mr. X.  escribi=F3:

- Do no reinvent the wheel, use .NET API so you have less code to debug.=20

- You can call .NET API MessageBox.Show since easily http://msdn.microsoft.=
com/es-es/library/system.windows.forms.messagebox.show%28v=3Dvs.80%29.aspx=
=20

- If you port your code to other OS (other versions o windows, smart device=
s, linux) you will not need to change your code to make it work again.

- yeah, it is not compiled but... Aren't the pros better than the cons?
Peter Duniho replied to Mr. X. on 24-May-12 03:08 PM
good grammar).

If you want help, you need to get into the habit of asking good questions.
Mr. X. replied to Peter Duniho on 28-May-12 01:27 AM
Just needed to add reference to System.Windows.Forms.

Still, I do not know why the command console does not return an output to
basic console window (or how can I show the console window - The output type
is class library).

Thanks, anyway :)




good grammar).

If you want help, you need to get into the habit of asking good questions.