|
S. Senthil Kumar wrote: The typical solution is to move the initialization code to the constructor of the class.
Cool. Moving the initialization of colorValues, shapeValues, and allPolygons to the constructor worked. Thanks.
I think the full code design is "cleaner" when I declare the colorValues/shapeValues arrays as static (as mentioned below).
Thank you everyone.
|
|
|
|
|
Okay, I think it is the "cannot reference the non-static" part of the error which is relevant.
If I move the Main() code into a class:
class MyClass
{
Array colorValues = Enum.GetValues(typeof(ColorType));
Array shapeValues = Enum.GetValues(typeof(ShapeType));
Polygon[] allPolygons = new Polygon[colorValues.Length * shapeValues.Length];
public MyClass()
{
int index = 0;
foreach (ColorType color in colorValues)
{
foreach (ShapeType shape in shapeValues)
{
allPolygons[index] = new Polygon(color, shape);
Console.WriteLine(allPolygons[index]);
index++;
}
}
}
}
...and call it from Main()
static void Main(string[] args)
{
MyClass myClass = new MyClass();
}
I get the error. But how do I work around that? How would I create the private allPolygons array? What am I actually doing wrong?
|
|
|
|
|
ssclaire wrote: Array colorValues = Enum.GetValues(typeof(ColorType)); Array shapeValues = Enum.GetValues(typeof(ShapeType)); Polygon[] allPolygons = new Polygon[colorValues.Length * shapeValues.Length];
Seems to me like these should be static anyhow, they don't change between instances, right ?
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 )
|
|
|
|
|
Just move the code to the constructor and you'll be fine
class MyClass
{
Array colorValues = Enum.GetValues(typeof(ColorType));
Array shapeValues = Enum.GetValues(typeof(ShapeType));
Polygon[] allPolygons;
public MyClass()
{
allPolygons = = new Polygon[colorValues.Length * shapeValues.Length];
...
}
}
|
|
|
|
|
ssclaire wrote: static void Main(string[] args) { Array colorValues = Enum.GetValues(typeof(ColorType)); Array shapeValues = Enum.GetValues(typeof(ShapeType)); Polygon[] allPolygons = new Polygon[colorValues.Length * shapeValues.Length];
If a method is static, it can't access a non static property as it has no instance to refer to.
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 )
|
|
|
|
|
Hi
Thanks for existing, as I would be totaly lost without you and others willing to share.
I am a beginner and need help.
I get this error when running the following to get members fo the Administrators group.
"Compiler Error Message: CS1519: Invalid token 'foreach' in class, struct, or interface member declaration"
I want to learn to access AD via C# and am having fits.
Thanks for being there.
Marvin
DirectoryEntry localMachine = new DirectoryEntry
("WinNT://" + Environment.MachineName + ".");
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
Console.WriteLine(member.Name);
}}
End if
|
|
|
|
|
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.
|
|
|
|