Re: Late Binding Question
- From: "Siv" <g@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 18 May 2008 21:52:23 +0100
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@xxxxxxxxxxxxxxxxArmin,
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@xxxxxxxxxxxxxxxxArmin,
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> schriebArmin,
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
.
- References:
- Late Binding Question
- From: Siv
- Re: Late Binding Question
- From: Armin Zingler
- Re: Late Binding Question
- From: Siv
- Re: Late Binding Question
- From: Armin Zingler
- Re: Late Binding Question
- From: Siv
- Re: Late Binding Question
- From: Siv
- Re: Late Binding Question
- From: Lloyd Sheen
- Late Binding Question
- Prev by Date: Re: End function if it Takes too long
- Next by Date: Re: Late Binding Question
- Previous by thread: Re: Late Binding Question
- Next by thread: Re: Late Binding Question
- Index(es):
Relevant Pages
|