|
Its only valid at the class level (not the method level) but you can use the readonly keyword on your variable declaration. It will then be a compile error to change the reference outside of the constructor.
[edit]
duh forgot the handy example
class Test {
private readonly MyClass foo;
public Test()
{
foo = new MyClass();
}
} [/edit]
James
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
|
Hi!
Is there any simple method to detect which Link on linkLabel has been clicked before context menu opening?
I've got linkLabel with context menu set to some contextMenu. When I click LMB, the linkLabel1_LinkClicked event fires ok. But RMB does not fire this - just contextMenu appears, without any info about current link. Any ideas?
h.
|
|
|
|
|
if you set the ContextMenu property back to null you should be able to handle the RMB event as you want, then use the ContextMenu's Show method to show the correct context menu.
James
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
|
|
|
|
|
It works!
I was sure it'll be easy.
Thanks
h.
|
|
|
|
|
Got some Rich Text, want it in a Word document after klicking a button
but have no clude howto do this, Crystal Reports rip the text apart...
any ideas ? have i overseen something ?
|
|
|
|
|
hi !
take a look at this:
<sdk>v1.1\Samples\Technologies\Interop\Applications\Office\Word
there is a small demo-application provided by the sdk-samples.
andi
|
|
|
|
|
sry, but i dont get the point....
i didnt find a similar directory, and also nothing in the help
|
|
|
|
|
I need to apply arithmatic operations on string i.e.
string1 = "12";
string2 = "10";
string3 = string1+ string2;
and now I want string3 to contain "22"
currently I'm using functions like Add(string1, string2) which is a pain.
The problem is that the above statement (string3 = string1+ string2) will concatenate the two strings not add them and overloading the + operator doesn't work. The compiler gives an error saying One of the parameters of a binary operator must be a containing type. If I make my own String class, the overloading works fine.
Anyone got any ideas?
thanks in advance...hasan
|
|
|
|
|
You have to convert them to a numeric type first.
string3=(Int32.Parse(string1)+Int32.Parse(string2)).ToString();
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
right. But I don't want to have to convert the stings to ints every time as i'm using around 15 for each calculation and doing what you suggested would make the code sort of confusing.
I think it can be done using operator overloading so that the developer can add two strings simply by writing
string3=string1+string2
Anyone with some other solution?
|
|
|
|
|
public static string operator + ( string s1, string s2 )
{
return (Int32.Parse(s1) + Int32.Parse(s2)).ToString();
}
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Your solution
Ista wrote:
public static string operator + ( string s1, string s2 )
{
return (Int32.Parse(s1) + Int32.Parse(s2)).ToString();
}
doesn't work...I've had already tried it. The compiler generates an error: One of the parameters of a binary operator must be a containing type". So the + operator can't be overloaded in this way.
|
|
|
|
|
This is the class
public class Class1
{
public string s;
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public Class1( string stype )
{
s = stype;
}
public override string ToString()
{
return s;
}
public static string operator + ( Class1 s1, Class1 s2 )
{
string s1s = s1.ToString();
string s2s = s2.ToString();
return (Int32.Parse(s1s) + Int32.Parse(s2s)).ToString();
}
}
in your form type
private void button1_Click(object sender, System.EventArgs e)
{
Class1 c = new Class1("12");
Class1 c1 = new Class1("20");
string s = c + c1;
MessageBox.Show(s);
}
its rough but works
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
Just want to point something out: Can you imagine the maintainance nightmare that would result from doing what you propose? If you read
string3 = string1 + string2;
in your code 6 or 12 months from now, would you know whether it meant "concatenate" or "treat the strings as numbers and add"? Would your replacement?
What I'm saying is that although the language might allow you to do what you want, do you really want to do it? It's great writing software that's only used once, to prove a point, as it were, but the reality of our game is that we generally have to write code that we'll have to maintain down the road (and if not us, then someone else). In that case, the simpler you express the intent of your program in code, the simpler it is to test and maintain, and the easier it is to go home at 5 o'clock.
My recommendation is to create a small class that encapsulates what you want to do. Override the + operator (and the += operator, and the - operator and the -= operator) to your heart's content at that point. It won't be as confusing as "sometimes in my code the + operator on strings means this, other times it means that".
Cheers, Julian
Program Manager, C#
This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
|
|
Hmm...I believe you're right about the maintinence...overloading would cause a lot of confusion so I guess I'll just use my Add & Sub functions.
However you've mentioned that "although the language might allow you to do what you want, do you really want to do it?"
and I'm curious how does the languge allow me to do what I want i.e how does it allow me to overload +, - operators etc. for primitive datatypes like int and sting.
Hasan
P.S. The part about going home at 5 went straight to my heart
|
|
|
|
|
P.S. The part about going home at 5 went straight to my heart
nice said
Muhammad Sarfraz
|
|
|
|
|
The fact of the matter is, that unless you write your own CLR, you'll have to convert to numbers (Int32 , Int64 , whatever) even if you create your own class and overload the + operator - there is no way around it. All you'll be doing then, however, is making it look like you're not - it's still happening under the covers.
Using the Add(string, string) method you mentioned earlier is the best way - always modularize your code. This will allow you to use a simple, short statement to add the numeric values of the strings and will allow you to optimize the algorithm once in the future as opposed to finding all lines where you do this and change them. I recommend using a static method too, so you don't have to worry about instantiating some class just to add a couple of strings values.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I'm curious. Assuming you could overload this operator, what would you do with the following code:
string1 = "12";
string2 = "10";
string3 = "abc";
string3 = string1 + string2 + string3;
Would you want this to be "22abc" or "1210abc"?
There's a good reason not to do this - it's confusing for developers maintaining your code. Personally, I think you should either create a new class and overload or you should just continue using the Add() method.
|
|
|
|
|
I've decided to continue using my Add() method.
However your point about the "abc" string; since I would be converting the string to an int in my overloaded method, I'd get an error in case "abc" were used; so to avoid this I'd check that the user only enters numeric data.
that out of the way, I'd still like to know how to overload + for string or int. (just curios).
thanks....hasan
|
|
|
|
|
Imagine if someone developed a language where any variable could be of any value type - now that would be just silly, but at least your scenario would work
|
|
|
|
|
actually it would equal zero but god if I was maintaining that could I would be throwing punches I mean really int32.Parse() is very short to right
I'm not an expert yet, but I play one at work. Yeah and here too.
|
|
|
|
|
I need the ability to get/set permission attributes, such
as the ones you get when you right click
Properties/Security tab for a given file or dir in file manager...
How can this be done programmatically in .NET...???
thanks,
|
|
|
|
|
int binToHex1=(0*2^3)+(1*2^2)+(0*2^1)+(1*2^0);
Howcome that won't evaluate the 5, the right answer... it always comes out to 6!?!
/\ |_ E X E GG
|
|
|
|
|
That is because in C# ^ is not the exponentiation operator. It computes the bitwise exclusive-OR of its operands. Try this:
double binToHex=(0*Math.Pow(2,3))+(1*Math.Pow(2,2))+(0*Math.Pow(2,1))+(1*Math.Pow(2,0));
The result will be binToHex=5
|
|
|
|