|
Accessing indexes requires that an object at that index (or with that index keyword, like the table style or column style name above) exists. If it doesn't, null is returned from the index property. When you try to call a method or access a property on null, you get a NullReferenceException .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I sometimes see it in a perfectly valid line of code. I simply cut and re-paste the line and the compiler is then happy!
|
|
|
|
|
Hi,
How does one enumerate the groups a user belongs to using the WinNT:// provider (not LDAP) ??
TIA,
Matt
|
|
|
|
|
Well, in VBScript, it looks something like this:
strComputer = "." ' Use a '.' to denote the local machine.
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
For Each objUser in objGroup.Members
If objUser.name = "username" Then
Wscript.Echo objGroup.Name
End If
Next
Next
You go through each group on the machine and check to see if the username exists in that group.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
hi
i want serialize and deserialize textbox properties for example font and backcolor,...
please help me and tell me what i do?
thanks
|
|
|
|
|
Read the XmlSerializer class documentation in the .NET Framework SDK. It includes a sample.
XML Serialization will, by default, serialize any public property or field, so serializing those properties is easy:
TextWriter writer = new XmlTextWriter("file.xml",
System.Text.Encoding.UTF8);
XmlSerializer serializer = new XmlSerializer(textBox1.GetType());
serializer.Serialize(writer, textBox1); See the documentation for more details.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
my code is
this is serialize of textbox:
XmlWriter xw = new XmlTextWriter(fs, System.Text.Encoding.ASCII);
xw.WriteStartElement("Form");
xw.WriteAttributeString("X", theText.Left.ToString());
xw.WriteAttributeString("Y", theText.Top.ToString());
xw.WriteAttributeString("Width", theText.Width.ToString());
xw.WriteAttributeString("Height", theText.Height.ToString());
xw.WriteAttributeString("Value", theText.Text);
xw.WriteAttributeString("Font", theText.Font.Tostring());
xw.WriteAttributeString("Backcolor", theText.Backcolor.Tostring());
xw.WriteEndElement();
XmlNodeReader reader = new XmlNodeReader(doc);reader.value is string but textbox.font or backcolor is Syste.Drawing
please tell me what i do?
thanks.
|
|
|
|
|
I'm not shore about the Font but with the Backcolor you can:
xw.WriteAttributeString("Backcolor", theText.BackColor.ToArgb().ToString());
and read it inn with
theText.BackColor = Color.FromArgb(Convert.ToInt32(reader.Value));
the font you might have to use
theText.Font.Name.ToString()
Hope it helps
Thomas
|
|
|
|
|
thanks for your help
but i should deserialize font and alignment of my textbox with reader.value ,too
please tell me what i do?
thanks.
|
|
|
|
|
You should be using TypeConverter s for the whole thing, otherwise use ToString . Many classes can't be reconstituted from their ToString representations. If you use XmlSerializer as I mentioned, this is all done for you. But, if you insist on doing things the hard way, you can get a TypeConverter reference for each type using something like this:
TypeConverter converter = TypeDescriptor.GetConverter(theText.Left);
xw.WriteAttributeString("X", converter.ConvertToString(theText.Left));
converter = TypeDescriptor.GetConverter(theText.Top);
xw.WriteAttributeString("Y", converter.ConvertToString(theText.Top));
xw.WriteEndElement();
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
the serialize code:
xw.WriteAttributeString("Font",theText.Font.Name.ToString());
and it is deserialize code,i use convector but it dosent work properly.
FontConverter ff=new FontConverter();
theTextBox.Font=(System.Drawing.Font )ff.ConvertFromString(reader.Value );
what i do?
thanks.
|
|
|
|
|
You don't instantiate the FontConverter itself, which is documented if you bother to read it. Always use TypeDescriptor.GetConverter .
And instead of saying "doesn't work properly" - which is has in my experience - you might want to be more specific about what's not working, since I can't really help if I don't know what the problem you're having is.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks for reply
i use from TypeConvector in my code but c# dosent know it.
my serialize or save code is:
xw.WriteStartElement("TextBox");
xw.WriteAttributeString("X", theText.Left.ToString());
xw.WriteAttributeString("Y", theText.Top.ToString());
xw.WriteAttributeString("Width",c.Width.ToString());
xw.WriteAttributeString("Height",c.Height.ToString()); xw.WriteAttributeString("Value", c.Text);
TypeConverter converter = TypeDescriptor.GetConverter(theText.Font); xw.WriteAttributeString("Font", converter.ConvertToString(theText.Font))
xw.WriteAttributeString("TextAlign",theText.TextAlign.ToString());
xw.WriteAttributeString("Backcolor",c.BackColor.ToArgb().ToString());
xw.WriteAttributeString("Forecolor",c.ForeColor.ToArgb().ToString());
xw.WriteEndElement();
and my deserialize or read code is:
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i); switch(reader.Name)
{
case "X":
theTextBox.SetBounds(Convert.ToInt32(reader.Value),
theTextBox.Top, theTextBox.Width, theTextBox.Height);
break; case "Y": theTextBox.SetBounds(theTextBox.Left,
Convert.ToInt32(reader.Value), theTextBox.Width,theTextBox.Height); break;
.. case "Font": TypeConverter ff = TypeDescriptor.GetConverter(reader.value);
theTextBox.Font=(System.Drawing.Font)ff.ConvertFromString (reader.Value );
break;
case "Backcolor":
theTextBox.BackColor= Color.FromArgb(Convert.ToInt32(reader.Value));
break;
please tell me,what i do for my deserialize code?
|
|
|
|
|
So what exactly is the error? Be specific. What's the exceptio type, if any? What's the exception message read.
And as I said originally, you really should be 1) using a TypeConverter for everything (this is what the XML Serialization routines do), or 2) just using the XmlSerializer , which takes care of all this for you. All that code you've typed could be wrapped up into 3 or 4 lines. use what's available in the BCL unless you have a good reason for re-implementing everything yourself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Also take a look at BinaryFormatter and SoapFormatter classes which can be used to serialize objects to a stream e.g. a filestream.
They cannot be used to serialize a whole TextBox instance as it's not marked as serializable, but they work with specific fields as most of them are simpler types which can be serialized.
www.troschuetz.de
|
|
|
|
|
Hi
I want to reload a page with a button. The page I wish to reload lies in an i frame, the button that I wish to use to reload lies in another i frame. I have this code in the *.aspx file and it works. The code lies behind a link, and I wish to have it behind a button.
href="javascript:location.reload()" target="sub_top"
can I use this code in code-behind? how do I do this?
Thanks!
-- Evil geniuses for a better tomorrow --
|
|
|
|
|
No, you can't use that code in the code-behind. Since the code-behind will regenerate the entire HTML page anyway, you'll just end up loading the entire frameset. You're better off putting that code in the onclick handler of a button.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Code project C# guru’s ,
I am attempting to implement RFC 2095 ( http://www.ietf.org/rfc/rfc2095.txt ) I’ve found what I believe is an error in the crypto library for C#.
Now be kind I’ve been coding C# for exactly 1 week , and it has been about 7 years since I coded in production . The last time I coded was in VI on Unix. This is just one of the methods I’m attempting to implement in this project. The rest of the project includes an SMTP server, and verification schema.
I’m implementing the procedures in a C# program found in RFC 2095, and using the CRAM algorithm found in http://www.ietf.org/rfc/rfc2104.txt . The test data strings found in RFC 2095 include a share key string “tanstaaftanstaaf” and the challenge “<1896.697170952@postoffice.reston.mci.net>” these test values should produce according to the test in the RFC the hashed hex output “b9 13 a6 02 c7 ed a7 a4 95 b4 e6 e7 33 4d 38 90” .
In the following program from the MSDN code library (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconextendingkeyedhashalgorithmclass.htm) I get the following output from the output string B9 13 A6 2 C7 ED A7 A4 95 B4 E6 E7 33 4D 38 90 on the test case. And, of course the fourth hex pair has dropped the leading zero .
That would be wrong according to the Keyed MD-5 hash algorithm . The leading zero should be left in place. From my feeble attempts at debugging it looks like it is either the output of the HEX according to Microsoft or internal to the crypto library. Either way how do I make sure that it does not drop that leading zero ?
Thanks in advance,
Sam
From the MSDN Code Library with RFC 2095 Edits
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Security.Cryptography;
public class TestHMACMD5
{
static private void PrintByteArray(Byte[] arr)
{
int i;
Console.WriteLine("Length: " + arr.Length);
for (i=0; i<arr.length; i++)=""
="" {
="" console.write("{0:x}",="" arr[i]);
="" console.write("="" ");
="" if="" (="" (i+9)%8="=" 0="" )="" console.writeline();
="" }
="" (i%8="" !="0)" public="" static="" void="" main()="" create="" a="" key.
="" byte[]="" key1="{0x0b," 0x0b,="" 0x0b};
="" pass="" the="" key="" to="" constructor="" of="" hmacmd5="" class.="" hmac1="new" hmacmd5(key1);
="" another="" added="" test="" case="" for="" rfc="" 2095
="" key2="System.Text.Encoding.ASCII.GetBytes("tanstaaftanstaaf");" hmac2="new" hmacmd5(key2);
="" encode="" string="" into="" byte="" array,="" hash="" array,
="" and="" print="" screen.
="" data1="System.Text.Encoding.ASCII.GetBytes("KeyString" printbytearray(hmac1.computehash(data1));
="" 2095
="" data2="System.Text.Encoding.ASCII.GetBytes("<1896.697170952@postoffice.reston.mci.net">");
PrintByteArray(hmac2.ComputeHash(data2));
}
}
public class HMACMD5 : KeyedHashAlgorithm
{
private MD5 hash1;
private MD5 hash2;
private bool bHashing = false;
private byte[] rgbInner = new byte[64];
private byte[] rgbOuter = new byte[64];
public HMACMD5 (byte[] rgbKey)
{
HashSizeValue = 128;
// Create the hash algorithms.
hash1 = MD5.Create();
hash2 = MD5.Create();
// Get the key.
if (rgbKey.Length > 64)
{
KeyValue = hash1.ComputeHash(rgbKey);
// No need to call Initialize, ComputeHash does it automatically.
}
else
{
KeyValue = (byte[]) rgbKey.Clone();
}
// Compute rgbInner and rgbOuter.
int i = 0;
for (i=0; i<64; i++)
{
rgbInner[i] = 0x36;
rgbOuter[i] = 0x5C;
}
for (i=0; i<keyvalue.length; i++)=""
="" {
="" rgbinner[i]="" ^="KeyValue[i];
" rgbouter[i]="" }=""
="" public="" override="" byte[]="" key="" get="" {="" return="" (byte[])="" keyvalue.clone();="" }
="" set="" if="" (bhashing)="" throw="" new="" exception("cannot="" change="" during="" hash="" operation");
="" (value.length=""> 64)
{
KeyValue = hash1.ComputeHash(value);
// No need to call Initialize, ComputeHash does it automatically.
}
else
{
KeyValue = (byte[]) value.Clone();
}
// Compute rgbInner and rgbOuter.
int i = 0;
for (i=0; i<64; i++)
{
rgbInner[i] = 0x36;
rgbOuter[i] = 0x5C;
}
for (i=0; i
|
|
|
|
|
The bug is in your code. If MD5 didn't work once, it wouldn't work for any plain text.
Use Console.Write("{0:X2}", arr[i]) instead of just using "{0:X}" for the format specifier. This makes sure that 2 chars are used per bit.
Also, it's faster to use arr[i].ToString("X2") in this case, and this is typically true when you need to format only one value. Why? Because using Console.Write (or any of the formatting methods) instantiates a StringBuilder and does some having parsing to eventually call Byte.ToString(string, IFormatProvider) , which you could just do directly here (though you don't need to pass an IFormatProvider in this case).
It's best you don't claim bugs, especially when you've only been programming C# for 1 week. Check out the documentation, search the 'net for similar findings, and ask someone to verify (like on this forum), then declare you've found a bug and report it to the proper channels.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
It's best you don't claim bugs, especially when you've only been programming C# for 1 week. Check out the documentation, search the 'net for similar findings, and ask someone to verify (like on this forum), then declare you've found a bug and report it to the proper ch
First. Thank you very much.
Second. I was using the example code from Microsoft on their implemenatation of the correct code (which is still obviously wrong according to the RFC). I did check the net for this very topic (very thin and obscure task). I did ask somebody to verify but they were looking at the entire code and easily missed the problem.
Third. It was an obvious small error on my part that I should have caught. I just never figured that Microsoft would provide a sample of an RFC that wasn't compliant. That is exactly why I thought to ask the kind folks here at Code Project for help.
Fourth I wasn't claiming a bug and was looking for the exact validation you provided in that the Microsoft code as supplied was wrong and not something deeper. They should change that one item to make it RFC compliant.
Fifth. Thanks again. Sorry if you thought I was calling your baby ugly. Haven't you ever beat your head on the keyboard looking at the obvious to somebody else while completely missing the EASY! answer?
Again thank you very much for the help.... Now to finish the project. And, I think C# is pretty darn nifty......Thanks again, and again.... Really...
--------------------
Sam
|
|
|
|
|
The samples in the documentation is riddled with bugs. Look at it - hopefully it helps you to understand if the class documentation doesn't - but never copy it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
selil wrote:
static private void PrintByteArray(Byte[] arr)
{
int i;
Console.WriteLine("Length: " + arr.Length);
for (i=0; i {
Console.Write("{0:X}", arr[i]);
Console.Write(" ");
if ( (i+9)%8 == 0 ) Console.WriteLine();
}
if (i%8 != 0) Console.WriteLine();
}
This is happening because the format specifier in {0:X} is dropping the leading zero. The value is represented with the leading zero (obviously, since the value is 2), but the leading zero is just not printed.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
THANK YOU! Too!
I'm working on a coding project for a class (500 level course) to implement a secure email (schema) login procedure. I was looking to learn C# since I had heard about how easy it is to utilize for fast development. The hardest thing to learn has been where certain libraries are located. Doing the windows programming was a breeze, but I beat my head trying to figure out where this particular issue was (I implicitly trusted the MS code). I used to do a lot of programming in VC++, and Java and everybody seems to think C# is the wave of the future (RFC 2095 and RFC 2104) so to increase the level of difficulty I switched. So I'm writing an IMAP server and IMAP mail client for my first C# project. I have three days to complete the assignment in a new language, against sketchy RFC's, and it must be fully functional by tomorrow morning... Yee haw.
Thanks again!
--------------------
Sam
|
|
|
|
|
We know we can use propertyGrid to configure object,but how can we use it to configure html component's property such 's property.This condition is like ASP.NET'S develop entironment??
ok
|
|
|
|
|
You mind elaborating a little? Are you trying to assign or change HTML for a control? Why not just switch to HTML view?
If this question is specific to ASP.NET, please post it in the ASP.NET forum. If you're asking about design-time support, read Enhancing Design Time Support[^] in the .NET Framework and ask about any specific questions you have here.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|