Equivalent In Vb.Net - vb.net

Im Trying to get the Visual Basic. Net Equivalent of this function in c#
public bool IsBomb { get { return number == -1; } }
I Tried the Get Function but im not sure how to implement it in vb.net

Public ReadOnly Property isBomb as Boolean
Get
return (number = -1)
End Get
End Property

Read this document on MSDN about implementing properties in VB.NET.
http://msdn.microsoft.com/en-us/library/bc3dtbky.aspx

Public ReadOnly Property IsBomb As Boolean
Get
Return CBool(number = 1)
End Get
End Property
For future reference, there is a great site for converting C# to vb.net and vice-versa.
http://www.developerfusion.com/tools/convert/vb-to-csharp/

Related

How to pass a list (returned in a VB.net function) in VBScript?

I have a system function in VB.net that return a list of Strings (I've debugged this function).
And right now I need to call that system function in VBScript. How can I get that list of strings?
VB.net system function
Public Function someList As List(Of String)
Return List
End Function
VBScript:
Dim results
Set results = System.someList
For Each result In results
wscript.echo result
Next
The problem is that VBScript doesn't understand Generic classes (or is it COM that is the problem?), so you'll have to convert your list to something non-generic such as ArrayList.
I had the same problem when trying to use my C# library from VBScript and I wrote a simple conversion function (in C#) to overcome this problem:
public ArrayList toArrayList(IEnumerable collection)
{
var arrayList = new ArrayList();
foreach (object element in collection)
{
arrayList.Add(element);
}
return arrayList;
}
Some more helper functions I needed to get things working can be found here: ScriptingInteropHelper

Using VB.net Collection class via COM returns error: "Class doesn't support automation"

I have an existing VB.net class library which has a public property with a type of VB's Collection class. I'm exposing the class library as a COM-object to be able to use it in Progress.
When I access the Collection-property with an integer index (e.g. comObj.OutputCol.Item(1)) it works fine, but when I try to use the string indexer (e.g. comObj.OutputCol.Item("FirstCol")) I get the following error (from a VBScript I use for testing):
Error message: Class doesn't support automation
Error code: 800A01AE
Is it possible to use the string indexer in any way via COM?
Sample code, COM-object i VB.net:
<ComClass(TestClass.ClassId, TestClass.InterfaceId, TestClass.EventsId)>
Public Class TestClass
Public Const ClassId As String = "063CA388-9926-44EC-B3A6-856D5299C210"
Public Const InterfaceId As String = "094ECC57-4E84-423A-B20E-BD109AEDBC20"
Public Const EventsId As String = "038B18BD-54B4-42D3-B868-71F4C52345B0"
Private _sOutputCol As Collection = Nothing
Private Property sOutputCol() As Collection
Get
If _sOutputCol Is Nothing Then
_sOutputCol = New Collection()
End If
Return _sOutputCol
End Get
Set(ByVal Value As Collection)
_sOutputCol = Value
End Set
End Property
Public ReadOnly Property OutputCol() As Collection
Get
Return sOutputCol
End Get
End Property
Public Sub New()
sOutputCol.Add("First object", "FirstCol")
sOutputCol.Add(2, "SecondCol")
End Sub
End Class
Sample test-code in VBScript:
Set comObj = WScript.CreateObject("VbComTest.TestClass")
wscript.echo comObj.OutputCol.Item(1) ' Works
wscript.echo comObj.OutputCol.Item(CStr("FirstCol")) ' Gives the error
I have registred the dll with: >regasm "...path...\VbComTest.dll" /codebase
OK, the problem was that the indexer is overloaded and you shouldn't use that in COM-visible interfaces: https://msdn.microsoft.com/en-us/library/ms182197.aspx
Extract from the page about what happens to overloaded methods:
When overloaded methods are exposed to COM clients, only the first
method overload retains its name. Subsequent overloads are uniquely
renamed by appending to the name an underscore character '_' and an
integer that corresponds to the order of declaration of the overload.
For example, consider the following methods.
void SomeMethod(int valueOne); void SomeMethod(int valueOne, int
valueTwo, int valueThree); void SomeMethod(int valueOne, int
valueTwo);
These methods are exposed to COM clients as the following.
void SomeMethod(int valueOne); void SomeMethod_2(int valueOne,
int valueTwo, int valueThree); void SomeMethod_3(int valueOne, int
valueTwo);
Visual Basic 6 COM clients cannot implement interface methods by using
an underscore in the name.
So to use the string indexer I have to write:
wscript.echo comObj.OutputCol.Item_3("FirstCol")
(Item_2 takes an Object as parameter and will also work, if the documentation is correct).

Windws Store App - Convert String into Color in VB.NET

There is no FromName method available in VB.NET for Windows Store Apps. How can I convert string to color? I want to set an object's fillcolor by passing string value of color.
I found this workaround for C#
public static Color FromName(string name)
{
var property = typeof(Colors).GetRuntimeProperty(name);
return (Color)property.GetValue(null);
}
I can't seem to translate it into equivalent VB.NET code.
I also tried to write Windows Runtime Component using the above code but it says GetRunTimeProperty is not even defined anywhere.
Public Function FromName(ColName As String) As SolidColorBrush
Dim [property] = System.Reflection.RuntimeReflectionExtensions.GetRuntimeProperty(GetType(Windows.UI.Colors), ColName)
Return New SolidColorBrush(DirectCast([property].GetValue(Nothing), Windows.UI.Color))
End Function
And done!
Does this not work?
Public Shared Function FromName(name As String) As Color
Dim [property] = GetType(Colors).GetRuntimeProperty(name)
Return DirectCast([property].GetValue(Nothing), Color)
End Function
GetRuntimeProperty is an extension method, not a member of System.Type. You need to Import System. Reflection to use it. See http://msdn.microsoft.com/en-us/library/bb384936.aspx for more on extension methods in VB

Integer.TryParse - a better way?

I find myself often needing to use Integer.TryParse to test if a value is an integer. However, when you use TryParse, you have to pass a reference variable to the function, so I find myself always needing to create a blank integer to pass in. Usually it looks something like:
Dim tempInt as Integer
If Integer.TryParse(myInt, tempInt) Then
I find this to be quite cumbersome considering that all I want is a simple True / False response. Is there a better way to approach this? Why isn't there an overloaded function where I can just pass the value I want to test and get a true / false response?
No need to declare the integer.
If Integer.TryParse(intToCheck, 0) Then
or
If Integer.TryParse(intToCheck, Nothing) Then
If you have .Net 3.5 ability you can create an extension method for strings.
Public Module MyExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function IsInteger(ByVal value As String) As Boolean
If String.IsNullOrEmpty(value) Then
Return False
Else
Return Integer.TryParse(value, Nothing)
End If
End Function
End Module
And then call like:
If value.IsInteger() Then
Sorry, getting carried away I know, but also you can add this to the MyExtensions class above in .Net 3.5 and not worry unless you need validations.
<System.Runtime.CompilerServices.Extension()> _
Public Function ToInteger(ByVal value As String) As Integer
If value.IsInteger() Then
Return Integer.Parse(value)
Else
Return 0
End If
End Function
Then simply use
value.ToInteger()
This will return 0 if it isn't a valid Integer.
Since you are using VB.net you can use the IsNumeric Function
If IsNumeric(myInt) Then
'Do Suff here
End If
public static class Util {
public static Int32? ParseInt32(this string text) {
Int32 result;
if(!Int32.TryParse(text, out result))
return null;
return result;
}
public static bool IsParseInt32(this string text) {
return text.ParseInt32() != null;
}
}
Try this code.
Module IntegerHelpers
Function IsInteger(ByVal p1 as String) as Boolean
Dim unused as Integer = 0
return Integer.TryParse(p1,unused)
End Function
End Module
The nice part is that since it's declared as a Module level function it can be used without a qualifier. Example Usage
return IsInteger(mInt)
Why not write an extension method to clean up your code? I haven't written VB.Net for a while, but here is an example in c#:
public static class MyIntExtensionClass
{
public static bool IsInteger(this string value)
{
if(string.IsNullOrEmpty(value))
return false;
int dummy;
return int.TryParse(value, dummy);
}
}
J Ambrose Little performed timing tests for IsNumeric checks back in 2003. You may wish to retry the mentioned tests with v2 of the CLR.
A variation would be:
Int32.TryParse(input_string, Globalization.NumberStyles.Integer)

How can I access a class variable via an array in VB.NET?

If I have the following class and declaration:
Public Class objLocation
Public SysLocationId As String
Public NameFull As String
Public LatRaw As String
Public LongRaw As String
Public Active As Integer
End Class
dim lLocation as new objLocation
I can access each variable thus lLocation.SysLocationId, etc. Is there an alternate way, so I can access each variable by index, so something like lLocation(0), lLocation(1), etc., which gives me the flexibility to compare to classes of the same type via a for next loop, or against other sources, like a datatable.
If your goal is comparison, usually what you'll do is implement the IComparable interface or overload the >, < operators (if an ordering is needed) or just the = operator (if equivalence is needed).
You just write one function in one location and invoke that function whenever you need to do your comparison. The same goes for comparing to objects stored in a database. Where you put these functions depends on your application architecture, but for the object-object comparison you can have it as part of the objLocation class itself.
There is no built-in langauge support for this. However you can simulate this by creating a default indexer property on the class
Public Class objLocation
...
Default Public ReadOnly Property Indexer(ByVal index As Integer)
Get
Select Case index
Case 0
Return SysLocationId
Case 1
Return NameFull
Case 2
Return LatRaw
Case 3
Return LongRaw
Case 4
Return Active
Case Else
Throw New ArgumentException
End Select
End Get
End Property
Then you can use it as follows
Dim x As objLocation = GetObjLocation
Dim latRaw = x(2)
No, you can not do this outright.
You have to use reflection to get the properties, but you have to be aware that there is no guarantee on the order of the properties returned (which is important if you want to index them numerically).
Because of that, you will have to keep the sort order consistent when working with the properties (and indexes).
Are you looking for a List:
Dim LocationList As List<objLocation>;
For Each loc As objLocation In LocationList
loc.whatever
Next
or to use the index:
For i = 0 To LocationList.Length - 1
LocationList(i).whatever
Next
sorry, if the VB syntax isn't right...I've been doing C# lately and no VB
You can do that as follows. It is C# and something is a bit different with using indexers in VB, but you should absolutly be able to get it working in VB.
public class ObjLocation
{
private String[] Properties = new String[5];
public const Int32 IndexSysLocationId = 0;
public const Int32 IndexNameFull = 1;
public const Int32 IndexLatRaw = 2;
public const Int32 IndexLongRaw = 3;
public const Int32 IndexActive = 4;
// Repeat this for all properties
public String SysLocationId
{
get { return this.Properties[ObjLocation.IndexSysLocationId]; }
set { this.Properties[ObjLocation.IndexSysLocationId] = value; }
}
public String this[Int32 index]
{
get { return this.Properties[index]; }
set { this.Properties[index] = value; }
}
}
Now you have the object with the properties as before, but stored in an array and you can also access them through an indexer.
This method I implemented in a public structure to return an array of string variables stored in a structure:
Public Shared Function returnArrayValues() As ArrayList
Dim arrayOutput As New ArrayList()
Dim objInstance As New LibertyPIMVaultDefaultCategories()
Dim t As Type = objInstance.GetType()
Dim arrayfinfo() As System.Reflection.FieldInfo = t.GetFields()
For Each finfo As System.Reflection.FieldInfo In arrayfinfo
Dim str As String = finfo.GetValue(objInstance)
arrayOutput.Add(str)
Next
Return arrayOutput
End Function
Put it inside the structure or a class. Maybe this sample code helps.