Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
I'm building a Global Hotkey class for my app and I'm getting this: "Method must have a return type".

This is the code that I am currently using(it is from http://www.dreamincode.net/forums/topic/180436-global-hotkeys/[^]

C#
class HotKeys
    {
        public GlobalHotkey(int modifier, Keys key, Form form) //GlobalHotkey is giving me the error given before
        {
            this.modifier = modifier;
            this.key = (int)key;
            this.hWnd = form.Handle;
            id = this.GetHashCode();

        }

        public static class Constants
        {
            //modifiers
            public const int NOMOD = 0x0000;
            public const int ALT = 0x0001;
            public const int CTRL = 0x0002;
            public const int SHIFT = 0x0004;
            public const int WIN = 0x0008;

            //window message id for hotkey
            public const int WM_HOTKEY_MSG_ID = 0x0312;
        }

        private int modifier;
        private int key;
        private IntPtr hWnd;
        private int id;

        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);



        public override int GetHashCode()
        {
            return modifier ^ key ^ hWnd.ToInt32();
        }

        public bool Register()
        {
            return RegisterHotKey(hWnd, id, modifier, key);
        }

        public bool Unregister()
        {
            return UnregisterHotKey(hWnd, id);
        }
    }



Thanks in Advance - Cheers - CCB
Posted
Updated 27-Dec-19 19:43pm

You method GlobalHotkey either need to become a constructor, but then its name it should have the same name as it class.

All other methods, non-constructors, need return types, even if it does not return anything, then it's void, for example, public static GlobalHotKey Create(/* ... */) (where GlobalHotKey is then name of some type), public void DoSomething(/* ... */), and the like.

—SA
 
Share this answer
 
Comments
ChrisCreateBoss 9-Apr-15 21:38pm    
Wow, I didn't take a look to that, Thanks again Sergey!!
Sergey Alexandrovich Kryukov 9-Apr-15 21:56pm    
You are welcome.
—SA
In a method signature you must declare a return type e.g void, int, bool etc.
If you meant it to be a constructor it must have the same name as the class.
 
Share this answer
 
ChrisCreateBoss asked

Something more, the class is finished, I want now to use it in my project, the class is in the same solution of Form1, but I can't use "using Hotkeys", it looks like if the class does not exists.
This is not how using directive (not to be mixed up with using statement) is used. First of all, this is not more that shortcut. There are alias and regular namespace forms.

Say, your class full name is My.Namespace.Hot.HotKey. Then, you can shortcut this full name in usage if you use using:
C#
using My.Namespace.Hot;
// ...
Hotkey instance;

or
C#
using My.Namespace;
// ...
Hot.Hotkey instance;

or
C#
using My;
// ...
Namespace.Hot.Hotkey instance;

or, finally alias:
C#
using VeryHot = My.Namespace.Hot.HotKey;
// ...
VeryHot instance;


In all cases, you can use the full names of the types, no matter is you use using or not.

I hope the examples are clear, but why not reading about it and learning both namespaces and using? Please see:
https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx,
https://msdn.microsoft.com/en-us/library/0d941h9d.aspx,
https://msdn.microsoft.com/en-us/library/sf0df423.aspx.

—SA
 
Share this answer
 
v2
Comments
ChrisCreateBoss 9-Apr-15 22:11pm    
Solved that already, but thanks anyways, don't know why nut I think that C# Classes hate me, I get many errors often when using classes; I have one more, and I hope it's the last one: Can't access to Constants from my code: I'm using "private GlobalHotkeys.GBH ghk;" and then "ghk = new GlobalHotkeys.GBH(Constants.NOMOD, Keys.Space, this);" and it says that Constants does not exist in the current context, but in dreamCode, it is used exactly the same way as I'm doing.
Sergey Alexandrovich Kryukov 9-Apr-15 22:39pm    
All right, I did not know that you solved this problem at the time of answering, so I hope you accept if formally, too.

As to your next question, you need to show wider context of the code. Will you post it as a separate question on more comprehensive code sample and formatted as code? If you do, please live me a link, I'll try to answer a bit later, need to go now.

—SA
ChrisCreateBoss 10-Apr-15 17:55pm    
Sure, I'll post a new question with the formatted text. I'll give you the link in a few minutes.
Sergey Alexandrovich Kryukov 10-Apr-15 18:30pm    
Already answered... If you thought that this new question is related to this solution, you were perfectly right — with one additional twist: using a nesting class (which could be your quite reasonable decision — don't listed to the false "advice" by BacchusBeale).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900