Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Tricky VB to C# conversion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
meerkatinfrance@hotmail.com - 01 Mar 2008 09:41 GMT
I am trying to convert some VB that uses Linq into C#
Is there anyone out there who is kind enough (clever enough) to show
me how to convert the following Sub loadAddress into C# for me.

Sub DoIt()
Dim WWV5 As New WWV5DataContext
Dim Xorg As Object

If Roles.IsUserInRole("Admin") Then
Xorg = (From o In WWV5.ORGs Where o.OrgID = ID Select o).Single
Else
Xorg = (From o In WWV5.ORGScopies Where o.OrgID = ID Select o).Single
End If
loadAddress(Xorg)
End Sub

Protected Sub loadAddress(ByRef pOrg As Object)
txtName.Text = pOrg.OrgName
txtCharNum.Text = pOrg.CharNum
txtAdd1.Text = pOrg.Add1
End Sub
Marc Gravell - 01 Mar 2008 10:56 GMT
This is complicated because it appears that the VB is using "Option
Explicit Off" or "Option Strict Off" (I'm nto really a VB-person, so
I'm not sure which ;-p) - i.e.

> Protected Sub loadAddress(ByRef pOrg As Object)
>   txtName.Text = pOrg.OrgName
> ...

I C# you can't do this; you'd need to declare pOrg as something more
specific; I'm guessing that there is a suitable type here - possibly
"Org" or something (not clear from the code); basically, whatever Xorg
really is. If "ORGs" and "ORGscopies" return completely different
types, you may need to use a common interface, but I'm guessing that
they return the same type.

Once you've typed XOrg and pOrg correctly, the rest should be simple -
something like:

Org org;
if(Roles.IsUserInRole("Admin")) {
 org = (from o in WWV5.ORGs where o.OrgID == ID select o;).Single();
} .. etc
LoadAddress(org);

protected void LoadAddress(Org org) {
 txtName.Text = org.OrgName;
 // etc
}

Note I dropped the "ByRef" since it is unnecessary even in the VB: you
aren't reassigning the instance.

Actually, for very simple uses like above, I tend to find the
"regular" syntax clearer than the LINQ syntax; I'd be inclined to
write:

org = WWV5.ORGs.Where(o => o.OrgID == ID).Single();

Marc
meerkatinfrance@hotmail.com - 01 Mar 2008 12:05 GMT
Hi Marc.
Thankyou so much for your reply.
I have noted your recommendations and will use them in future.

It is clear that you know what you are talking about, so I am
embarrassed to admit that I could not find your solution.

Either it is staring me in the face or I did not explain the problem
correctly.

You are right - Orgs and OrgsCopy are two Identical SQL database
tables ( the data in them is obviously different)

You say that I need to declare something more specific ( not use
object )
The only way I can think of doing this would be to have the following,
but that defeats the object of having a common LoadAddress()

Are you saying I have to do this? I hope not, because that would mean
C# would be impractical. This would be a huge shame, because I was
getting to like it :-(

void getdata(string UserName, int ID)
 {
   WWV5DataContext WWV5 = new WWV5DataContext();

   if (UserName == "Admin")
   {
     ORG Xorg = WWV5.ORGs.Where(o => o.OrgID == ID).Single();
     LoadAddressOrg(Xorg);
   }
   else
   {
     ORGScopy Xorg = WWV5.ORGScopies.Where(o => o.OrgID ==
ID).Single();
     LoadAddressOrgsCopy(Xorg);
   }
 }

void LoadAddressOrg(ORG Xorg)
 {
   txtName.Text = Xorg.OrgName;
   txtAddress.Text = Xorg.Add1;
 }

 void LoadAddressOrgsCopy(ORGScopy Xorg)
 {
   txtName.Text = Xorg.OrgName;
   txtAddress.Text = Xorg.Add1;
 }
meerkatinfrance@hotmail.com - 01 Mar 2008 12:25 GMT
Hello Marc,

Me again !
I forgot to mention that the using statement you suggestion did not
work.  error:Cannot implicitly convert type 'ORGScopy' to 'ORG'

I hope you have not gone home :-)

Regards,

Pete
Marc Gravell - 01 Mar 2008 23:32 GMT
> I forgot to mention that the using statement you suggestion did not
> work.  error:Cannot implicitly convert type 'ORGScopy' to 'ORG'

OK; sounds like my assumption (that they were the same type) was
wrong. Not to worry. There are a few more tricks we can use - the
first is an interface:

public interface IOrg {
 string OrgName {get;}
 string CharNum {get;}
 string Add1 {get;}
}

This says "there is an interface IOrg, that has a few readable named
properties".

Now; if you make LoadAddress accept an IOrg, it should still compile.
The next trick is to make both ORG and ORGScopy implement this
interface. Luckily, partial classes and implicit implementation come
to our rescue. The following additional code should suffice (must be
in the same namespace as you ORG etc):

partial class ORG : IOrg {}
partial class ORGScopy : IOrg {}

This says "here's some extra code for ORG: it implements IOrg"; the
implementations should be found automatically.

Finally we need to ensure that the compile knows what we are doing, so
we change the first line here:

 IOrg org;
 if(Roles.IsUserInRole("Admin")) {
   org = ctx.ORGs.Where(o=>o.OrgID == ID).Single();
 } else {
   org = ctx.ORGScopies.Where(o=>o.OrgID == ID).Single();
 }
 LoadAddress(org);

See how that goes ;-p

Any problems, post back...
meerkatinfrance@hotmail.com - 02 Mar 2008 10:14 GMT
Hi Marc,

I must admit that I was a bit sceptical when I first read your
messsage but I desparately needed this thing to work.

I have not the faintest idea what an Interface is ( but I will now
learn ).
Instead of creating the partial classes you suggested, I just added
the Interface to the end of the ORG and ORGScopy classes of the dbml
file

public partial class ORG : INotifyPropertyChanging,
INotifyPropertyChanged, IOrg

public partial class ORGScopy : INotifyPropertyChanging,
INotifyPropertyChanged, IOrg

I copied the rest of your suggestion to the letter, and all worked
perfectly, although I have no idea why.

I am extremely happy - thank you very much for taking the time to
help.

I can't thank you enough,

Regards,

Pete

>> I forgot to mention that the using statement you suggestion did not
>> work.  error:Cannot implicitly convert type 'ORGScopy' to 'ORG'
[quoted text clipped - 38 lines]
>
>Any problems, post back...
Marc Gravell - 02 Mar 2008 10:29 GMT
>  I just added the Interface to the end of the ORG and ORGScopy classes
> of the dbml file

*important*:

You shouldn't edit the generated code (from the dbml) - it is quite
likely (i.e. guaranteed) to be randomly destroyed when the compiler
decides it wants to update the generated class files.

Partial classes are simply a way of getting around this, by separating
the /user/ code and the /generated/ code into separate files. Hence
the "partial" bit I quoted does exactly the same as adding ": IOrg" to
the generated class, but without the risk of losing your change at the
worst possible moment.

> (interfaces)

Interfaces are a way of describing the signature of an object, without
having to know the implementation details. In particular, it allows us
to describe the common features of ORG and ORGScopy (i.e. that they
both have a few properties such as OrgName), without forcing them to
be the same thing. I'm probably not describing it right; I recommend
finding a good "OO" book?

> thank you very much for taking the time to help.

You are more than welcome; I'm glad it worked ;-p
If you get any problems, post back (but you might want to start a new
topic if it is an unrelated issue).

Marc
Marc Gravell - 01 Mar 2008 10:58 GMT
Oh; one other thing; data contexts are usually disposable, so I'd
actually have something like:

using(var ctx = new WWV5DataContext()) {
 Org org;
 if(Roles.IsUserInRole("Admin")) {
   org = ctx.ORGs.Where(o=>o.OrgID == ID).Single();
 } else {
   org = ctx.ORGScopies.Where(o=>o.OrgID == ID).Single();
 }
 LoadAddress(org);
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.