473,378 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# constructor problem

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?
Nov 17 '05 #1
7 1428
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" <zi***@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.com...
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?

Nov 17 '05 #2

"Ollie Riches" <ol**********@phoneanalyser.net> skrev i en meddelelse
news:Om**************@TK2MSFTNGP11.phx.gbl...
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....
Nov 17 '05 #3
"zikje" <zi***@discussions.microsoft.com> a écrit dans le message de news:
B8**********************************@microsoft.com...

| 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
Nov 17 '05 #4
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)
Nov 17 '05 #5
I stand corrected :)

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
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)

Nov 17 '05 #6

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

Nov 17 '05 #7
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. :)

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
6
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.