String function not working while debugging.

A

archana

Hi all,

I am facing some wierd problem in quick watch at a time of debugging
my application.

I have one variable of type string, say suppose i have code as below:

string FileName;
FileName = FileName.Substring(0,FileName.LastIndexOf("."));

At a time of debugging if i check value of filename it is displaying
value properly,

But when i try to add quick watch for expression
FileName.Substring(0,FileName.LastIndexOf(".")), it is giving me error
message that 'error: 'FileName.Substring' does not exist '

I don't know why this happening.

Or is it like that, i can't apply quick watch on string function while
debugging.

any help will be truely appreciated.

Thanks in advance.
Archana.
 
B

Biren Prajapati

I think it is like that only. Even you can't evaluate the functions of
int datatype.
 
A

archana

Hi,
thanks for ur reply.

So means there is no other alternative to exeute function at runtime
other that storing value of those variables into variable and checking
value of that variable.

If u know any other alternative please tell me.

Thanks.
 
B

Biren Prajapati

I could not figureout the exact way but, I created a simple class as
following

public class Test
{
public int i=10;

public int GetI()
{
return i;
}
}

I could execute GetI() function in debug mode. I think what we can
conclude from it is, the functions on the valuetypes can't be executed
in debug mode. I also tried to execute functions of hashtable in debug
mode and I got success.
 
J

Jon Skeet [C# MVP]

Biren Prajapati said:
I could not figureout the exact way but, I created a simple class as
following

public class Test
{
public int i=10;

public int GetI()
{
return i;
}
}

I could execute GetI() function in debug mode. I think what we can
conclude from it is, the functions on the valuetypes can't be executed
in debug mode. I also tried to execute functions of hashtable in debug
mode and I got success.

That wouldn't explain string not working for you, as string is a
reference type.
 
B

Biren Prajapati

I know string is a reference type but you would be knowing that string
is a special type which whenever assigned to another type of string
variable, the reference is not copied but another instance of that
string is created and copied into that string. Which is not the same in
other reference types.
 
J

Jon Skeet [C# MVP]

Biren said:
I know string is a reference type but you would be knowing that string
is a special type which whenever assigned to another type of string
variable, the reference is not copied but another instance of that
string is created and copied into that string. Which is not the same in
other reference types.

You're mistaken. No copy is made, and string acts like any other
reference type. It happens to be immutable, that's all. There are a few
ways in which String is special:

1) The CLR knows about it (for literals etc)
2) Literals are interned
3) The C# compiler handles concatenation using String.Concat
automatically.
4) There's a corner-case where calling the string constructor doesn't
actually create a new string (but only in a specific situation).

Other than that, it acts as any other reference type.

As String is immutable, it would be utter folly for a new instance to
be created every time the reference were assigned to another variable.
Here's a short but complete program which demonstrates that your claim
is incorrect:

using System;

class Test
{
static void Main()
{
// Avoiding using literal strings here just in
// case people think that's what's making it work...
string x = DateTime.Now.ToString();
string y = x;
Console.WriteLine (object.ReferenceEquals(x, y));
}
}

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top