Problem assigning a value

  • Thread starter Thread starter Ricardo Sta. Rita
  • Start date Start date
R

Ricardo Sta. Rita

Hello people,

We have been dealing with for a long time now. It's a bit weird for a
newcomer like us so we decided to ask the geniuses here.

C# doesn't seem to be assigning values, consider the lines of codes below:

------------------------------------------------
using Sem = semantics.SemanticsProcessingClass;
------------------------------------------------

//We created an instance of the class
Sem MySem = new Sem();

//This is where the problem is
string semSuggWords = MySem.fileContent;
----------------------------------------------

The value of the fileContent is not being stored. We have tried different
approaches and still no values. The debugger is telling us that it is "out
of scope".

Even a simple value assignment like this doesn't seem to work: string
semSuggWords = "test";

One solution we which indeed fixed it and finally made it possible to assign
a value to the newly created variable is declare it as a class field. Weird,
it would mean we have to declare all vars as fields.

Anyone encountered this problem?

Thanks in advanced.

Best,
Ricardo Sta. Rita
 
Ricardo,

I find it hard to believe that something so simple doesn't work, and
yet, I can't think of a situation where it wouldn't. Can you post an
example of what you are trying to do? My only thought is that you are
declaring MySem on the class level, as well as semSuggWords. If this is the
case, then you can't do it, because you can't guarantee the order in which
MySem and semSuggWords are going to be initialized.

Hope this helps.
 
Ricardo Sta. Rita said:
We have been dealing with for a long time now. It's a bit weird for a
newcomer like us so we decided to ask the geniuses here.
C# doesn't seem to be assigning values, consider the lines of codes below:

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hello again,

Below is the short but complete program that I think will help you
understand the problem
=========================================================
using Sem = semantics.SemanticsProcessingClass;
namespace ElectronicWriter;
public class MainWriterProc : Page
{

public void MainProcess(object sender, System.EventArgs e)
{
Sem MySem = new Sem();
MySem.SetValue(myWords,myTopic,myType,myPath); //
We made sure that the object will

// start processing data and assign values

// to all the fields

string semSuggWords = MySem.fileContent;
// fileContent is a public string field

// in Sem class.
}

}

=========================================================

Wierd part here is that "semSuggWords" is not recieving any values. Any
clues?
Another thing, this problem occured only after we started using DllImport in
our program, which I know will not, in any way, affect the MainProcess.

I don't know if it's just us or it's a bug.

Thanks for the help....
Ricardo


Jon Skeet said:
below:

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hello jon,

I think what I posted is enough to let you know what the problem I'm having.

Consider this lines of codes:

string myStringTest = "";
myStringTest = "test";
myStringTest = "hello";

Although it's not a complete code, you know that I created a string named
"myStringTest" and tried assigning values to it 3 times. Problem is, the
values doesn't seem to get assigned to.

Thanks
 
Ricardo Sta. Rita said:
I think what I posted is enough to let you know what the problem I'm
having.

Don't you think I'm a better judge of what I need to understand your
problem than you are?
Consider this lines of codes:

string myStringTest = "";
myStringTest = "test";
myStringTest = "hello";

Although it's not a complete code, you know that I created a string
named "myStringTest" and tried assigning values to it 3 times.
Problem is, the values doesn't seem to get assigned to.

They do in that case. You've clearly got something wrong in the code
you're not posting. Until you post it, we can't tell what it is.

So, for the third time: please post complete code.
 
Thanks anyway...


Jon Skeet said:
Don't you think I'm a better judge of what I need to understand your
problem than you are?


They do in that case. You've clearly got something wrong in the code
you're not posting. Until you post it, we can't tell what it is.

So, for the third time: please post complete code.
 
Ricardo Sta. Rita said:
Hello again,

Below is the short but complete program that I think will help you
understand the problem
=========================================================
using Sem = semantics.SemanticsProcessingClass;
namespace ElectronicWriter;
public class MainWriterProc : Page
{

public void MainProcess(object sender, System.EventArgs e)
{
Sem MySem = new Sem();
MySem.SetValue(myWords,myTopic,myType,myPath); //
We made sure that the object will

// start processing data and assign values

// to all the fields

string semSuggWords = MySem.fileContent;
// fileContent is a public string field

// in Sem class.
}

}

I've seen this before. First, it's clear that you can *never* know
whether sumSuggWords ever gets assigned a value, because you never
*do* anything with it, and it's not returned from the method. So, I'm
guessing that you're looking at the string in the debugger. Well, the
compiler is smart; it realizes that the string is never used, and does
not include it in the compiled IL. It's optimized right out. That's
why the debugger tells you that it's out of scope: it never existed,
so it was never in scope.

Look at the compiled method in a disassembler or decompiler like
Reflector to see what I mean. Then do something with the string after
it's assigned, like Debug.WriteLine(sumSuggWords), to see the string
pop back into the IL.

(It's impossible to know for sure if this is what you're running into
without, as Jon's asking for, a complete program. The reason he asks
for that, btw, is so that both you and he can be sure you're running
exactly the same code when debugging the problem, and that the problem
does not lie elsewhere. Also, it helps the debugging process to
isolate the problem from the rest of your code.)
 

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

Back
Top