Why does it show an error while declaring a variable in BASIC? - var

I am using the BASIC programming language and while declaring variable;
dim basic as string
it shows the error that unexpected token called DIM.
wonder why?

Related

Why does the assignment of Font objects fail?

Suppose I have the following VBA statements in Excel 2019 (Windows):
Dim myFont As Font
Dim myRange As Range
Set myFont = Range("A1").Font
Set myRange = Range("B1:B5")
Why does this statement fail with "Run time error 13: type mismatch":
Set myRange.Cells(1,2).Font = myFont
Both sides are objects, hence the "Set". This statement appears to be conceptually the same the first two "Set" statements which work.
On the other hand, this assignment to multiple cells
Set myRange.Font = myFont
fails with a different error: "Run time error 438: object doesn't support this method or property".
I have similar code in a Module I'm writing and expected the Font object assignments to work. My thought was that if I can assigned a Range object to another, why not Font?
To make the assignments work, I had to create separate assignments for each Font property.
To make the assignments work, I had to create separate assignments for each Font property.
That's how you set fonts on a Range, because the Range.Font has no setter, so you can't Set it directly.
The weird errors are because VBA is trying very hard to make the assignment work as written, through a mechanism that attempts to invoke the object's default member at run-time: since Range.Font is get-only, it's being interpreted as Set RNG.Font.[_Default] = SomeFont, but the default member of Font is most likely its Name (a String), and the assignment is thus a type mismatch error.
Side note, Rubberduck (free & open-source VBE add-in I maintain) inspections would have warned about this.

How to write an Xpath expression containing an apostrophe in VBA Selenium

My question relates to coding selenium specifically in VBA.
I have a function that finds webelements based on text passed to it (in string variable 'toFind'). The relevent Xpath identification method I use is (where driver. is the selenium chromedriver):
mySearch = "//*[contains(text(),'" & toFind & "')]"
Set ret = driver.FindElementsByXPath(mySearch)
This works unless the toFind variable contains an apostrophe. For example if "Consultant's Forename" is passed then my expression evaluates to:
Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant's Forename')]"), which causes an invalid selector run-time error.
I have researched elsewhere on the site and see a number of answers describing escaping from the single quotes using the backslash character. Based on this I have tried to use Set ret = driver.FindElementsByXPath("//*[contains(text(),\"Consultant's Forename\")]") instead. However, this will not compile in microsoft visual basic for applications as it reports a syntax error (code line is red). I have not tried using the driver.findElements(By.xpath method as opposed to driver.FindElementsByXPath as I assumed this would not make a difference to the handling of the XPath expression. I have tried the other suggestions of using the 'concat' function but this also seems not to be valid in VBA selenium.
I don't know if these methods are specficaly for platforms other than VBA or I am just getting my syntax wrong?
The only way I can work it at present is to ignore the existence of the apostrophe:
Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant') and contains(text(),'s Forename')]")
Whilst this works it is an incomplete solution and any help on the correct syntax to deal with the xpath location in VBA for text containing an apostrophe would be much appreciated.

Variant but not an Object

If I know a bit of data is going to be an Object, but I don't know what kind, I can pass it to a function or routine like this:
Sub mySubExpectingAnObject(myVal As Object)
Which is more explicit than
Sub mySubExpectingAnObject(myVal As Variant)
And should be preferred even if both work
If instead I know the bit of data is not an object, but could be anything else (Long, Double, String etc.), is there any way of Diming the argument as not an object. E.g
Sub mySubExpectingNotAnObject(myVal As NotObject)
Since if I use Variant here, there will be no automatic anti-object check. Does such a type exist; one that can encapsulate any non-object datatype exclusively? Is there a workaround other than just
If isObject(myVal) Then Err.Raise 5
or similar?
I don't see what's wrong with using a Variant and throwing an invalid procedure call or argument run-time error 5, which pretty much literally says "this procedure was invoked with an invalid argument", given IsObject(theParameter) returning True.
Variant exists specifically to lift the compile-time type constraints so that VBA can deal with foreign types such as IUnknown and whatnot: there's no way you can use a Variant and enforce any kind of compile-time check against it - by definition a Variant's type is only resolved at run-time.
The idiomatic way to solve this, is to use good, meaningful names for both your procedure and their parameters. Pretty hard to recommend anything with the examples you've got though.
There is no "variant but not an object" type, and you can't use a Variant and expect any kind of type validation at compile-time.

Compile error calling a Sub Procedure

New to VBA but years of experience with assembler, C and C#. I have created a Private Sub called CPScenarioData(wsname as String, rownum As Integer). When I call the procedure using the statement:
CPScenarioData(wsname, l)
I get an error 'Compile error: Expected =', however when I preceded the statement with Call no error occurs, why is this. I have other Private subs that I call without using Call that work fine. I am sure there is a simple answer or mistake I have made and will feel very sheepish when I see the answer but that's life. I am using Excel 2013 VBA. Thank you for your help.
It's a quirk (feature according to taste) of VB. To call a sub, you don't include brackets. So simply type:
CPScenarioData wsname, 1
Normally brackets are used to denote a Function, which returns a value. In VB you must provide a variable to receive the returned value. (Hence the compile error for missing =; it is expecting a = CPScenario(wsname, 1)).
Adding the word Call enables you to keep the brackets for Subs (equivalent of c# void).

String to XNamespace Implicit Conversion inside a BizTalk Orchestration

I am a bit confused by what I see and hence headed over to SO.
I am developing a BizTalk (2010) Orchestration and I am wanting to parse an incoming XML message. I just need to retrieve then number of times a particular node is repeating. I could have used XPath. But, I chose to use LinqToXml.
I have created a variable of type System.Xml.Linq.XNamespace and inside an expression shape, I am assignning it a string value.. say http://mycompany/v1.0. This is a perfectly valid C# statment, as there is an implicit conversion from String to XNamespace (MSDN link).
But the Orchestration will not compile at all. I get this error cannot implicitly convert type System.String to System.Xml.Linq.XNamespace.
And if I dont use the XNamespace variable and directly run LinqToXml on the incoming message like this
MessageCount = MyXElement.Elements("{http://mycompany/v1.0}ListOfNotifications").Elements("{http://mycompany/v1.0}Notification").Count();
I get a cannot convert from String to XName error. Even this is confusing.
I am using BizTalk 2010 and C# 4.0. Can someone explain if I am missing something? I have tried all these code snippets using LinqPad and I get the expected response. So, there are no typos or missing references.
I opted to use the XPath option to retrieve the values that I needed, instead of using LinqToXml. The code that I ended up writing looks like below:
xpath(myOrchVariable, "string(/*[local-name()='InputRootNode' and namespace-uri()='http://my/name/space'])")

Resources