|
Of any URL? And what URLs would those be? Internet Explorer isn't the only application that makes use of monikers like URLs. And are you talking about URLs of a running instance of Internet Explorer? If so, you can create an interop assembly (RCW) from shdocvw.dll and use the InternetExplorerClass to either create or access a running instance, from which you can then get the URL in a number of ways like the InternetExplorerClass.LocationURL property.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
I have BitArray like this...
bool[] myBools = new bool[8] {true,false,true,false,true,false,true,false,};
BitArray bitArrayBools = new BitArray( myBools );
How do i convert this BitArray to it's integer value ???
Any help will be greatly appreciated !!!!
saleem
|
|
|
|
|
|
Consider the following C# code -
class TestClass
{
public string str;
}
class TestApplication
{
static void Upper1 ( string strParam )
{
strParam = strParam.ToUpper ();
}
static void Upper2( TestClass tc )
{
tc.str = (tc.str) .ToUpper ();
}
static void Main ( string[] args )
{
string s = "lower 1";
Upper1 ( s );
Console.WriteLine ( s );
TestClass tc = new TestClass();
tc.str = "lower 2";
Upper2 ( tc );
Console.WriteLine ( tc.str );
}
}
The output of this code on execution is as follows -
lower 1
LOWER 2
Press any key to continue
I am new to C# and totally confused over the outcome. As far as I know both "string" and "class type" are reference types in C#. When I am calling "Upper1" with a string as parameter it is behaving as if the parameter was passed "by value" and the changes are not visible in the calling function.
But if I wrap the string within a class and pass an instance of the class as argument to "Upper2", it is behaving as expected.
Can anyone help me in solving this mystery?
Thanks a lot in advance.
Siddhartha
|
|
|
|
|
In the first method:
static void Upper1( string strParam )
{
strParam = strParam.ToUpper ();
} strParam is a copy of the reference to a string you pass in. So any modifications you do to strParam in this method gets done to this copy. This copy will not be visible outside its scope. (I'm guessing this is what you expected it to be.)
In the second method:
static void Upper2( TestClass tc )
{
tc.str = tc.str.ToUpper ();
} tc is a copy of the reference to the object you pass in. It is only the reference that is beeing copied (so the reference points at the same single object that was passed in.) Then you alter stuff in that object. This is done to the actual tc object you pass in.
|
|
|
|
|
Thanks Arjan for your prompt reply.
But still I have one doubt in my mind. It looks like inspite of being of "reference type", C# handle strings much like "value types" (May be because string is used in an application as frequently as int, float, char etc.).
Is it true?
|
|
|
|
|
Strings are immutable which basically means they CAN NEVER CHANGE - they are simply replaced in memory. Take this example;
string str1 = "Hello World";
string str2 = "goodbye cruel world";
str1 = "changed".
str2 = "Hello World";
The strings are initially set. When str1 is set to changed it is actually a completely new string - but the string Hello World still exists and is REUSED for str2.
Another example of immutability
str1 = "hello" + " world".
There have just been 3 strings created. "Hello", "World" and then a third string long enough to contain "Hello World" where the originals are copied over to.
This is strange behaviour looking at it from your example above - but not in the world of computers where memory is king and efficient reuse thereof is the ultimate goal.
|
|
|
|
|
Thanks a lot J4amieC. But my question still remains unanswered.
"Does C# handles string much like "value type" even though it is of "reference type" ?".
Clarification will be highly appreciated.
|
|
|
|
|
No it doesn't, string is a reference type.
<br />
class Test<br />
{<br />
<br />
public int x;<br />
}<br />
<br />
void Foo(Test t)<br />
{<br />
t = new Test();<br />
t.x = 2;<br />
}<br />
<br />
void Bar()<br />
{<br />
Test t = new Test();<br />
t.x = 5;<br />
Console.WriteLine(t.x);<br />
Foo(t);<br />
Console.WriteLine(t.x);<br />
}<br />
What do you expect the output to be? It happens to be 5 and 2. If you comment out the t = new Test() in function Foo(), you'll get 2 and 2. So it's not just strings, all reference types have the references passed by value, so reassigning t in Foo() didn't affect the one in Bar(). This is exactly what you experienced with string too.
string s are special in that there is no way you can change a string, so it allows the compiler and the runtime to do some tricks (search for string interning). Basically it allows the runtime to maintain a string pool so that only one copy of a string exists, if you create a string which already exists, you simply get a reference to that string object. Also, string comparison checks if the strings are in the pool and if so, a simple reference check is enough to find if they are equal..
Regards
Senthil
|
|
|
|
|
Hi Senthil,
I ran the following code
using System;
class TestApp
{
class Test
{
public int x;
}
void Foo(Test t)
{
t = new Test();
t.x = 2;
}
public void Bar()
{
Test t = new Test();
t.x = 5;
Console.WriteLine(t.x);
Foo(t);
Console.WriteLine(t.x);
}
public static void Main()
{
TestApp ta = new TestApp();
ta.Bar();
}
}
Output is
---------
With "t = new Test();" in Foo() : 5, 5
After commenting "t = new Test();" in Foo() : 5, 2
This is also as per my expectation.
How did you expect 5, 2 and 2, 2 in your example.
Please clarify
Regards
Siddhartha
|
|
|
|
|
Oops. My bad. Guess I shouldn't be typing answers late in the night. Yes, it should be 5,5 and 5,2. Well, if it is what you expected, why do you say string is treated like a value type? Only because Test is a reference type, setting t.x = 2 in Foo() changed the value (after commenting out t = new Test());
Regards
Senthil
|
|
|
|
|
Let me frame my doubt with the following code.
using System;
class Test
{
public static void Append(string strParam)
{
strParam = strParam + " - C#";
}
public static void Main()
{
string strMsg = "Hello World";
Console.WriteLine (strMsg);
Append(strMsg);
Console.WriteLine (strMsg);
}
}
Here a string strParam is being passed to Append(). Since string is a reference type, I would expect strParam to refer to the same string object to which strMsg is a reference. Hence as per my expectation on calling Append() the string " - C#" should get appended to the same string object to which strMsg is referring and it should become "Hello World - C#". But to achieve this behavior we have to make strParam as "ref".
I am unable to find justification to this behavior of string.
Thanks in advance
Siddhartha
|
|
|
|
|
What I'm trying to explain is that the behavior is how reference types work, not just string. Try doing it with your own class and you'll see it is true. In the previous example I gave, that's why when t = new Test() executed, the value of t.x did not change in the caller.
If your argument is correct, then
<br />
string x = "Hello World";<br />
string y = x;<br />
y = "Bad Boy";<br />
the last statement should change x too, I hope you agree that's not logical or intuitive. I hope you are not confusing with C++ references, which, by the way, behave as you said.
Think of references as pointers in C/C++. Take a pointer, say, int *p.
<br />
int x = 20;<br />
int *p = &x;<br />
int *z = p;<br />
int y = 70;<br />
*z = 50;
z = y;
It's the same way references operate. If you call a function passing a reference type, the *reference* gets passed by value, so if you try to change what the variable refers to, it won't affect the caller. But still because they both point to the same object, if you attempt to modify the object referred to, the caller will see the change.
Hope this helps.
Regards
Senthil
|
|
|
|
|
Thanks,
I now have a better insight of C# strings
|
|
|
|
|
Hi everyone,
Know of any vertical progress bar for .NET or how to create one? Any help is good. Thanks.
It is not the length of time, but what you plan to do with the time given to you that matters.
|
|
|
|
|
Search the articles here on CodeProject. There are some that cover this subject.
www.troschuetz.de
|
|
|
|
|
i have 3 forms 1 is MDIContainer that is mdiparent and remaining 2 are chiles i.e. Child1,Child2
if i want to open childs in mdi i have to write in mdiparent
Child1 obj = new Child1();
obj.Show();
obj.MdiParent = this;
suppose is want to open Clild2 on click of button on child1
so i have to write code
Child2 obj = new Child2();
obj.Show();
but it open in new it doesn't have any parent
i want that it open in mdiparent as a child
so i try
Child2 obj = new Child2();
obj.Show();
midparent par = new mdiparent()
obj.MdiParent = par;
but it is not possible to create object and specify child2 to mdiparent is mdiparent
any another way
please post as early as possible
thank you
Hemant
|
|
|
|
|
If child1 was created as an MDI child, then it's MDIParent property can be used.
In your Child2 code, you should be able to do the following:
Child3 frm3 = new Child3();
frm3.MdiParent = this.MdiParent;
frm3.Show();
Cheers,
Simon
sig :: "Don't try to be like Jackie. There is only one Jackie.... Study computers instead.", Jackie Chan on career choices.
article :: animation mechanics in SVG blog:: brokenkeyboards "Most of us are programmers, but a few use VB", Christian Graus
|
|
|
|
|
What is the way to deactivate the keyboard while my richTextBox is being updated?
|
|
|
|
|
While there are a few ways ranging from overriding ProcessDialogKey and doing nothing while a state variable is set to P/Invoking SetWindowsHookEx and writing an extremely efficient and robust keyboard hook, the most simple way is to set RichTextBox.Enabled to false . The point of disabling a control is to disable input into that control.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
The ValueChanged event of the NumericUpDown control is not fired when the user changes its value, by typing in the text part of it;
Though it should be, as documented in MSDN.
(it is only fired when the user clicks the up and down arrows)
How can I make it to be fired?
Lizzy
|
|
|
|
|
It should be fired when the control loses focus. Until the control loses focus the value is not actually changed when editing it manually. If this wasn't the case, if you mean to type 90 and the event fired when you typed just 9, bad things could happen that the developers using the control may not expect.
If this is the behavior you want, try extending the control and overriding ProcessDialogKey or ProcessDialogChar and fire the event yourself, or use a timer to fire the event after a short amount of time (enough to give moderately slow typers time to enter 2 or more digits - whatever it is you're expecting).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
how to set an edit box control so that it only receive numbers. I was thinking about using the event key press.
<italic>Work hard and a bit of luck is the key to success. You don`t need to be genius, to be rich.
|
|
|
|
|
you are taling about windows base application or webbased application?
|
|
|
|
|
It`s windows base application.
<italic>Work hard and a bit of luck is the key to success. You don`t need to be genius, to be rich.
|
|
|
|