|
IT needs to be in method or some other class member. EG:
class Foo
{
void Bar()
{
foreach (var a in b) ...
}
}
|
|
|
|
|
Hi
Thanks for your reply.
I tried:
<%@ Page Language="C#" %>
<% using System.DirectoryServices %>
<% using System.DirectoryServices.ActiveDirectory %>
<script runat="server">
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ".");
DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
object members = admGroup.Invoke("members", null);
void Bar()
{
foreach (object groupMember in (IEnumerable)members);
DirectoryEntry member = new DirectoryEntry(groupMember);
Console.WriteLine(member.Name);
}
And got this:
Line 8: DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ".");
|
|
|
|
|
My guess would be a missing bracket somewhere in the file, probably before the foreach statement.
|
|
|
|
|
If you are in VS, just press F1 to take you to the error documentation, it normally contains samples of what went wrong.
|
|
|
|
|
|
Email #1 (the one with the member id of 1).
|
|
|
|
|
Hi leppie
can u give me more explain ?
thanks.
|
|
|
|
|
|
Article needs to be approved before it appears in the list.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
I added a variable of type string to a class. I added getter and setters. Should new be called (for instance in constructor) before assigning it a value?
// "In the end it's a little boy expressing himself." Yanni
while (I_am_alive) { cout<<"I love to do more than just programming."; }
modified on Wednesday, June 11, 2008 1:31 PM
|
|
|
|
|
Hamed Mosavi wrote: Should new be called (for instance in constructor) before assigning it a value?
You mean like?
string foo = new string();
foo = "bar";
While I have seen this a lot, it is in fact completely absurd!
|
|
|
|
|
Almost that:
string foo;
public string Foo
{
get { return foo; }
set { foo= value; }
}
...
foo = new string();
....
class.Foo = "string";
The problem with me is my c++ background. Here foo in fact seems like a pointer. I ask myself do I need to make it point to somewhere before using it? Will the compiler calls new automatically. If I had more time, I would not post this question, but rather investigate MSIL code to find it out.
Thank you for reply.
// "In the end it's a little boy expressing himself." Yanni
while (I_am_alive) { cout<<"I love to do more than just programming."; }
|
|
|
|
|
You dont ever have to do that
The same rules a C (I dont know the quirks of C++).
It is null by default. And it acts like a const char* (in other words, you cannot mutate the characters) that you simply reference.
|
|
|
|
|
Thanks.
// "In the end it's a little boy expressing himself." Yanni
while (I_am_alive) { cout<<"I love to do more than just programming."; }
|
|
|
|
|
Hamed Mosavi wrote:
The problem with me is my c++ background. Here foo in fact seems like a pointer. I ask myself do I need to make it point to somewhere before using it? Will the compiler calls new automatically. If I had more time, I would not post this question, but rather investigate MSIL code to find it out.
Thank you for reply. [Rose]
This is a quite interesting question, actually. Since String is a class , you'd usually expect that you have to create a new instance when assigning a value to it (at least that's what you have to do with "normal" classes). I guess that the ldstr opcode in MSIL does have a special meaning for the .NET String keyword - just a guess though.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg wrote: This is a quite interesting question, actually. Since String is a class, you'd usually expect that you have to create a new instance when assigning a value to it
Exactly. This is my question in fact, since reference types need to be newed.
// "In the end it's a little boy expressing himself." Yanni
while (I_am_alive) { cout<<"I love to do more than just programming."; }
|
|
|
|
|
interstingly, something like this will result in a NullReferenceException:
string s = null;
string anotherString = s.Trim();
So my best guess is, that whenever using the assignment operator "= " something magic (;)) happens inside the .NET CLR.
There's also a discussion about this topic on google groups:
Topic 1[^]
Topic 2[^]
modified 12-Sep-18 21:01pm.
|
|
|
|
|
The NullReferenceException is because you are calling a method (Trim) on a null reference. There is no magic, you can assign null to references, but you obviously can't call methods or access properties/fields on a null reference.
|
|
|
|
|
The new keyword has to be used when creating a new instance of a string. However, most of the time you don't create any strings directly in your code.
The most common situation is the assignment of a literal string:
string s = "asdf";
In this case, the literal string "asdf" already exists as a constant in the assembly, so you are actually only assigning the reference of an already existing string object.
In most other situations you are calling a method that creates an instance of a string, so the actual creation is not in your code. Example:
string t = s.Substring(0, 1);
The Substring method creates a new instance of the String class and returns the reference, so you are only assigning the return value in your code.
You only use the new keyword when you create a string directly in your code:
string u = new string('*', 42); // creates a string containing 42 asterisks.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
This is the best answer I got. Thank you so much. I need to think the C#-way.
// "In the end it's a little boy expressing himself." Yanni
while (I_am_alive) { cout<<"I love to do more than just programming."; }
|
|
|
|
|
Greeeg wrote: This is a quite interesting question, actually. Since String is a class, you'd usually expect that you have to create a new instance when assigning a value to it (at least that's what you have to do with "normal" classes).
A string reference works just like a reference to any other class. You don't use the new keyword when assigning a reference, it's used to create an instance of a class. If the instance already exists, you just assign it's reference to the variable, and that is what happens most of the time when you are using a string.
Greeeg wrote: I guess that the ldstr opcode in MSIL does have a special meaning for the .NET String keyword
When the ldstr instruction is compiled, the string is created as a constant in the assembly. The runtime code created for the ldstr instruction just gets the reference of that string constant.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: A string reference works just like a reference to any other class. You don't use the new keyword when assigning a reference, it's used to create an instance of a class. If the instance already exists, you just assign it's reference to the variable, and that is what happens most of the time when you are using a string.
This is not entirely true. string is a special class. It is indeed a reference type but it behaves like a value type.
If it was a reference type like any other the following should return true but it doesn't.
<code>public static bool Test()
{
string s1 = "hello";
string s2 = s1;
s1 = "world";
return s1.Equals(s2);
}</code>
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
BadKarma wrote: This is not entirely true. string is a special class. It is indeed a reference type but it behaves like a value type.
There is nothing special about the string class. It may seem like it behaves like a value type, but actually it doesn't.
BadKarma wrote: If it was a reference type like any other the following should return true but it doesn't.
It's a reference type just like any other, and just as with any other reference type, replacing a reference to an object neither changes the content of the object nor any other reference to the object.
This is what the code is doing:
1. Declare a string reference called "s1".
2. Get the reference to the "hello" literal string.
3. Store the reference in the "s1" variable.
- The s1 variable now contains a reference to "hello".
4. Declare a string reference called "s2".
5. Get the reference from the "s1" variable.
6. Store the reference in the "s2" variable.
- The s2 variable now contains a reference to "hello".
7. Get the reference to the "world" literal string.
8. Store the reference in the "s1" variable.
- The s1 varaible now contains a reference to "world".
9. Compare the contents of the strings that s1 and s2 reference.
- As s1 references "world" and s2 references "hello", the comparison returns false.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
If you must do something, use string.Empty as a temporary value.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: If you must do something
Christian Graus wrote: use string.Empty as a temporary value
Will this cause the new to be called?
// "In the end it's a little boy expressing himself." Yanni
while (I_am_alive) { cout<<"I love to do more than just programming."; }
|
|
|
|