Results 1 to 12 of 12

Thread: New to C# - Please help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Exclamation New to C# - Please help

    Probably just a simple question but I have a static void created which supposively needs to be static in order to declare a new thread but I can't access any controls from that void on the main form because they aren't set as static.

    The error shows "An object reference is required for the non-static field, method, or property 'NiRDs.frmMain.tabNiRDs' ".

    How exactly do I create an object reference to the controls on the main form? I can't set the designer code to static because as soon as I load the Designer layout, it reverts the code back to non-static declarations of the controls.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: New to C# - Please help

    Thread moved from the 'CodeBank C#' forum (which is for you to post working code examples, not questions) to the 'C#' forum

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: New to C# - Please help

    Didn't realize I posted in the CodeBank. Sorry bout that.

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: New to C# - Please help

    I create threads all the time on non-static functions.

    What's the code you're using to create the thread?
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: New to C# - Please help

    Hi, thanks for the reply.

    Code:
    namespace NiRDs
    {
        public partial class frmMain : Form
        {        
            #region Public Declarations
                  public Thread t_StartNiRds = new Thread(StartNiRds);
         }
    }
    Then I have the declaration of the StartNiRds function.

    Code:
    public static void StartNiRds()
    {
    
    }
    If I remove the static from the StartNiRds function I get this error.

    A field initializer cannot reference the non-static field, method, or property 'NiRDs.frmMain.StartNiRds()'

  6. #6
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: New to C# - Please help

    Ah - try making it equal to null and then in the Form Initialize, you can create the "new Thread" part.

    I usually also use a Thread Start, so the whole thing looks like this:

    Code:
    //In global space:
    		System.Threading.Thread WorkMule = null;
    
    //In the constructor, after Initialize Component:
    		WorkMule = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
    
    //When I want to start it:
    		WorkMule.Start();
    
    //The declaration of the DoWork class:
    		void DoWork()
    			{ //My code }
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: New to C# - Please help

    Oh, thanks very much. I had a large application coded in VB and I'm porting it to C# so I never had these problems. LoL.

    What is the purpose of the ThreadStart?

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: New to C# - Please help

    I don't think you have to do it anymore, but I have been programming from when .Net was still "preview candidate" and it was needed then, so I got used to using it.

    I think now, the compiler simply does it for you if you don't.

    If I'm wrong about the compiler, then its purpose is to store the delegate address (aka "where your function is at in memory") so that you can later call "Start" on it.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: New to C# - Please help

    Oh ok, I see now. Most likely the compiler does do it for you because in VB I don't have to declare it that way but it makes perfect sense.

    Thanks again!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: New to C# - Please help

    Actually, maybe you can help me with one more thing.

    I'm getting this error for some reason....

    "Operator '|' cannot be applied to operands of type 'WindowsService.ACCESS_MASK' and 'int' "

    Code:
    	private const Int32 SC_MANAGER_QUERY_LOCK_STATUS = 0x00010;
    	private const Int32 SC_MANAGER_CONNECT = 0x00001;
    	private const Int32 SC_MANAGER_CREATE_SERVICE = 0x00002;
    	private const Int32 SC_MANAGER_ENUMERATE_SERVICE = 0x00004;
    	private const Int32 SC_MANAGER_LOCK = 0x00008;
        private const Int32 SC_MANAGER_MODIFY_BOOT_CONFIG = 0x00020;
        private const Int32 SC_MANAGER_ALL_ACCESS = ACCESS_MASK.STANDARD_RIGHTS_REQUIRED | SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_LOCK | SC_MANAGER_QUERY_LOCK_STATUS | SC_MANAGER_MODIFY_BOOT_CONFIG;
    Code:
    	[Flags()]
    	private enum ACCESS_MASK : uint
    	{		
    		DELETE = 0x10000,
    		READ_CONTROL = 0x20000,
    		WRITE_DAC = 0x40000,
    		WRITE_OWNER = 0x80000,
    		SYNCHRONIZE = 0x100000,
    
    		STANDARD_RIGHTS_REQUIRED = 0xf0000,
    
    		STANDARD_RIGHTS_READ = 0x20000,
    		STANDARD_RIGHTS_WRITE = 0x20000,
    		STANDARD_RIGHTS_EXECUTE = 0x20000,
    
    		STANDARD_RIGHTS_ALL = 0x1f0000,
    
    		SPECIFIC_RIGHTS_ALL = 0xffff,
    
    		ACCESS_SYSTEM_SECURITY = 0x1000000,
    
    		MAXIMUM_ALLOWED = 0x2000000,
    
    		GENERIC_READ = 0x80000000,
    		GENERIC_WRITE = 0x40000000,
    		GENERIC_EXECUTE = 0x20000000,
    		GENERIC_ALL = 0x10000000,
    
    		DESKTOP_READOBJECTS = 0x1,
    		DESKTOP_CREATEWINDOW = 0x2,
    		DESKTOP_CREATEMENU = 0x4,
    		DESKTOP_HOOKCONTROL = 0x8,
    		DESKTOP_JOURNALRECORD = 0x10,
    		DESKTOP_JOURNALPLAYBACK = 0x20,
    		DESKTOP_ENUMERATE = 0x40,
    		DESKTOP_WRITEOBJECTS = 0x80,
    		DESKTOP_SWITCHDESKTOP = 0x100,
    
    		WINSTA_ENUMDESKTOPS = 0x1,
    		WINSTA_READATTRIBUTES = 0x2,
    		WINSTA_ACCESSCLIPBOARD = 0x4,
    		WINSTA_CREATEDESKTOP = 0x8,
    		WINSTA_WRITEATTRIBUTES = 0x10,
    		WINSTA_ACCESSGLOBALATOMS = 0x20,
    		WINSTA_EXITWINDOWS = 0x40,
    		WINSTA_ENUMERATE = 0x100,
    		WINSTA_READSCREEN = 0x200,
    
    		WINSTA_ALL_ACCESS = 0x37f
    	}
    If I remove the ACCESS_MASK.STANDARD_RIGHTS_REQUIRED, the error goes away. Why is this occuring?

  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: New to C# - Please help

    Quote Originally Posted by taigon View Post
    If I remove the ACCESS_MASK.STANDARD_RIGHTS_REQUIRED, the error goes away. Why is this occuring?
    Are they they same data type?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Oshawa
    Posts
    214

    Re: New to C# - Please help

    Actually, I figured this one out. Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width