|
-
Feb 9th, 2011, 09:22 AM
#1
Thread Starter
Addicted Member
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.
-
Feb 9th, 2011, 09:26 AM
#2
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
-
Feb 9th, 2011, 09:32 AM
#3
Thread Starter
Addicted Member
Re: New to C# - Please help
Didn't realize I posted in the CodeBank. Sorry bout that.
-
Feb 9th, 2011, 11:02 AM
#4
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)
-
Feb 9th, 2011, 11:10 AM
#5
Thread Starter
Addicted Member
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()'
-
Feb 9th, 2011, 11:41 AM
#6
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)
-
Feb 9th, 2011, 11:46 AM
#7
Thread Starter
Addicted Member
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?
-
Feb 9th, 2011, 12:03 PM
#8
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)
-
Feb 9th, 2011, 12:05 PM
#9
Thread Starter
Addicted Member
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!
-
Feb 9th, 2011, 01:09 PM
#10
Thread Starter
Addicted Member
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?
-
Feb 11th, 2011, 10:56 PM
#11
Re: New to C# - Please help
 Originally Posted by taigon
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
-
Feb 15th, 2011, 09:39 AM
#12
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|