-2

I am trying to split a string and assign the different values. The string it returns to me is: 0077|PCK|PRD|05025066840471|4|Can Opener|1|10|B|20.00|0|100|0|0.00|0|0|1|0|0

So I want to split the string on "|" and assign each of them to another variable. That is what I tried to do:

         public static void LoadPRD(string sData)
    {
        string[] s = null;
        prdType PRD = new prdType();
        s = sData.Split("|");


        PRD.bCode = s.Left(s[0], 14);
        PRD.PCode = s.Left(s[1], 12);
        PRD.Desc = s.Left(s[2], 40);
        PRD.Pack = s.Val(s[3]);
        PRD.Unit = s.Left(s[4], 12);
        PRD.VATCode = s.Left(s[5], 1);
        PRD.VATRate = Conversion.Val(s[6]);
        PRD.Cost = Conversion.Val(s[7]);
        PRD.Sell = Conversion.Val(s[8]);
        PRD.Stock = Conversion.Val(s[9]);
        PRD.AWS = Conversion.Val(s[10]);
        PRD.OnOrder = Conversion.Val(s[11]);
        PRD.OrderQty = Conversion.Val(s[12]);
        PRD.LabelQty = Conversion.Val(s[13]);
        PRD.Restriction = s.Left(s[14], 1);
        PRD.MinStock = s.Val(s[15]);
        PRD.PromoCode = s.Left(s[16], 3);
        PRD.MnM = s.Left(s[17], 3);


    }

The error message says that the Strings does not exist in the context, but it is not too of a helpful information, I do understand what it means but I am very confused on how to approach the solution.

Just so you know, I did create the variable before hand, I've posted them below:

public struct prdType
    {
        public string bCode;
        public string PCode;
        public string Desc;
        public Int16 Pack;
        public string Unit;
        public string VATCode;
        public float VATRate;
        // Stored in pence
        public long Cost;
        public long Sell;
        public long Stock;
        public float AWS;
        public long OnOrder;
        public long OrderQty;
        public long LabelQty;
        public string Restriction;
        public long MinStock;
        public string PromoCode;

    }

Your help will be much appreciated. Thanks.

EDIT:

On s = sData.Split("|"); it says: "The best overloaded method match for string.Split(params char[]) has some invalid arguments. It also says that arguments cannot be converted to char. Any ideas?

davidvnog
  • 654
  • 2
  • 11
  • 33
  • Well why did you think that `Strings` would work? Which type did you expect it to refer to? – Jon Skeet May 19 '14 at 16:15
  • You need to use `'`, not `"` - `s = sData.Split('|');`. But that doesn't fix the rest of the problems in your code, only that line. – Tim May 19 '14 at 16:24

3 Answers3

2

These methods come from Microsoft.VisualBasic namespace.It should be only used if you know what you're doing (see Tim's comment on this answer).

I wouldn't advise you to use these methods.

They are equivalent methods in c# (or they're rather easy to implement).

Like String.Split, for example (so you could do var s = sData.Split('|'); )

A way to do something equivalent to String.Left

Wouldn't advise to do this, but anyway :

If you want absolutely use them, you should :

  • Add a reference to Microsoft.VisualBasic assembly (right click on project's references, you should find it in Framework libs)

  • Add the right using at the top of your code : using Microfost.VisualBasic;

Community
  • 1
  • 1
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Although it'd be far, far better if OP didn't do this since it's only there for backwards support of legacy VB code. – Tim May 19 '14 at 16:28
2

Rather than use legacy VB methods for this, I would suggest using C# methods all the way.

string[] s =  sData.Split('|');

The use of Strings.Left is not readily apparent. Since you've already split the line, you'll have each element of the split in its entirety. If you want to take only the first n characters, you can do that, but there is no built-in equivalent for Strings.Left in C#.

For those elements that are a different type, you can use Convert.ToX:

PRD.Pack = Convert.ToInt16(s[3]));

PRD.VATRate = Convert.ToSingle(s[6]));

PRD.Cost = Convert.ToInt64(s[7]);

And so on. Note that float uses Convert.ToSingle, not Convert.ToFloat.

ADDED

Based on @Raphael's comment, Convert.ToX is not a direct replacement for Conversion.Val(), but as long as the string is strictly numeric you will be ok and will get the correct type.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • Well, just a small thing : `Conversion.Val()` is not equivalent to `Convert.Toxxx()`. It's doing this (weird) thing : `Returns the numbers contained in a string as a numeric value of appropriate type.` See http://msdn.microsoft.com/en-us/library/9da280t0(v=vs.110).aspx – Raphaël Althaus May 19 '14 at 16:41
  • On further reading of the link, it returns a `double` and will parse strings with alpha characters, so it's not the best fit but it is a more proper way to handle the split values, IMO. – Tim May 19 '14 at 16:44
0

You need to do s = sData.Split('|');

Mohammad
  • 1
  • 1
  • That's only part of the problem. *Every* line in OP's code that has `Strings` in it is going to throw the same error. – Tim May 19 '14 at 16:18
  • There is not "Strings" in C#. The best I can try to explain is this. Basically, sData.split will split the string with the character you'll give it and it stores it in an array. So your "s" has to be an array, which you have defined it as array. Then, I'm not sure what "Strings.left..." does, but you can just get those values using the element number of the array s: PRD.bCode = s[0]; PRD.PCode = s[1]; and so on. Hope this helps. – Mohammad May 19 '14 at 16:24
  • Not my question, but `Strings.Left` is pretty obvious - it takes the *n* characters from the left of the string. The point is that OP's code is broke with the whole `Strings` thing - based on Raphael's answer, it looks like they're trying to mix and match C# and VB (not even VB.NET). – Tim May 19 '14 at 16:26
  • Yea, I think Strings.Left is VB and doesn't exist in C#. But even if it did, why would you need to use Strings.Left? I mean each section of the string is stored as one element of the array, right? So why not just read it with the element number? Those Strings.Left have to go. – Mohammad May 19 '14 at 16:31