Connecting Tech Pros Worldwide Forums | Help | Site Map

What is wrong in this code?

Adrian
Guest
 
Posts: n/a
#1: Nov 8 '06
What is wrong in the code below?
Adrian.

using System;

namespace ConsoleApplication2
{
class Trial
{
static void Main(string[] args)
{
MutStruct ms;
ms.partNo = "xxx";
MutTransform mt = new MutTransform(ms.partNo);// errors here "cannot
convert from string to... etc."
Console.WriteLine(mt.PartNumber);
}
}

internal struct MutStruct
{
internal string partNo;
}

internal struct MutTransform
{
internal string PartNumber;
internal MutTransform(MutStruct mstr)
{
this.PartNumber = mstr.partNo.PadLeft(10, 'y');
}
}
}



Jianwei Sun
Guest
 
Posts: n/a
#2: Nov 8 '06

re: What is wrong in this code?


Adrian wrote:
Quote:
What is wrong in the code below?
Adrian.
>
using System;
>
namespace ConsoleApplication2
{
class Trial
{
static void Main(string[] args)
{
MutStruct ms;
ms.partNo = "xxx";
MutTransform mt = new MutTransform(ms.partNo);// errors here "cannot
convert from string to... etc."
Console.WriteLine(mt.PartNumber);
}
}
>
internal struct MutStruct
{
internal string partNo;
}
>
internal struct MutTransform
{
internal string PartNumber;
internal MutTransform(MutStruct mstr)
{
this.PartNumber = mstr.partNo.PadLeft(10, 'y');
}
}
}
>
>
You are passing ms.partNo, which is a string to MutTransform constructor.

I think you may want to do something like this:

MutTransform mt = new MutTransform(ms) instead;
Michael Nemtsev
Guest
 
Posts: n/a
#3: Nov 8 '06

re: What is wrong in this code?


Hello Adrian,

AMutTransform mt = new MutTransform(ms.partNo);//

here you set the string as the param of your ctor

Ainternal MutTransform(MutStruct mstr)

and there the constructor that takes the struct, not the string


---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


Bruce Wood
Guest
 
Posts: n/a
#4: Nov 8 '06

re: What is wrong in this code?



Adrian wrote:
Quote:
What is wrong in the code below?
Adrian.
>
using System;
>
namespace ConsoleApplication2
{
class Trial
{
static void Main(string[] args)
{
MutStruct ms;
ms.partNo = "xxx";
MutTransform mt = new MutTransform(ms.partNo);// errors here "cannot
convert from string to... etc."
Console.WriteLine(mt.PartNumber);
}
}
>
internal struct MutStruct
{
internal string partNo;
}
>
internal struct MutTransform
{
internal string PartNumber;
internal MutTransform(MutStruct mstr)
{
this.PartNumber = mstr.partNo.PadLeft(10, 'y');
}
}
}
Besides the fact that you appear to be using "struct" in an odd way....

The error message you're receiving is correct. ms.PartNo is a string,
but the constructor for MutTransform accepts a MutStruct, not a string.
You should make one of two changes. Either:

MutTransform mt = new MutTransform(ms);

or, down in MutTransform:

internal struct MutTransform
{
internal string PartNumber;
internal MutTransform(string partNo)
{
this.PartNumber = partNo.PadLeft(10, 'y');
}
}

One or the other. By the way, what's the point of wrapping a string in
a struct? Or is this is a pared-down version of what's really going
on...?

Adrian
Guest
 
Posts: n/a
#5: Nov 8 '06

re: What is wrong in this code?


"Jianwei Sun" <jsunnewsgroup@gmail.comwrote in message
news:%23l$W2G3AHHA.3928@TK2MSFTNGP03.phx.gbl...
Quote:
>
You are passing ms.partNo, which is a string to MutTransform constructor.
>
I think you may want to do something like this:
>
MutTransform mt = new MutTransform(ms) instead;
Ohoh.

Thank you,
Adrian.


Adrian
Guest
 
Posts: n/a
#6: Nov 9 '06

re: What is wrong in this code?


"Bruce Wood" <brucewood@canada.comwrote in message
news:1163016844.757665.189920@m73g2000cwd.googlegr oups.com...
Quote:
By the way, what's the point of wrapping a string in
a struct? Or is this is a pared-down version of what's really going
on...?
>
Yes it is.
Adrian


Closed Thread


Similar C# / C Sharp bytes