C# constructor problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I've got a really weird problem.

public Map(float zoomFactor, int panStep, float maxWidth, float minWidth,
Document svgDocument, string svgId)
{
this.zoomFactor = zoomFactor;
this.panStep = panStep;
this.maxWidth = maxWidth;
this.minWidth = minWidth;
this.svgDocument = svgDocument;
this.svgId = svgId;
}

and

this.m_map = new
Map(0.6f,15,1477f,50f,m_esvgControl.GetDocument(),"svgmap");
None of the values I fill in, are taken over by the instance of Map. I don't
get this. This is like the same for every programming language, and as far as
I can tell from books and examples, also counts for C#. What am I doing wrong?
 
This is because the parameter list for the constrcutor method have EXACTLY
the same names as the Map class variable names. In other words you are not
doing an assignment of the constructor parameter to the class variable but
the assigning the class variable to the class variable :)

Somthing like this should 'work'
public Map(float zf, int ps, float maxw, float minw, Document svgdoc, string
si)
{
this.zoomFactor = zf;
this.panStep = ps;
this.maxWidth = maxw;
this.minWidth = minw;
this.svgDocument = svgdoc;
this.svgId = si;
}


HTH

Ollie Riches
 
Ollie Riches said:
This is because the parameter list for the constrcutor method have EXACTLY
the same names as the Map class variable names. In other words you are not
doing an assignment of the constructor parameter to the class variable but
the assigning the class variable to the class variable :)

I don't think that is the problem. I do it almost all the time:

public MyClass(int a, string b) {
this.a = a;
this.b = b;
}

should work fine (note the use of "this").

Can't say I can see what the OP's problem is though....
 
"zikje" <[email protected]> a écrit dans le message de (e-mail address removed)...

| public Map(float zoomFactor, int panStep, float maxWidth, float minWidth,
| Document svgDocument, string svgId)
| {
| this.zoomFactor = zoomFactor;
| this.panStep = panStep;
| this.maxWidth = maxWidth;
| this.minWidth = minWidth;
| this.svgDocument = svgDocument;
| this.svgId = svgId;
| }
|
| and
|
| this.m_map = new
| Map(0.6f,15,1477f,50f,m_esvgControl.GetDocument(),"svgmap");
| None of the values I fill in, are taken over by the instance of Map. I
don't
| get this. This is like the same for every programming language, and as far
as
| I can tell from books and examples, also counts for C#. What am I doing
wrong?

Well, I just copied your code into a new module, added private fields and
public properties to read the values and it compiled and ran as expected;
both in debug mode and at runtime.

Could you show more code of how you are using the class ?

Joanna
 
Ollie said:
This is because the parameter list for the constrcutor method have EXACTLY
the same names as the Map class variable names. In other words you are not
doing an assignment of the constructor parameter to the class variable but
the assigning the class variable to the class variable :)

Somthing like this should 'work'
public Map(float zf, int ps, float maxw, float minw, Document svgdoc,
string si)
{
this.zoomFactor = zf;
this.panStep = ps;
this.maxWidth = maxw;
this.minWidth = minw;
this.svgDocument = svgdoc;
this.svgId = si;
}

This is not true. I use the same syntax (as the OP) all the time -
prefixing the field instances of the names with "this." should make it
work correctly.

I don't see anything wrong with the code as shown. The problem has to be
somewhere else, like in the definition of the fields. Could we see more of
the code of that Map class?


Oliver Sturm
 
I stand corrected :)

Oliver Sturm said:
This is not true. I use the same syntax (as the OP) all the time -
prefixing the field instances of the names with "this." should make it
work correctly.

I don't see anything wrong with the code as shown. The problem has to be
somewhere else, like in the definition of the fields. Could we see more of
the code of that Map class?


Oliver Sturm
 
zikje said:
Hello,

I've got a really weird problem.

public Map(float zoomFactor, int panStep, float maxWidth, float minWidth,
Document svgDocument, string svgId)
{
this.zoomFactor = zoomFactor;
this.panStep = panStep;
this.maxWidth = maxWidth;
this.minWidth = minWidth;
this.svgDocument = svgDocument;
this.svgId = svgId;
}

and

this.m_map = new
Map(0.6f,15,1477f,50f,m_esvgControl.GetDocument(),"svgmap");

This looks fine.
None of the values I fill in, are taken over by the instance of Map. I don't
get this. This is like the same for every programming language, and as far as
I can tell from books and examples, also counts for C#. What am I doing wrong?

I'm guessing that you are reassigning the m_map variable after you do
this, and that's resetting all of the values. Can you show the rest of
the code? There's nothing wrong with your constructor or code as shown.

Matt
 
One very important thing that you don't show is whether Map is a class
or a struct. If it's a struct, you may be doing something after the
original instantiation that is causing your problem.

Could you post one of Jon Skeet's famous "short but complete programs"
to demonstrate what you're seeing? See Jon's page here:

http://www.yoda.arachsys.com/csharp/complete.html

for instructions. :)
 
Back
Top