|
Hello
FYI
This is the solution
In my form, I declare a bool attribute : DisableCtx;
Set to true or false depending the context
And I use the opening event
ctxmnuPB.Opening += delegate(object sender, CancelEventArgs args)
{
args.Cancel = DisableCtx;
};
|
|
|
|
|
Hi,
Thanks for sharing your knowledge
|
|
|
|
|
Hi everybody,
I'm feeling a bit dumb here - right now I'm learning some of the nice features of C# 3.0 and can't get this code to compile:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public Customer() { }
}
List<Customer> listOfCustomers =
new List<Customer> {
{ Id = 1, Name="Dave", City="Sarasota" },
{ Id = 2, Name="John", City="Tampa" },
{ Id = 3, Name="Abe", City="Miami" }
};
Errors are, for each item in the array:
The name 'Id' does not exist in the current context
The name 'City' does not exist in the current context
What's wrong with this code? I've taken the example code from this site[^]
I'm using VS 2008 with .NET 3.5 as target platform.
regards
modified on Tuesday, May 13, 2008 3:11 PM
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg,
Are you trying to build a List of Customers?
public Customer(int id, string name, string city)
{
Id = id;
Name = name;
City = city;
}
List<Customer> new List<Customer> {
new Customer(1, "Dave", "Sarasota"),
new Customer(2, "John", "Tampa"),
new Customer(3, "Abe", "Miami")
};
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Gareth H wrote: Are you trying to build a List of Customers?
Argh, I forgot to replace the < and > signs with their html code.
Yeah, I want to create a list of customers.
But the example on the site I mentioned does create this list without using new Customer() at all, and this does not compile on my machine.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg,
Am not sure. Seems this is a compile setting, I have never seen it before.
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
He's got himself all confused to put it bluntly. You can initialise collections using the new expressions, however you have to do as Gareth H pointed out, i.e.:
var customers = new List<Customer>()
{
new Customer() { Id = 0, Name = "Dave", City = "Sarasota" },
}; Where he's got mixed up is with the anonymous types, you could if you don't need to use the customers outside your existing method do the following:
var customers = new[]
{
new { Id = 0, Name = "Dave", City = "Sarasota" },
new { Id = 1, Name = "Ed", City = "London" },
};
foreach (var customer in customers)
{
Console.WriteLine("{0} [{1}]", customer.Name, customer.City);
} Which means you don't have to explicitly create a class to hold the data. However as I mentioned you cannot pass this data (directly) to another class or even function within the same class. What the compiler does is generate a "random" new class automatically (something like <>A_$72832$ for example, which is illegal in anything but IL).
But you can't mix the two, although it would save a little typing.
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
|
|
|
|
|
Thanks. Seems like the author of the article didn't even check the code he was writing
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Possibly writing it from memory, although if I write stuff like that I tend to fire up VS2008 anyway and type it in because it's faster with Intellisense.
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
|
|
|
|
|
hi everybody i got problem in making search in a list using delegate.
i got a list of article here is my class article
public class Article <br />
{<br />
public Article();<br />
public List<Component> Components { get; set; }<br />
public int ID;<br />
}<br />
<br />
here is my class Component<br />
public class Component<br />
{<br />
public Component(); <br />
public int Quantity { get; set; }<br />
}
using this code to make search it works
List <Article> ListResult = ListArticle.FindAll(delegate(Article art) { return art.ID == 99; }); It works i got all articles with the ID equals to 99
but when i m trying to do filter in components i got a problem in compiling this code
List <Article> ListResult = ListArticle.FindAll(delegate(Article art)<br />
{<br />
for (int i = 0; i< art.Component.Count; i++)<br />
{<br />
return art.Component[i].Quantity.Equals(20);<br />
}<br />
});
I want to search in all article and in the collection of Component to retrieve those who has Quantity=20
Thank you for help
|
|
|
|
|
You got two minor errors:
List<article> ListResult = ListArticle.FindAll(delegate(Article art)
{
for (int i = 0; i < art.Component<code>s</code>.Count; i++)
{
return art.Component<code>s</code>[i].Quantity.Equals(20);
}
<code>return false;</code>
});</article>
1) Components instead of Component, because that's how the property is called in your Article definition
2) your delegate must return something in case there are no Components in the list, otherwise you get the "not all code paths return a value" compiler error
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
papy-boom wrote: return art.Component[i].Quantity.Equals(20);
This is going to return on the first element. You want
if (art.Components[i].Quantity.Equals(20)) return true;
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 )
|
|
|
|
|
yes but how can i do to return all element that reach the condition
|
|
|
|
|
In C++ I can ensure that objects passed to my function are not null by making them a reference like so:
void foo(MyObjectType &anObject) { ... }
Is it possible to do this in C#?
- Dy
|
|
|
|
|
Hi,
try this with your 'ensured' function
MyObjectType * p = NULL;
foo( *p);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Take a look at the ref Keyword[^]
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Personally, I'd check for that before I call foo.
if(myObjectType != null)
{
foo(myObjectType);
}
But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson Because programming is an art, not a science. Marc Clifton
|
|
|
|
|
Much better to do it inside the function since:
1. You only have to do the check once (of course, the caller has to code defensively).
2. There's a reason NullReferenceException is there.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
There's the ?? operator, I assume it would work when you call a method. But, in C# all classes are references. And, you can pass null through that function anyhow, so you can't do that in C#.
You have the ref keyword but it only works on value types. You can put 'ref' on a class param, but it makes no difference, it was a reference to start with
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: You have the ref keyword but it only works on value types
Can you explain? String is a predefined reference type, but when passed as a parameter, it is passed by value. To change the string, you have to explicitly mark it as ref in the signature
namespace RefChecker
{
class Program
{
static void changeMyString1(string myString)
{
myString += " worked";
}
static void changeMyString2(ref string myString)
{
myString += " worked";
}
static void Main(string[] args)
{
string string1 = "has it";
string string2 = "has it";
changeMyString1(string1);
changeMyString2(ref string2);
Console.WriteLine(string1);
Console.WriteLine(string2);
}
}
}
Knowledge is hereditary, it will find its way up or down.
Luc Pattyn
and since what every time when i want to add button to this control one add two times
posted in C# forum
|
|
|
|
|
The string class is a special case in a number of ways. Any other class, is passed by ref, always.
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: You have the ref keyword but it only works on value types. You can put 'ref' on a class param, but it makes no difference, it was a reference to start with
No, it does make a difference. If you pass a reference type with "ref", you are essentially passing a reference to the reference (something like a pointer to a pointer in C++). For example.
class A
{
public int x;
}
void Method()
{
A a = new A();
M1(ref a);
Console.WriteLine(a.x);
a = new A();
M2(a);
Console.WriteLine(a.x);
}
void M1(ref A a)
{
a = new A();
a.x = 1;
}
void M2(A a)
{
a = new A();
a.x = 2;
}
M1 takes a by ref and therefore assigning to a changes the reference itself. M2 takes a by value, so changing a doesn't change the a in Method.
|
|
|
|
|
:applause:
I can't believe how few people know about this. And I'm far from being a guru myself.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
You are actually assigned a new reference here, not changing the object the reference refers to. Try this...
class Program
{
class MyClass
{
public int x;
}
static void changeMyClass1(MyClass myClass )
{
myClass.x = 100;
}
static void changeMyClass2(ref MyClass myClass )
{
myClass.x = 100;
}
static void Main(string[] args)
{
MyClass instance1 = new MyClass();
MyClass instance2 = new MyClass();
instance1.x = 0;
instance2.x = 0;
changeMyClass1(instance1);
changeMyClass2(ref instance2);
Console.WriteLine(instance1.x);
Console.WriteLine(instance2.x);
}
}
Like CG said, they are by ref, by default. Both instances will change. Strings behave differently, which, in my opinion is bad.
Knowledge is hereditary, it will find its way up or down.
Luc Pattyn
and since what every time when i want to add button to this control one add two times
posted in C# forum
|
|
|
|
|
Malcolm Smart wrote: Like CG said, they are by ref, by default. Both instances will change.
Yes, but as you pointed out, passing the parameter with "ref" allows you to change the reference itself - which is different from changing the object being referenced. In your example, both methods only change the object being referenced, and there is no difference in that case.
My point was passing reference types for "ref" has a different meaning than passing them without "ref".
And strings don't behave differently - strings are immutable reference types. In your example,
static void changeMyString1(string myString)
{
myString = mystring + " worked";
}
static void changeMyString2(ref string myString)
{
myString = mystring + " worked";
}
the second method takes myString by ref, and therefore changes the reference itself. After both methods have executed, mystring will be referring to a new string object with " worked" concatentated. By passing myString as ref in the second method, the "original" myString that was passed to changeMyString2 changes to refer to the new string object. The first method doesn't take myString by ref, and therefore the original myString passed doesn't change.
If you know C++, the following piece of code demonstrates the difference.
int global = 10;
void Test()
{
int val = 0;
int *p = &val;
Method2(p);
Method1(&p);
}
void Method1(int **p)
{
*p = &g;
}
void Method2(int *p)
{
p = &g;
}
You can consider p to be equivalent to a reference type. Method2 is equivalent to not passing with "ref", and Method1 is equivalent to passing with "ref". Changing p inside Method2 doesn't affect p in Test, but changing *p in Method1 does.
|
|
|
|