What is wrong in this code?

A

Adrian

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');
}
}
}
 
J

Jianwei Sun

Adrian said:
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;
 
M

Michael Nemtsev

Hello Adrian,

A> MutTransform mt = new MutTransform(ms.partNo);//

here you set the string as the param of your ctor

A> internal 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
 
B

Bruce Wood

Adrian said:
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...?
 
A

Adrian

Jianwei Sun said:
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.
 
A

Adrian

Bruce Wood said:
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
 

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

Top