Is this a known bug in C# VS2005?

J

Jonas

Why doesn´t the "v" variable get assigned?

private void SomeString()
{
string someString = "this.is.some.string";
int x = someString.IndexOf(".some",0, someString.Length);
int y = someString.IndexOf('.', 0);
int v = x - y;

}

If you do it like this it´s all works fine.

private int SomeString()
{
string someString = "this.is.some.string";
int x = someString.IndexOf(".some",0, someString.Length);
int y = someString.IndexOf('.', 0);
return x-y;

}
 
P

Peter

Jonas said:
Why doesn´t the "v" variable get assigned?

private void SomeString()
{
string someString = "this.is.some.string";
int x = someString.IndexOf(".some",0, someString.Length);
int y = someString.IndexOf('.', 0);
int v = x - y;

}

If you do it like this it´s all works fine.

private int SomeString()
{
string someString = "this.is.some.string";
int x = someString.IndexOf(".some",0, someString.Length);
int y = someString.IndexOf('.', 0);
return x-y;

}

I think you're getting confused about local variables. Your first
method does assign a value to the variable v. But this variable is only
local to the method SomeString, so you can't see this value outside the
method.

In your second method SomeString, you return the value of x-y to
whoever calls the method. So you could do:
int v = SomeString();
and see the value returned to your v variable.

Was that what you meant?

/Peter
 
I

Ignacio Machin ( .NET/ C# MVP )

Why doesn´t the "v" variable get assigned?

        private void SomeString()
        {
            string someString = "this.is.some.string";
            int x = someString.IndexOf(".some",0, someString.Length);
            int y = someString.IndexOf('.', 0);
            int v = x - y;

        }

If you do it like this it´s all works fine.

private int SomeString()
        {
            string someString = "this.is.some.string";
            int x = someString.IndexOf(".some",0, someString.Length);
            int y = someString.IndexOf('.', 0);
            return x-y;

        }

Hi,
I'm pretty sure that v (the one defined INSIDE your method is
assigned), do you have a v outside your method?
you have two options:
-change the line
int v = x - y;
to
v = x - y;

or simply use your second code sniplet
 

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