Convert Double value to Array in vb.net - vb.net

I have vb.net application which fetches data from the excel and perform some operation.
I am using excel object Range to get the values from excel.
Below is the line of code I am using,
xlsRangetrans = xlsWorkbook.Worksheets(SHEET_1).Range("Range1")
Dim transArray(,) As Object = xlsRangetrans.Value
The above code runs successfully if I have more than one values in the Range1.
But if I have only one value I am getting error.
Eg: xlsRangetras.value has "123.0,124.0,nothing,nothing" as System.Array which runs successfully
But if xlsRangetras.value has "123.0" as Double, it gives me error.
How to convert Double value to Array in my case.
Please help.

Dim helper As Object = xlsRangetrans.Value
Dim transArray(,) As Object = _
If(TypeOf helper Is Object(,), helper, {{DirectCast(helper, Object)}})
Update
Obviously the If operator didn't exist in older .NET versions. In this case that piece of code might help.
Dim helper As Object = xlsRangetrans.Value
Dim transArray(,) As Object
If TypeOf helper Is Object(,) Then
transArray = helper
Else
transArray = {{DirectCast(helper, Object)}}
End If

Related

Using the New List command with a Passed Parameter

I'm trying to Pass a Field Parameter from my form textbox to a Function to create a New List object from the Data Table parameter I'm passing.
In the following code, the first tmpReadTable shows with no syntax error, but when I try to use the Parm with the Datatable name I'm not sure what I'm missing syntax wise. I'm new to this, thanks in advance!
Updated code below:
Thank you for all the helpful replies...sorry I'm not more experienced, I'm coming from a Visual Foxpro background.
To summarize:
I want to pass in my IMPORT table parameters from my form.
The cImportTable is an empty SQL Table to use to import and validate each CSV file row.
I found this example in Murach's VB book but he leaves out how the LIST is being created from a PRODUCTS table in an earlier exercise. So I thought I could just substitute my passed cImportTable to do the same...that's where I'm stuck and maybe you all know of a better way.
Private Function ReadImportFile(ByVal cImportFile As String, ByVal cGroupID As String, ByVal cControlTable As String, ByVal cImportTable As String)
MessageBox.Show(cImportFile + " " + cGroupID + " " + cControlTable)
If Not File.Exists(cImportFile) Then
MessageBox.Show("File: " + cImportFile + " does not exist - cancelling process.")
Return False
End If
Dim curFileStream As New StreamReader(New FileStream(cImportFile, FileMode.Open, FileAccess.Read))
Dim curImportTable = "NewDataSet." + cImportTable
'Here I'm trying to create a LIST or DATASET using my Empty SQL Import Table and read in each row of the CSV file in the DO WHILE loop
'...I'm coming from Visual Foxpro background so am not sure what I'm missing or what is the standard procedure to do this simple task.
'This line gives me a syntax issue - and I'm not even sure what it's suppose to do, I'm taking it from Murach's VB book example,
'but he leaves out this vital piece of how to create this LIST from a Datatable - or if it's even the right method to use.
Dim tmpReadTable = New List(Of curImportTable)
Do While curFileStream.Peek <> -1
Dim row As String = curFileStream.ReadLine
Dim columns() As String = row.Split(",")
Dim ImportRecord As New curImportTable
ImportRecord.GroupId = columns(0)
ImportRecord.MemberId = columns(1)
Loop
'More Processing after Importing CSV file.....
curFileStream.Close()
'If lNoErrors
Return True
End Function
You are using a variable instead of TYPE on the code line #3 here
' This seems to be ok, no syntax error
Dim tmpReadTable = New List(Of NewDataSet.FO_ImportDataTable)
' The variable below implicitely will be of STRING type
Dim curImportTable = "NewDataSet." + cImportTable.ToString
' This line is not going to work
Dim tmpReadTable = New List(Of curImportTable)
' BUT THIS WILL
Dim x = New List(Of String)
Another issue is that Dim tmpReadTable happened twice in your code! can't re-declare variable. On top you declared it as NewDataSet.FO_ImportDataTable
Besides, I recommend declare all variables like Dim curImportTable as String, this way you can recognize types easier. Option Infer is good when you use anonymous types, LINQ, etc

Getentity method in vb.net + Autocad?

ACADAPP = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application")
ACADDOC = ACADAPP.Documents.ActiveDocument
second_POINT = ACADDOC.Utility.GetEntity(select_object, , "Select Object <Enter to Exit> : ")
ACADDOC.Utility.GetEntity returns an error as
type mismatch
in vb.net autocad,when I'm trying with vb6 it works fine.
What about that 2nd empty parameter - is that correct? According to the specification, it expects an object - a point.
object.GetEntity Object, PickedPoint [, Prompt]
Such as...
ThisDrawing.Utility.GetEntity returnObj, basePnt, "Prompt, i.e. Select an object"
By the way - is that really a VB.NET? Or Visual Basic for Application (VBA)? Notice, there are significant differences in syntax and capabilities... The AutoDesk general documentation (incl. online) would be for VBA, not VB.NET.
EDIT:
Dim returnObj As AcadObject
Dim basePnt As Variant
ThisDrawing.Utility.GetEntity returnObj, basePnt, "Select an object"
Note, that this example is for VBA, I've never worked with VB.NET and ACAD, I'm not even sure how it is supported.
Make sure you handle empty selection too...
Here's a simple function that will return a selected object.
The PromptEntityResult's ObjectId Property is the actual returned entity, which you will have to get to with a transaction.
Public Shared Function GetEntity() As PromptEntityResult
Dim retVal As PromptEntityResult = Nothing
Dim oDoc As Document = Core.Application.DocumentManager.MdiActiveDocument
Dim oEd As Editor = oDoc.Editor
Dim oPeo As New PromptEntityOptions(Environment.NewLine & "Please select an object")
With oPeo
.SetRejectMessage(Environment.NewLine & "Cannot select that object.")
.AllowNone = False
.AllowObjectOnLockedLayer = True
End With
retVal = oEd.GetEntity(oPeo)
Return retVal
End Function

Why won't this list of struct allow me to assign values to the field?

Public Structure testStruct
Dim blah as integer
Dim foo as string
Dim bar as double
End Structure
'in another file ....
Public Function blahFooBar() as Boolean
Dim tStrList as List (Of testStruct) = new List (Of testStruct)
For i as integer = 0 To 10
tStrList.Add(new testStruct)
tStrList.Item(i).blah = 1
tStrList.Item(i).foo = "Why won't I work?"
tStrList.Item(i).bar = 100.100
'last 3 lines give me error below
Next
return True
End Function
The error I get is: Expression is a value and therefore cannot be the target of an assignment.
Why?
I second the opinion to use a class rather than a struct.
The reason you are having difficulty is that your struct is a value type. When you access the instance of the value type in the list, you get a copy of the value. You are then attempting to change the value of the copy, which results in the error. If you had used a class, then your code would have worked as written.
try the following in your For loop:
Dim tmp As New testStruct()
tmp.blah = 1
tmp.foo = "Why won't I work?"
tmp.bar = 100.100
tStrList.Add(tmp)
Looking into this I think it has something to do with the way .NET copies the struct when you access it via the List(of t).
More information is available here.
Try creating the object first as
Dim X = New testStruct
and setting the properties on THAT as in
testStruct.blah = "fiddlesticks"
BEFORE adding it to the list.

Unable to solve error "reference to a non-shared member requires an object reference. vb.net "

I have the following code
Case "Formula_MDX"
Dim cubeid As String = Request("intCubeId")
Dim strDimCode As String = Request("strDimCode")
Dim strMdxFormula As String = Request("strMdxFormula")
Dim result As String
result = HostAnalytics.HostAnalyzer.HostAnalyzer.setSubstituteVarMDXType(cubeid, strDimCode, strMdxFormula)
Case Else
Response.Write("Invalid call")
End Select
that vb method returns data of type string.
I declared the result of the typed string. but it is showing on that vb method like
"reference to a non-shared member requires an object reference"
How to solve this? Did I make any other mistakes in this code?
Make an object of that type, and invoke the method on that
Dim ha As New HostAnalytics.HostAnalyzer.HostAnalyzer() 'Edit, need New
result = ha.setSubstituteVarMDXType(cubeid, strDimCode, strMdxFormula)

vb.net in InDesign Scripting - Grouping TextFrames

I want to group textframes in my InDesign CS3 vb.net script. It worked for InDesign 2.0 but it does not work with InDesign CS3. Here is my code:
Dim myDoc As InDesign.Document = Nothing
Dim myGroup As InDesign.Group = Nothing
Dim myObjectList(2)
myObjectList.SetValue(myOuterTextFrame, 0)
myObjectList.SetValue(myInnerTextFrame, 1)
myObjectList.SetValue(myContentTextFrame, 2)
myGroup = myDoc.Groups.Add(myObjectList)
Getting error "Unable to cast object of type 'System.Object[]' to type 'InDesign.Objects'."
I know you asked for this a long time ago so I'm mostly answering for future searches. I haven't found a fully managed way to do this using the .Net Framework and believe me, I've searched for it. I've tried a million different casts, subclassing, reflection, you name it. What ultimately worked in the end was JavaScript. Below is a method that takes an InDesign.Document object and two or more integers that represent InDesign item IDs. It then creates some JavaScript and has InDesign execute it. Finally it returns an InDesign.Group created from those objects.
Public Function GroupObjects(ByVal indesignDocument As InDesign.Document, ByVal ParamArray objectIds() As Integer) As InDesign.Group
'Sanity checks
If indesignDocument Is Nothing Then Throw New ArgumentNullException("indesignDocument")
If objectIds Is Nothing OrElse objectIds.Count < 2 Then Throw New ArgumentException("You must pass at least 2 object ids")
'We'll assign a unique label to the group that we create in JavaScript so that we can find it in managed code later
Dim GID = Guid.NewGuid().ToString()
'Create the JavaScript
Dim Buf As New StringBuilder()
Buf.AppendLine("var items = new Array();")
For Each ID In objectIds
Buf.AppendFormat("items.push(app.activeWindow.activePage.pageItems.itemByID({0}));", ID)
Buf.AppendLine()
Next
Buf.AppendLine("var g = app.activeWindow.activePage.groups.add(items);")
Buf.AppendFormat("g.label='{0}';", GID)
Dim IA = indesignDocument.Parent
IA.DoScript(Buf.ToString(), InDesign.idScriptLanguage.idJavascript)
'Loop through all document groups looking for the object with the label created above
For Each G As InDesign.Group In indesignDocument.Groups
If Not String.IsNullOrWhiteSpace(G.Label) AndAlso G.Label = GID Then Return G
Next
Return Nothing
End Function
To use it in your code you'd say:
Dim MyGroup = GroupObjects(myOuterTextFrame, myInnerTextFrame, myContentTextFrame)
This one worked for me:
Type type = Type.GetTypeFromProgID("InDesign.Application");
Host = (InDesign.Application)Activator.CreateInstance(type);
InDesign.Objects o = Host.CreateCollection();
I found my answer in the InDesign Scripting Samples - the Neon sample script gave grouping examples