0

I am working on a SSIS job which has a script task in VB.Net. Our organizational directive is to move towards C# in the long run. For a new SSIS job I am developing, I used an online VB.Net to C# converter and using it in a scrip task. The VB.Net statement that reads a variable has similar syntax in C#, but throwing an error message. Below is the VB.Net and the equivalent C# code.

VB.Net statement:

Dim strEnvironment As String = Dts.Variables.Item("g_Environment").Value.ToString()

C# statement:

String strEnvironment = Dts.Variables.Item("g_Environment").Value.ToString()

As you can see, the statement to store a value in a variable is same between VB.Net and C#. However, when I use this code in the script task of SSIS job, the C# code is flagging the below error message.

'Variables' does not contain a definition of 'Item' and no accessible extension method 'Item' accepting a first argument of type 'Variables' could be found (Are you missing a using directive or an assembly reference?). 

My import statements have the following:

Using System;
Using System.IO;
Using System.text;
Using Sytsem.Collections;
Using System.Data;
Using Microsoft.Visualbasic;
Using Microsoft.SqlServer.Dts.Runtime;

I am completely new to SSIS/VB.Net and C#. Can someone help?

4
  • Maybe it should be Dim strEnvironment as String in VB instead of String strEnvironment . Commented Aug 13, 2021 at 7:14
  • 5
    Dts.Variables["g_Environment"] Commented Aug 13, 2021 at 7:16
  • @ErrBon, sorry, my bad in that syntax error. It was Dim strEnvironment As String only. I changed the OP. Commented Aug 13, 2021 at 7:21
  • @Chetan, It kinda helped. I had to use Dts.Variables["g_Environment].ToString(); Thanks for your help. Commented Aug 13, 2021 at 7:24

2 Answers 2

2

Item is the indexer in VB. The direct equivalent of Item(x) in VB is [x] in C#.

I'm a little surprised that a converter would not be aware of this (or that it would be confused by e.g. calling a function with the empty parameter list omitted).

Also see this question for more information on indexers: Create Indexer in VB.NET which can be used from C#

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think the converter you used could determine that Item seems to be a default property, not a method (function, in vb terms), or it didn't understand how to translate it to C#

VB has ( ) for both methods and collections(arrays)

Dim array = "a,b".Split(",")      'method, args are surrounded in ()
Dim a = array(0)                  'indexer also is surrounded in ()

C# has ( ) for methods and [ ] for accessing indexable collections by some indexer:

var array = "a,b".Split(",");     'method, surrounded by ()
var a = array[0];                 'indexer, surrounded by []

For classes that represent a collection, there might be provided an indexing property in VB - property (of any name) marked with Default that is the property accessed when no other property name is specified. In C# the equivalent property is always called this and you don't get to choose the name, nor do you specify it when you index the item

Your VB:

Dim strEnvironment As String = Dts.Variables.Item("g_Environment").Value.ToString()

Could also have been:

Dim strEnvironment As String = Dts.Variables("g_Environment").Value.ToString()

But as it's indexing, you use [] with it in C#:

String strEnvironment = Dts.Variables["g_Environment"].Value.ToString();

By the way, modern Visual Studio helps you with props vs methods:

enter image description here

..methods are yellow, properties are white and local variables are light cyan.. (But there isn't anything that shows in intellisense that shouts "this thing is indexable")

If you write .Something(...) and see that it's white, then Something is a property, not a method

Also, class names are green, dim stuff is unused and 3 dots mean theres a suggestion for something that could perhaps be improved

1 Comment

FYI, the issue is Item not Items (not sure if you meant to refer to Variables, though the problem doesn't indicate if it's a function or a property) and Item is the automatic indexer for VB (interestingly enough, you'll even see it in reflection).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.