0

I have this construction in my BIML file:

myColumns = myFile.ReadLine().Replace("\"","").Split('|');

I would like to replace this with:

myColumns = myFile.ReadLine().Replace("\"","").Split('<#=delimiter#>');

but apparantly that does not work. Somehow syntaxhighlighting tells me that doesn't work.....

ALSO:

string[] myFiles = Directory.GetFiles(path, "*.csv");

string[] myFiles = Directory.GetFiles(path, "*.<#=filetype#>");

When simply using

 myColumns = myFile.ReadLine().Replace("\"","").Split('delimiter');

it tells me Cannot implicitly convert type 'string' to 'char'

delimiter is declared as string delimiter ="|"

when changing that to char delimiter ="|" I get exactly the same error but then at the line where I am declaring delimiter.....

Henrov
  • 1,610
  • 1
  • 24
  • 52
  • 1
    `char delimiter ="|";` fails because a character uses a single tick mark thus `char delimiter ='|';` – billinkc Apr 08 '21 at 14:18
  • 1
    @billinkc Just a nitpick, but it uses single quotes `'` rather than either back tick ` or 'forward' tick `´` characters – iamdave Apr 08 '21 at 14:55
  • 1
    @iamdave Yeah, I find when I tell people single quote, they can interpret it as one double quote `"` so I've taken to calling a single quote a tick mark and back tick as back tick. I was unaware of a forward tick, aka [acute accent](https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows) – billinkc Apr 08 '21 at 15:17

1 Answers1

2

If I am understanding you correctly, this part of your file:

myColumns = myFile.ReadLine().Replace("\"","").Split('|');

will already be contained within a code block or seperate code file, yes? If so you do not need to parameterise the delmiter with another code block, but just reference the variable, assuming you have already declared it somewhere:

<#
...
char delimiter = '|';
...
var myColumns = myFile.ReadLine().Replace("\"","").Split(delimiter);
...
#>

In response to your comment, using this code would probably look something like this in your Biml file?

...
 <Tables>
  <Table Name="TableName" etc>
    <Columns>
    <# // This is the start of a Code Block

     // We can define some C# data structures
     char delimiter = '|';
     var myColumns = myFile.ReadLine().Replace("\"","").Split(delimiter);

     // Then start a loop which will generate some Biml nodes
     foreach(string col in myColumns)
     {
      // Note that the foreach loop is left open despite closing the code block on the next line.
      // This means the following Biml is looped over until the foreach ends.
    #>
     <Column Name="<#=col#>" etc />
    <# // We can then start another code block which carries on from the last one
     } // This closes the foreach loop defined earlier
    #>
    </Columns>
  </Table>
 </Tables>
...

You can see in this example how the C# code is contained within <# and #> tags with raw Biml everywhere else. The use of <#= and #> tags are a syntax shortcut for referencing previously defined C# variables. These obviously are only necessary when you are within a Biml part of the file and not already within a code block.

iamdave
  • 12,023
  • 3
  • 24
  • 53
  • I guess that's how it would work with c# but these are BIML files – Henrov Apr 08 '21 at 12:19
  • 2
    @Henrov Yes, but BIML is an XML like syntax that can contain C# code *within code blocks*, as denoted by `<# ... #>` tags. You can't just have C# anywhere within the file, it *must* be within a code block. If you can post more of your Biml file to your question it should be pretty obvious where your Biml and C# sections start and end. – iamdave Apr 08 '21 at 12:48
  • 1
    @Henrov I have added a bit more explanation on how C# works within Biml files which should help you understand how this works. – iamdave Apr 08 '21 at 13:03
  • I will try this. – Henrov Apr 08 '21 at 13:14
  • ery busy that is why slow response. Appreciate yo thinking with me – Henrov Apr 08 '21 at 13:19
  • Error: Argument 1: cannot convert from 'string' to 'char' <# StreamReader myFile = new StreamReader(filePath); myColumns = myFile.ReadLine().Replace("\"","").Split(delimiter); myFile.Close(); foreach(string myColumn in myColumns) { #> I can use the delimiter parameter elsewhere – Henrov Apr 08 '21 at 13:52
  • The variable `delimiter` is going to be available based on C# scoping rules. If you're in a Biml text nugget `<#= #>` or control nugget `<# #>` you'll be able to reference the variable --- just don't try to use a text nugget within a control and you'll be fine – billinkc Apr 08 '21 at 14:23
  • 1
    @Henrov The issue is right here in your error message: *Error: Argument 1: cannot convert from 'string' to 'char'* - you are trying to use a `string` data type as your delimiter when the `Split` function requires a `char` data type. In C# you can see this distinction with `char` literals using single quotes `'` and strings using double quotes `"`, as per your example code the `Replace` takes `string` data types and the `Split` a `char`: `.Replace("\"","").Split('|');` – iamdave Apr 08 '21 at 14:43