Is this a known bug in C# VS2005?

  • Thread starter Thread starter Jonas
  • Start date Start date
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;

}
 
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
 
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
 
Back
Top