Option Strict On related problem.

J

Jorge Cavalheiro

Hello
After reading yesterday's thread on performance of C# and
vb.net i've just turn it on my solution and spent 2 hours
correcting the errors :)

I have a problem i don't how to fix i have a class that
export data to a Excel Workbook.
I get 2 types of errors : Option Strict On disallows
implicit conversions from 'System.Object'
to 'Excel.Worksheet' and Option Strict On disallows late
binding.

This how the excel objects are defined :
Dim objPrograma As New Excel.Application
Dim objLivro As Excel.Workbook = objPrograma.Workbooks.Add
Dim objFolha As Excel.Worksheet = objPrograma.ActiveSheet

The following raise errors:
objLivro.Sheets("Folha1").name = "Português"
objLivro.Sheets("Folha2").delete()

And also:
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()

For example the following works ok:
objFolha.Range("a4").VerticalAlignment =
Excel.XlHAlign.xlHAlignCenter
objFolha.Range("a4").HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter


Any suggestion how to solve this matter, would be most
welcome thanks!

Kind Regards
Jorge Cavalheiro
 
T

Trev Hunter

Jorge,
After reading yesterday's thread on performance of C# and
vb.net i've just turn it on my solution and spent 2 hours
correcting the errors :)

Glad you made the change - it's always a good idea to switch "Option Strict
On" all the time. Apart from performace gains because the compiler can
optimise better, it should also give you a better understanding of how
different types convert to and from other types. The better you know how
this process works, the more optimzations you can use in your own code
because you can tweak your design.
The following raise errors:
objLivro.Sheets("Folha1").name = "Português"
objLivro.Sheets("Folha2").delete()

As mentioned, Option Strict ON does not allow late binding. I'm not
familliar with Excel's object model, but I suspect that
"objLivro.Sheets("Folha1")" returns an Object, not a specific type.

Try wrapping the call in a call to "ctype" or "directcast" like this

---------------

directCast(objLivro.Sheets("Folha1"), Excel.WorkSheet).name = "Português"
directCast(objLivro.Sheets("Folha2"), Excel.WorkSheet).delete()

---------------
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()

Try the following
---------------

directCast(objLivro.Sheets("English"), Excel.WorkSheet).activate()
objFolha = directCast(objPrograma.ActiveSheet, Excel.WorkSheet)

---------------

Obviously, change the type"Excel.WorkSheet" to whatever type you are
expecting. I would also consider using a With Block or a local variable to
hold a strongly typed reference to the objcts you are accessing - this way
you cut down the number of calls to DirectCast

HTH,

Trev.
 
A

Armin Zingler

Jorge Cavalheiro said:
I have a problem i don't how to fix i have a class that
export data to a Excel Workbook.
I get 2 types of errors : Option Strict On disallows
implicit conversions from 'System.Object'
to 'Excel.Worksheet' and Option Strict On disallows late
binding.

This how the excel objects are defined :
Dim objPrograma As New Excel.Application
Dim objLivro As Excel.Workbook = objPrograma.Workbooks.Add
Dim objFolha As Excel.Worksheet = objPrograma.ActiveSheet

The following raise errors:
objLivro.Sheets("Folha1").name = "Português"

I assume Sheets only contains Sheet objects:

Directcast(objLivro.Sheets("Folha1"), Excel.Sheet).name = "Português"
objLivro.Sheets("Folha2").delete()

Directcast(objLivro.Sheets("Folha2"), Excel.Sheet).delete
And also:
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()


Use directcast also.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top