PC Review


Reply
Thread Tools Rate Thread

C# constructor problem

 
 
=?Utf-8?B?emlramU=?=
Guest
Posts: n/a
 
      27th Sep 2005
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?
 
Reply With Quote
 
 
 
 
Ollie Riches
Guest
Posts: n/a
 
      27th Sep 2005
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




"zikje" <(E-Mail Removed)> wrote in message
news:B8B1CBCE-928C-4FC2-9CD5-(E-Mail Removed)...
> 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?



 
Reply With Quote
 
Peter Kirk
Guest
Posts: n/a
 
      27th Sep 2005

"Ollie Riches" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> 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....


 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      27th Sep 2005
"zikje" <(E-Mail Removed)> a écrit dans le message de news:
B8B1CBCE-928C-4FC2-9CD5-(E-Mail 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      27th Sep 2005
Ollie Riches wrote:

>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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
 
Reply With Quote
 
Ollie Riches
Guest
Posts: n/a
 
      27th Sep 2005
I stand corrected

"Oliver Sturm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ollie Riches wrote:
>
>>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
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      27th Sep 2005

zikje wrote:
> 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

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      27th Sep 2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# constructor best practice: how much logic to place in a C#constructor Jon Microsoft C# .NET 2 11th Nov 2009 09:18 PM
Problem with Constructor shapper Microsoft ASP .NET 1 6th Dec 2006 02:56 PM
overloaded constructor, access zero-argument constructor mblatch Microsoft C# .NET 3 8th Apr 2005 09:53 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:29 AM.