Late binding problem

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have the following in my VS 2008 code:

parameters(0).Value = Session("User").CompanyID

I assume this means that since there is no "User" at this point I can't do
this.

But it will be there and the User Object has a CompanyID property.

How can I access this in my code?

Thanks,

Tom
 
I also have the same problem with:

Sub SetMoveList_Click(ByVal s As Object, ByVal e As EventArgs)
Trace.Warn("in SetMove_Click s.id = " & s.id)
Dim oPanel As Panel = s.parent
Dim oDLI As DataListItem = s.parent.parent

I am getting the error on s.id, s.parent and s.parent.parent.

I used to do this in 2003 - how would I do the same thing here?

Thanks,

Tom
 
tshad said:
I have the following in my VS 2008 code:

parameters(0).Value = Session("User").CompanyID

I assume this means that since there is no "User" at this point I can't do
this.

But it will be there and the User Object has a CompanyID property.

How can I access this in my code?

Hi Tom,
as far as I see you have the problem that "CompanyID" is not known at
that moment (or is this different in VB.NET?). In that case you have to
cast the result of Session("User") to the object you expect. Maybe this
helps you: http://www.codeproject.com/KB/dotnet/CheatSheetCastingNET.aspx

Best,
Hilmar
 
Mark Rae said:
You appear to be using VB.NET instead of C#, so it's probably something
like:

If Session("User") Is Not Nothing Then
parameters(0).Value = Session("User").CompanyID
End If
You're right I am using VB.Net and Option Strict On.

Tried that but got the same error.

The actual statement is:

If Not Session("User") Is Nothing Then

But that didn't help in any case.

Thanks,

Tom
 
Mark Rae said:
Are you going to tell the group what the error actually was...?

My mistake. I thought I did.

I am getting:

Error 54 Option Strict On disallows late binding.

I get this for:

parameters(0).Value = Session("User").CompanyID

Where User is a class.

And I also get it for:

Sub SetMoveList_Click(ByVal s As Object, ByVal e As EventArgs)
Trace.Warn("in SetMove_Click s.id = " & s.id)
Dim oPanel As Panel = s.parent
Dim oDLI As DataListItem = s.parent.parent

Here I am trying to get the Panel and DataListItem which works fine if
Option Strict is Off.

Why would this be a problem with it on. "s" is an object and objects have
parents.

Thanks,

Tom
 
tshad said:
Mark Rae said:
Are you going to tell the group what the error actually was...?

My mistake. I thought I did.

I am getting:

Error 54 Option Strict On disallows late binding.

I get this for:

parameters(0).Value = Session("User").CompanyID

Where User is a class.

And I also get it for:

Sub SetMoveList_Click(ByVal s As Object, ByVal e As EventArgs)
Trace.Warn("in SetMove_Click s.id = " & s.id)
Dim oPanel As Panel = s.parent
Dim oDLI As DataListItem = s.parent.parent

Here I am trying to get the Panel and DataListItem which works fine if
Option Strict is Off.

Why would this be a problem with it on. "s" is an object and objects have
parents.

Thanks,

Tom

First off objects do not have parents. What you need to do is use CType
to tell the compiler what the objects are. For example s is not an
object, it is a class that is derived from object. So whatever
SetMoveList is , is the object type in the CType statement.

Something like:

dim oPanel as Panel = ctype(s,HtmlButton).parent

This will allow the compiler to know that s is infact an HtmlButton (you
need to know what type it is).

You will have to do the same with s.parent.parent

LS
 
tshad said:
I figured it out.

s.parent

needs to be changed to:

CType(s, DataGridItem).Parent.Parent

This doesn't make a lot of sense, however.

We know that "s" is an object and objects have parents as properties (don't
they?).

So why do I need to change this:

s.Parent.GetType().ToString()

to
CType(s, ImageButton).Parent.GetType().ToString()

Is that really late binding?

Thanks,

Tom
Thanks,

Tom
 
Lloyd Sheen said:
tshad said:
My mistake. I thought I did.

I am getting:

Error 54 Option Strict On disallows late binding.

I get this for:

parameters(0).Value = Session("User").CompanyID

Where User is a class.

And I also get it for:

Sub SetMoveList_Click(ByVal s As Object, ByVal e As EventArgs)
Trace.Warn("in SetMove_Click s.id = " & s.id)
Dim oPanel As Panel = s.parent
Dim oDLI As DataListItem = s.parent.parent

Here I am trying to get the Panel and DataListItem which works fine if
Option Strict is Off.

Why would this be a problem with it on. "s" is an object and objects
have parents.

Thanks,

Tom

First off objects do not have parents. What you need to do is use CType to
tell the compiler what the objects are. For example s is not an object,
it is a class that is derived from object.

What?

It is defined as an object:

ByVal s As Object
So whatever SetMoveList is , is the object type in the CType statement.
I thought objects had parents. Maybe that was because I used to write my
code with Option Strict Off (actually defaulted to that) and I would use
s.Parent all the time.

If objects don't have parents, do I need to change:

s.Parent,Parent,Parent to something like:

CType(CType(Ctype(CType(s,something).Parent,something).Parent,something).Parent,something)

Because in each case we would need to know what the parent is.

Also, why does this work:

I don't this is the case as this works:

Dim theParent As Control = CType(sender,DataGridItem).Parent.Parent

I don't need to cast each parent.

But I do need to cast this:

Dim oDataList As DataListItem = CType(CType(s, ImageButton).Parent.Parent,
DataListItem)

Doesn't seem too consistant
Something like:

dim oPanel as Panel = ctype(s,HtmlButton).parent

But would you not nee to Ctype the whole thing as Panel?
 
tshad said:
tshad said:
I figured it out.

s.parent

needs to be changed to:

CType(s, DataGridItem).Parent.Parent

This doesn't make a lot of sense, however.

We know that "s" is an object and objects have parents as properties (don't
they?).

So why do I need to change this:

s.Parent.GetType().ToString()

to
CType(s, ImageButton).Parent.GetType().ToString()

Is that really late binding?

Thanks,

Tom
Thanks,

Tom


The Object class is the base class for all others. It does not have a
parent property.

LS
 
Lloyd Sheen said:
The Object class is the base class for all others. It does not have a
parent property.

Ok, that makes sense. But then why does this work?

Dim oDGI As DataGridItem = CType(CType(s, RadioButton).Parent.Parent,
DataGridItem)

How come I can do a Parent.Parent?

Isn't the first Parent an object - CType(s, RadioButton).Parent - since I
didn't type that one? The CType for the 2nd Parent I understand?

Thanks,

Tom
 
tshad said:
Ok, that makes sense. But then why does this work?

Dim oDGI As DataGridItem = CType(CType(s, RadioButton).Parent.Parent,
DataGridItem)

How come I can do a Parent.Parent?

Isn't the first Parent an object - CType(s, RadioButton).Parent - since I
didn't type that one? The CType for the 2nd Parent I understand?

Thanks,

Tom
You have given the compiler a start on what the "object" really is.
When you "cast" the object as a RadioButton the compiler will know what
that the parent of a RadioButton is a certain set of objects, all of
which have a parent property.

LS
 
Back
Top