Re: Late Binding Question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Lloyd,

Thanks for the clarification. I wish I understood more about the internal workings of compilers as I would not find this stuff so confusing.
One day, I'll make some time and try and get my head around them. What you say makes absolute sense and that is really why I am modifying this code from its original version. I just hadn't equated that particular error as being caused by the same things I was looking to correct in the code by turning on option explicit.

I can see why C++ programmers think VBers are less capable than them as we have been shielded from this stuff in the past, and still are to an extent when we don't turn on option explicit. At the end of the day, I am going through some pain understanding why this stuff matters and hopefully as a result am becoming a better programmer and producing faster less error prone code as a result.

Long way to go though I suspect!

Cheers for your help and advice.

Siv

"Lloyd Sheen" <a@xxx> wrote in message news:eDu9dPSuIHA.5832@xxxxxxxxxxxxxxxxxxxxxxx

"Siv" <g@xxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:B394F9A1-81C6-4055-97B0-32AC3623BFFE@xxxxxxxxxxxxxxxx
Armin,

This seems to pacify the compiler:
DirectCast(XLApp.Cells(r, c), XL.Range).Formula = T7Lines(n).TextMonthNumber

I will see if the program runs as expected after I have corrected all sections that need amending.

I tried

Siv

"Siv" <g@xxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:C71A9709-EEE8-4F1D-BE6B-2FBE95C41774@xxxxxxxxxxxxxxxx
Armin,
I changed the line:

rng = XLapp.Cells(SheetRow, SheetCol)

to

rng = DirectCast(XLApp.Cells(SheetRow, SheetCol), XL.Range)

Which the parser is happy with; whether it works when I run it is another question!

I will now try and figure how I apply that to the

XLApp.Cells(r + 3, c).formula = Format(T7Lines(n).RiskPointsTotal, "0.0")

Lines??

I have temporarily changed the late binding error as a warning and when I do that the green wavy line appears under the left side of the argument. I still don't understand why the compiler thinks this is late bound?? As far as I can see all the variables are determined so that r+3 and c are known values? I never did get what all the fuss about late binding is?

Thanks,

Siv


"Armin Zingler" <az.nospam@xxxxxxxxxx> wrote in message news:ewfKaCRuIHA.5832@xxxxxxxxxxxxxxxxxxxxxxx
"Siv" <g@xxxxxxxxxxxxxxxxxxxxxxxx> schrieb
Armin,

I added the Excel reference using the
"Microsoft.Office.Interop.Excel" reference in the Dot Net tab. Does
adding that as a COM object work better and if so why?

Did you already have that reference before asking, or did you just add
it now? Inferred from your question, I thought you did not have any
reference yet. Obviously the PIAs (primary interop assemblies) are
installed on your machine. In this case, it's the preferred way instead
of the COM reference. The PIAs are taylor-made by the manufacturer of
the COM component (Excel). Otherwise, when setting the COM reference,
the Interop assembly is created by a standardized COM-Import procedure
that possibly doesn't fit 100% perfect and is recreated in every project
and every time you set a reference to the COM component.

Assuming that you have a reference to the assembly, the problem
is probably because the type of the default property of a range object
is "Object". Not every object has a property "Formula". Therefore the
error message. You'll have to cast it to the expected type. I don't know
it by heart; something like

Directcast(XLApp.Cells(r, c), DestinationType).Formula ....


Armin







You were asking about why late binding is "bad". When you late bind there is a perfomance penalty to pay for each call. Since the compiler does not know what type of object is being used until runtime it must each time determine whether or not the property, function or subroutine is valid for the target object at runtime. This takes time.

There is also the possiblity that the object at runtime will not support the call. This will result in an exception which can be trapped using try - catch but again this is very costly in comparison to an early binding call.

So really two reason not to use late binding if you can. One performance and second program integrity.

LS

.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... Even if we grant that, there remain others which are not matters of opinion, ... and cannot use it as a *reference*. ... Failure to do so causes common bugs. ... I do compiler stuff; we don't actually do our own compiler/libc ...
    (comp.lang.c)
  • Re: Late Binding Question
    ... I have temporarily changed the late binding error as a warning and when I do that the green wavy line appears under the left side of the argument. ... I still don't understand why the compiler thinks this is late bound?? ... "Microsoft.Office.Interop.Excel" reference in the Dot Net tab. ... Obviously the PIAs (primary interop assemblies) are ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Using early-bound interface on a late-bound object
    ... > the compiler determines which interfaces to use, ... > supports the declared interface. ... >> help if someone can point me to some authoritative document or reference ... within customer shops when they mirrored this 'division' to keep their ...
    (microsoft.public.vb.general.discussion)
  • Re: Garbage Collection Eligibility and portability
    ... (in the parlance of compiler writers). ... There isn'tany way to get a given Lisp to act that way. ... that alters exactly when a variable reference to a memory location ceases holding it from collection. ... The other, which I've seen called "Aggressive garbage collection" in some articles on this behavior in C#/.Net, appears to make a memory location eligible the instant the last reference to the location is used. ...
    (comp.lang.lisp)
  • Re: Why we should (not?) have closures after all
    ... Programmers should have tools that can tell them as much as possible about their code's likely behavior. ... The wart can be used anywhere a type is used in declaring a reference ... The compiler does nothing special if a "can be null" reference is ... it would be nice if there was a variant of the "assert" statement that was always enabled and threw IllegalArgumentException instead of AssertionError. ...
    (comp.lang.java.programmer)