Create Data.SqlClient.SqlParameter with a SqlDataType, Size AND Value inline? - vb.net

I have a function which can take a number of SqlParameter objects:
Public Shared Function RunSQL(sqlInput As String, Optional params As Data.SqlClient.SqlParameter() = Nothing) As String
The aim is basically being able to call the function and create the parameters if needed, as needed.
Unfortunately, there do not appear to be any constructors available that will allow me to specify the Value as well as the SqlDbType without having to add lots of additional parameters (that I do not need) as well.
The desired outcome would be something like:
Dim myStr As String = RunSQL("spMyStoredProc", {New Data.SqlClient.SqlParameter("#FieldName", Data.SqlDbType.NVarChar, 50, "MyValue")})
Obviously as the constructor for this does not exist, my question is basically to ask whether or not there is any way around this, as in any alternative, etc whilst still allowing the convenience of declaring the parameters in the function call?
I'd rather not have to declare all of my parameters beforehand just to set the Value property.

First, your code compiles but the last parameter is not the value but the source-column-name.
But you could use the With statement to assign the Value:
Dim myStr As String = RunSQL("spMyStoredProc",
{
New Data.SqlClient.SqlParameter("#FieldName", Data.SqlDbType.NVarChar, 50) With {.Value = "MyValue"}
})
See: Object Initializers

Related

Cast generic structure to other structure

I'm having trouble wrapping my head around the problem I'm having. I want to apply some general rules to structures, and as the type of them differs, I want to use a generic function to do this. My problem is that to manipulate the structure via methods only available with parameters of a specified type, I can not find a way to do so without extensive casting. See, for example, what steps are needed to specify that a DateTime value should always be specified as UTC:
Public Shared Function Sanitize(Of T As Structure)(retValue As T?) As T?
' If value is DateTime it must be specified as UTC:
If GetType(T) = GetType(DateTime) AndAlso retVal.HasValue Then
' To specify the value as UTC, it must first be casted into DateTime, as it is not know to the compiler that the type in fact IS
' DateTime, even if we just checked.
Dim retValAsObj = CType(retVal, Object)
Dim retValAsObjAsDateTime = CType(retValAsObj, DateTime)
Dim retValWithSpecifiedKind = DateTime.SpecifyKind(retValAsObjAsDateTime, DateTimeKind.Utc)
retVal = CType(CType(retValWithSpecifiedKind, Object), T?)
End If
Return retVal
End Function
Am I missing something? Casting four times for such a simple task seems to complex for me to be the best / simplest solution.
You can use extension methods
With extension method you don't need to check type and cast it.
With extension method you will have own method for every type - simple to maintain
With extension method you will have "readable" syntax
<Extension>
Public Shared Function Sanitize(Date? nullable) AS Date?
{
If nullable.HasValue = False Then Return nullable
Return DateTime.SpecifyKind(nullable.Value, DateTimeKind.Utc)
}
<Extension>
Public Shared Function Sanitize(Integer? nullable) AS Integer?
{
If nullable.HasValue = False Then Return nullable
If nullable.Value < 0 Then Return 0
Return nullable.Value
}
Somewhere in the code
Dim sanitizedDate As Date? = receivedDate.Sanitize()
Dim sanitizedAmount As Integer? = receivedAmount.Sanitize()
Extension methods have some downsides - for example you not able to "mock" them for unit testing, which force you to test "Sanitize" method every time it used (if you are using Test-First approach).

What does variable-initialization line do in VB.Net?

Can someone please tell what the following line of VB.Net is initializing:
Dim x As SomeType() = New SomeType(0) {}
What holds x variable? Is it an array? How can it be translated to C# for example?
I guess SomeType is probably an anonymous type, but still have no clue...
The line:
Dim x As SomeType() = New SomeType(0) {}
declares an array of SomeType objects, which can hold one instance of SomeType.
When declaring an array of objects the value that is passed into the constructor is the max index of the array. So this declaration is basically declaring an array with a length of 1. The {} portion of the line is where you could define the values that should be stored in the array. If you were to change SomeType to integer you could instantiate and fill your array like:
Dim intArray as Integer() = New Integer(0) {7}
and that would give the first instance stored in the intArray variable a value of 7.
SomeType is not an anonymous type. SomeType would be a class that would have to be defined somewhere in your app.
In C# I think the sytax would look like:
SomeType[] x = new SomeType[0];
I'm not exactly sure how you would accomplish the {} portion of the VB.NET line in C#.
It's simply declaring and initializing an array of a given type. In C# I think it would be, quite similarly:
SomeType[] x = new SomeType[0] { };
Is it an array?
Yes. VB uses () for arrays instead of C#'s [].
I guess SomeType is probably an anonymous type
No, it's a defined static type like any other.

How do I pass parameter of inline new object?

I have a method that accepts a string as a parameter.
The string I need to pass is the property of an instantiated object.
I don't need the object to stick around once I get the value of that property.
I know I can do this like so:
Dim x As New myClass1
foo.thing1 = MyMethod(x.Name)
x = New MyClass2
foo.thing2 = MyMethod(x.Name)
'etc...
But I would prefer to do this inline if possible, since I have to do this several times in a row with different MyClass types.
EDIT:
Figured it out:
foo.thing = MyMethod(new MyClass().Name)
Try
foo.thing1 = MyMethod((New myClass1).Name)
foo.thing2 = MyMethod((New MyClass2).Name)
You need the braces around New myClass1, otherwise VB thinks you are trying to create an object of type myClass1.Name, which, of course, does not exist.
foo.thing = MyMethod(new MyClass().Name)

How to create a Dictionary with for value type that is known?

I want to create a Dictionary where the Key type is Integer and the Value type is the type of the class I am currently executing in.
I've tried the following:
Dim col as new Dictionary(Of Integer, Me.GetType())
but I am getting an error stating that `keyword does not name a type.
How do I create a dictionary based on the type of the executing class?
C# sample of creating dictionary of int to a type.
Methods used:
Type.MakeGenericType
Type.GetConstructor
ConstructorInfo.Invoke
The problem is mainly to express resulting type in somewhat type-safe manner. In Dictionary case one may resort to IDictionary, or continue to use reflection to manipulate objects.
It may also be possible to somehow express most manipulation with generic code which is invoked by more reflection with MakeGenericMethod
Sample:
var myType = typeof(Guid); // some type
// get type of future dictionary
Type generic = typeof(Dictionary<,>);
Type[] typeArgs = { typeof(int), myType };
var concrete = generic.MakeGenericType(typeArgs);
// get and call constructor
var constructor = concrete.GetConstructor(new Type[0]);
var dictionary = (IDictionary)constructor.Invoke(new object[0]);
// use non-generic version of interface to add items
dictionary.Add(5, new Guid());
Console.Write(dictionary[5]);
// trying to add item of wrong type will obviously fail
// dictionary.Add(6, "test");
Just use the class name Dim col as new Dictionary(Of Integer, MyClass)
On a side not using integer as your key can get confusing since a dictionary also uses integer for the index. If the keys are to be consecutive integers, you might be better served by a list.

What is the correct way of creating an associative array in VB.NET?

I'm a PHP guy, and in PHP I would do something like the following:
$variable = array('0001'=>'value1', '0010'=>'value2');
I'm pretty new to VB.NET, so how do I translate the above code to VB.NET?
I think I have to use a dictionary:
Dim variable As New Dictionary(Of String, String)
variable.Add("0001", "value1")
variable.Add("0010", "value2")
Is this the correct way of doing it or should I use something else for this?
It is a way, or if you prefer a one-liner way of initialization, you can do this:
Dim variable As New Dictionary(Of String, String) From { {"0001", "value1"}, {"0010", "value2"} }
As far as which is the better one, it's more a matter of coding standard, and/or personal preference.
Considering what kind of container to use, you should use only those from System.Collection.Generics in .NET unless you are forced otherwise. And Dictionary is the default associative container. You can see the alternatives (SortedDictionary for example), if it matches more your use case.
You can use Collection:
Dim var As New Collection
var.Add("vakue1", "0001")
var.Add("value2", "0010")
Loop through all with:
For Each v As String In var
Next
For other ways of treating items, look at the sample in Collection(Of T) Class (MSDN).