|
Your code is not addressing the values in the class. Try this:
public partial class Form1 : Form
{
MessageBox.Show("Value is: {0}", Class1.Just_A_Test_Set[1]);
}
static class Class1
{
public static byte[] The_Command_Set_01 = {
0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01
};
public static byte[] Just_A_Test_Set = {
0xFF, 0xFE, 0xFD, 0xFC,
0xFB, 0xFA, 0xF9, 0xF8,
0xF7, 0xF6, 0xF5
};
}
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Aha, so the word "public" can precede the word "static" on the same line. My education continues. Obfuscation rules the world.
I just tried your idea.
In my external file which holds the command bytes (Protocol_Stuff.cs) I have these two groups...
static class Class1
{
static Class1();
public static byte[] BARN_Command_Hello = new byte[] {
0xFF, 0x00, 0x0B, 0x00,
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77
}
;
public static byte[] BARN_Test = new byte[] {
0xFF, 0x00, 0x0B, 0x00,
0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77
}
;
}
In the file "Form1.cs" I have this...
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Value is: {0}", Class1.BARN_Test[1]);
}
When I choose "Build/Build", C# gives me this response...
Error 1 The name 'Class1' does not exist in the current context E:\YahYah\SoAndSo\Etc\Form1.cs 277 46 TheProgramName_03
So I say: DUH !
This is the problem that I've been trying to overcome for several days. How do I associate the stuff in one file with the stuff in another file ?
e.g., in old-fashioned low-tech anachronistic assembly language (none dare mention speed or size) I would just put...
Extern: BARN_Test ...in the file that uses it and...
Public BARN_Test ...in the file that actually holds the label. The assembler would make the space for it and the linker would put them together.
How do I do that same sort of procedure in C# with separate files ?
|
|
|
|
|
It works fine for me. Did you put the correct namespace clause in your Class1 file?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Cancel all questions.
This post here [^] should detail the idiocy behind all this mystery.
Hopefully, I'm going to write real code now.
Thank you for loaning some brains on my behalf.
I will try my best to avoid such a blunder in the future.
|
|
|
|
|
Not quite an answer to your question, but expanding on Richard's suggestion[^] of a static class, I also recommend the getter methods generate the command, rather than hard-coding them all.
Of course, this depends on how your commands are structured. For instance, if each command includes the length of the data, or ends with a checksum or CRC, it's typically best to calculate that value and add it to the command, rather than hard-coding it.
If all of your commands are completely different and don't have any common fields like this, then ignore this post
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
dybs wrote: If all of your commands are completely different and don't have any common fields like this, then ignore this post Really, I don't want the send routine to be locked into sort of any formatted thing of any kind.
I want the user to click one button, and then I'll handle it from there; e.g., the number of bytes, when they are sent, etc.
The commands will be who-knows-what size, and the routine should just send out that many bytes; plus, they can change in the next version, so, I don't want format to be an issue.
I just want to do something along this line of thinking
private void Button1_Click(object sender, EventArgs e)
{
try
{
Send_This_Out_Our_Chosen_Serial_Port(The_Data_For_Button1);
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
|
|
|
|
|
I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form. How do I do this? I set it with
SetBackground(Color.Lime);
I saw the following threads, but they didn't seem to be the same thing, as far as I can tell. If it's the same, I'm not sure how to apply it to my situation.
[^]
http://www.masonmc.com/2008/getting-system-colors-in-c/[^]
|
|
|
|
|
MichCl wrote: I have a current form that is either lime or red. I think that (due to background worker threads), the best way to check the current state is to check the color of the form.
The way I read this, the forms' color determines the "state"? Usually, we keep it in a separate variable, and set the color based on that.
I'm having trouble understanding the question; am I correct in guessing that you want to "read/access" a property from a background-worker?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I'm having trouble recording the new state because I'm on a different thread than I started on (using background worker). But I was able to set the color of the background on this thread, so I can wait until I exit the other thread, read the color of the background, and set my state variable. I've tried looking into setting variables created in different threads, but had trouble with what was suggested, and so far I've been able to avoid the issue.
For this case, I program something and have success, so I set the form color for success. Then I have to record the number of success' or failures, and that's the problem. My success/failure count and the form field it's shown in were created in a different thread. Any idea how to get the form color so I can increment my counts based on that?
|
|
|
|
|
MichCl wrote: For this case, I program something and have success, so I set the form color for success. Then I have to record the number of success' or failures, and that's the problem. My success/failure count and the form field it's shown in were created in a different thread.
Let's call that thread the mainthread or the UI-thread. The "something" you speak of, is that running in it's own thread?
You could poll a property in the form from a thread, but that requires locking -something that's always cool to avoid.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I could avoid locking and thread jumping by just querying the color of the background. Any idea how to do it?
|
|
|
|
|
You cannot; the background-color is owned by the form, owned by the mainthread.
How are you querying this value from the worker-thread? Are you polling it, checking it every so seconds?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
The Control.Invoke method has an object return type and so with an appropriate delegated method any information can be obtained from a Control.
e.g.
private delegate Color ColourDelegate(Control c);
private void GetColourInfo(Control c) {
Color bgcol = (Color)c.Invoke(new ColourDelegate(GetColour), c);
Color bgcol2 = (Color)c.Invoke(new ColourDelegate(delegate { return c.BackColor; }));
}
private Color GetColour(Control ctrl) {
return ctrl.BackColor;
}
Alan.
|
|
|
|
|
MichCl wrote: I have a current form that is either lime or red. ... check the color of the form. How do I do this? That doesn't smell right.
The color of the form should be driven the state of an object, not the other way around. It seems what you want to do is to raise an event when the status of your object changes (presumably from "good" to "bad"). Any number of observers can listen for this change and respond to it an appropriate manner.
/ravi
|
|
|
|
|
Hello,
I have added an Extended attribute (extensionAttribute1) to represent a special app ID (string) in Active Directory.
Now, I have extended the UserPrincipal class so I can see this new attribute but I get exceptions when running the code.
Any help to figure out what is wrong will be much appreciated.
Exceptions:
2012-11-02 03:14:43,659 [10] DEBUG Classes.Utils.MyHelper - DoesUserExist: Exception while trying to query Active Directory. at System.DirectoryServices.AccountManagement.Principal.MakePrincipal(PrincipalContext ctx, Type principalType)
at System.DirectoryServices.AccountManagement.SDSUtils.SearchResultToPrincipal(SearchResult sr, PrincipalContext owningContext, Type principalType)
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at
Classes.Utils.UserPrincipalExtension.FindByIdentity(PrincipalContext context, String identityValue) in C:\visual studio 2010\Projects\App\Classes\Utils\UserPrincipalExtension.cs:line 61
at App.Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 69
2012-11-02 03:14:43,659 [10] DEBUG App._Default - UserID_Changed: Exception trying to lookup user in Active Directory. at Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 94
at App._Default.UserID_Changed(Object sender, EventArgs e) in C:\visual studio 2010\Projects\App\Default.aspx.cs:line 105
Extension Class:
[DirectoryObjectClass("person")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalExtension : UserPrincipal
{
public UserPrincipalExtension(PrincipalContext context, string userName) : base(context) { }
public UserPrincipalExtension(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
: base(context,
samAccountName,
password,
enabled)
{
}
UserPrincipalExSearchFilter searchFilter;
new public UserPrincipalExSearchFilter AdvancedSearchFilter
{
get
{
if (null == searchFilter)
searchFilter = new UserPrincipalExSearchFilter(this);
return searchFilter;
}
}
[DirectoryProperty("extensionAttribute1")]
public string extensionAttribute1
{
get
{
if (ExtensionGet("extensionAttribute1").Length != 1)
return null;
return (string)ExtensionGet("extensionAttribute1")[0];
}
set { this.ExtensionSet("extensionAttribute1", value); }
}
public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
string identityValue)
{
return (UserPrincipalExtension)FindByIdentityWithType(context,
typeof(UserPrincipalExtension),
identityValue);
}
public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
IdentityType identityType,
string identityValue)
{
return (UserPrincipalExtension)FindByIdentityWithType(context,
typeof(UserPrincipalExtension),
identityType,
identityValue);
}
}
public class UserPrincipalExSearchFilter : AdvancedFilters
{
public UserPrincipalExSearchFilter(Principal p) : base(p) { }
}
Application Call:
public static bool DoesUserExist(string usr)
{
string methodName = "DoesUserExist: ";
bool result = false;
PrincipalContext context = null;
UserPrincipalExtension user = null;
try
{
context = new PrincipalContext(ContextType.Domain, "MYDOM.LOCAL");
user = UserPrincipalExtension.FindByIdentity(context, usr);
if ((user != null) && (user.extensionAttribute1 != null))
{
log(methodName, "User does exist. UserID=" + usr + "extension=" + user.extensionAttribute1);
result = true;
}
else
{
log(methodName, "User does NOT exist.");
}
}
catch (Exception ex)
{
log(methodName, "Exception while trying to query Active Directory. " + ex.StackTrace);
throw ex;
}
finally
{
user.Dispose();
context.Dispose();
}
return result;
}
|
|
|
|
|
Log the entire exception, not just the stacktrace. There are six lines on which "an" exception might occur. Can you point out to us which line it is?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
It seems that it complains about the Extension class not having the right constructor. But I do have it. Please see the original post and you will see the constructor that the exception is saying I don't have.
Here is the entire trace:
2012-11-02 10:24:41,921 [10] DEBUG App.Classes.Utils.MyHelper - DoesUserExist: Exception: System.NotSupportedException: Extension class must define a constructor that accepts a PrincipalContext argument.
at System.DirectoryServices.AccountManagement.Principal.MakePrincipal(PrincipalContext ctx, Type principalType)
at System.DirectoryServices.AccountManagement.SDSUtils.SearchResultToPrincipal(SearchResult sr, PrincipalContext owningContext, Type principalType)
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at App.Classes.Utils.UserPrincipalExtension.FindByIdentity(PrincipalContext context, String identityValue) in C:\visual studio 2010\Projects\App\Classes\Utils\UserPrincipalExtension.cs:line 61
at App.Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 70
Code where exception happened(Line 61 in the Extention class):
public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
string identityValue)
{
return (UserPrincipalExtension)FindByIdentityWithType(context,
typeof(UserPrincipalExtension),
identityValue);
}
Problem happens in the return line of that code.
|
|
|
|
|
I see one constructor, where your base-class defines two of them; is there an implementation for this[^] signature?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy,
Thanks a lot for the observation. I am glad I posted here to have other eyes look at it. I cannot believe I had such a small issue. hehe. The problem was that I had an extra parameter sent in to my basic constructor and that was not good. I am not even sure why I was sending in the userName.
Here is what I had:
Faulty Constructor:
public UserPrincipalExtension(PrincipalContext context, string userName) : base(context) { }
Fixed Constructor:
public UserPrincipalExtension(PrincipalContext context) : base(context) { }
Thanks so much for all of your help.
|
|
|
|
|
The law states that "given enough eyeballs, all bugs are shallow"
You'd have found it in the morning
|
|
|
|
|
public interface IMyInterface
{
string myInterfaceProp{ get; }
}
public class MyClass :IMyInterface
{
public string myInterfaceProp
{
get { return "some value"; }
}
}
public class UserClass
{
MyClass objMyClass =new MyClass ();
objMyClass.
}
How to hide the myInterfaceProp in the userclass when the "MyClass " instance is accessed? .
modified 4-Nov-12 22:00pm.
|
|
|
|
|
Your question is not clear, what exactly are you trying to achieve here? If you make it private or protected then it will be inaccessible.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
You cant hide an interface method.
An interface method has no implementation so you need not worry about hiding it.
|
|
|
|
|
The whole point of an interface is that it defines, well, an interface: a public contract for your class, or for third party classes, and the methods and properties that it will expose. It doesn't define any content, it simply states 'if a class says it implements me, you must be able to do these operations on it'.
Therefore it doesn't make sense to put protected methods or properties on an interface. If you don't want a property to be visible to the outside world, then don't add it to the interface!
|
|
|
|
|
Your question is a bit vague, but I'll have a go.
If you mean that you don't want to list myInterfaceProp as a member of the MyClass class (i.e. in Intellisense), implement the interface explicitly (What you're doing in your snippet is called implicit interface implementation).
Try this:
public class MyClass : IMyInterface
{
string IMyInterface.myInterfaceProp
{
get { return "some value"; }
}
}
That way you want have access to this member if you try to access it directly from MyClass , but you'll need a cast to IMyInterface .
2A
|
|
|
|
|