Saturday, 17 April 2010

SOLVED - 'srcDomainMulti' is not declared. It may be inaccessible due to its protection level

When converting a project from C# to VB I ran into this compiler error which stopped me in my tracks for about 15 minutes. Just when I gave up and decided to rebuild the page by hand inspiration hit and I found my solution...

The scenario

I had just put the finishing touches to a domain whois tool and I was ready to convert it over to a VB project. I'm not a very strong VB.net coder but the project required the final deliverable in VB so I opted to make it all in C# first and then convert it over using one of the great free tools for converting code online.

After creating my new solution, adding the website and the class library, creating the stub pages and running all of the code files through the online code converter I was almost ready to go.

The last step was to convert the aspx ui page. I was vaguely aware that in some situations there are differences between the syntax in these pages but I kept my fingers crossed that I wouldn't have to remember these little nuances so I could ship it quickly.

The problem

Hitting compile I was hit with the an error which confused me:

Error 1 'srcDomainMulti' is not declared. It may be inaccessible due to its protection level.

Double clicking it took me to a line in my code behind where I was tweaking a parameter of an ObjectDataSource just before it bound. The code couldn't see the ObjectDataSource on my page!

I played around for a few minutes, even rolling up my sleeves and writing some code to FindControl it just in case it was a naming container issue I was unaware of:

Dim srcMulti As ObjectDataSource = DirectCast(Me.FindControl("srcDomainMulti"), ObjectDataSource)
srcMulti.SelectParameters("PopularOnly").DefaultValue = "False"

But the page still didn't work. In the end I gave up and started recreating the page by dropping controls onto a new VB page and setting them up item by item. When it came to binding the first drop down list I noticed something odd. The namespace of my DataObject was listed as:

WhoisLibrary.WhoisLibrary.TldCollection

The solution

The solution was really straightforward in the end, as nearly all problems turn out. Without looking into this too deeply it seems that VB holds your hand when it comes to namespaces and automatically wraps the class libraries files in a namespace.

Each of the .vb files I had run through the converter also had a namespace declaration wrapped around them which was doubling up on my code and making everything invisible.

I whipped through the project removing all my namespace wrappers which removed the middle namespace layer, pressed compile and the project worked without any further grumbles!

kick it Shout it vote it on WebDevVote.com

4 comments:

Anonymous said...

your solution is really VAGUE!

rtpHarry said...

@Anonymous; The solution is that if you did it with an online converter then you should check to see if there is a namespace declaration that wraps the entire file and try removing it if that is the case. You're comment is vague as well :) Perhaps you could explain which part I am loosing you at?

Rob Brahier said...

I ran into this error tonight while converting from VB to C#, but for a different reason. If you have a multi-project solution then this error will also come up if a dependency (or a dependency of a dependency) won't compile. It can be tricky to spot since there are usually a LOT of errors during this type of conversion. Sorting the error list by File can make tracking this down a little easier.

Lausten North said...

I think what anonymous is saying is, what's a namespace wrapper? Where would one look for one?