14 Replies - 1200 Views - Last Post: 24 August 2012 - 01:06 AM Rate Topic: -----

#1 Thinus du Pisanie   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 07-October 11

VB to C# conversion

Posted 23 August 2012 - 12:09 AM

Hi guys I am a vb.net programmer but must do a c# project I have @pieces of code that I don't know how to write in c# and the online converters doesn't work..

For Each m In myq
            txtrole.Text = m.Roles
        Next 


Here I used linq

LunchDataContext cmdatacontext = new LunchDataContext();

 Dim mycheck = From cust In cmdatacontext.tblLogins _
       Where cust.EmployeeName.ToLower = txtuserlogin.Text.ToLower 


If any one can please assist will be nice.

Is This A Good Question/Topic? 0
  • +

Replies To: VB to C# conversion

#2 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: VB to C# conversion

Posted 23 August 2012 - 12:42 AM

For your first piece of code:

foreach (var m in myq)
                txtrole.Text = m.Roles;


I'm not comfotable enough with LINQ to convert your second piece, but a couple of pointers that might help:
  • There is no "Dim" keyword in C#. Instead, you declare the variable type, then the name, like int myNumber;
  • The underscore _ is not used in C# to denote statement continues on the next line. In C#. the semicolon ; denotes the statement has finished, and so the statement can be on any number of lines (although conventions should be followed)

Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: VB to C# conversion

Posted 23 August 2012 - 01:37 AM

Or you can be lazy and say var mycheck if you don't know the the resulting type will be. Let the compiler figure it out for you.

Chances are that you also got a compiler error or warning about cust.EmployeeName.ToLower() = txtuserlogin.Text.ToLower(). You should know that == is the comparison operator, while = is the assignment operator.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: VB to C# conversion

Posted 23 August 2012 - 01:43 AM

And as for string comparisons, you should choose ToLower() as your last resort. Your first choice should be String.Compare(..., StringComparison.OrdinalIgnoreCase). You next choice should be String.ToUpperInvariant().

Read about the Microsoft recommendations to avoid the Turkish-I problem: http://msdn.microsof...y/ms973919.aspx
Was This Post Helpful? 1
  • +
  • -

#5 Thinus du Pisanie   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 07-October 11

Re: VB to C# conversion

Posted 23 August 2012 - 05:46 AM

My vb.net code was this

 Dim mycheck = From cust In cmdatacontext.tblLogins _
       Where cust.EmployeeName.ToLower = txtuserlogin.Text.ToLower



I did this to get to c sharp
dynamic mycheck = from cust in cmdatacontext.tblLogins where cust.EmployeeName..ToLower  = txtuserlogin.Text.ToLower ;


Wich is fine but I get an error saying : "Can not convert string to bool"

Just on the piece saying
cust.EmployeeName..ToLower

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: VB to C# conversion

Posted 23 August 2012 - 05:49 AM

LINQ in C# allows requires a Select part, in VB it's optional unless you're doing a transform.

= is assignment is C# you want == equality check.

Plus you have and extra dot.

This post has been edited by AdamSpeight2008: 23 August 2012 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: VB to C# conversion

Posted 23 August 2012 - 05:50 AM

Try this:

var mycheck = from cust in cmdatacontext.tblLogins where cust.EmployeeName.ToLower()  == txtuserlogin.Text.ToLower() ;



ToLower is a method and must be called as such.
Was This Post Helpful? 1
  • +
  • -

#8 Thinus du Pisanie   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 07-October 11

Re: VB to C# conversion

Posted 23 August 2012 - 06:14 AM

Sorry MrCupOfT typing error.

D.I.C Head thanks it works no error anymore on the previous piece of code I have errornow on the

 ; 


Saying "A query body must end on a Select clause or a group clause"

So I did it this way
  var mycheck = from cust in cmdatacontext.tblLogins where cust.EmployeeName.ToLower() == txtuserlogin.Text.ToLower() select cust; 


Is this the Select what you meant MrCupOfT ?
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: VB to C# conversion

Posted 23 August 2012 - 06:29 AM

Yep.
Was This Post Helpful? 1
  • +
  • -

#10 Thinus du Pisanie   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 07-October 11

Re: VB to C# conversion

Posted 23 August 2012 - 06:46 AM

Thanks I understand now why... one more thing if you call a setting in vb.net you call it so

 My.Settings.backname = "Bleh" 


and in vb you call a form by

 frmnew_Order .show()


How do you call setting in c# and a form or can you tell my please where I can go search for basic c # knowlede will be very helpful thanks.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: VB to C# conversion

Posted 23 August 2012 - 06:54 AM

Depends
frmnew_Order .show()
if

frmnew_Order is the name of a form not an instance of a form.
In vb.net it will show the default instance. Default Instance I treat with contempt.
If it is a reference it'll show that referenced form.

For Each m In myq
            txtrole.Text = m.Roles
        Next 



Why just write
txtrole.Text = myq.Last().Roles


since you are overwriting the previous one each iteration.
Was This Post Helpful? 0
  • +
  • -

#12 Thinus du Pisanie   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 07-October 11

Re: VB to C# conversion

Posted 23 August 2012 - 06:58 AM

frmnew_Order .show() 


This is the form name of the form I want to call from a button click event
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: VB to C# conversion

Posted 23 August 2012 - 07:40 AM

Then you have to create an instance of that form and show that one, as C# thankful doesn't have default instances.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: VB to C# conversion

Posted 23 August 2012 - 07:58 AM

I noticed you used dynamic earlier. I understand you got that from a converter, but I'm going to warn you, any time you need to use dynamic, consider that you might not have come up with the best design.

Dynamic is late-binding. It doesn't give you any type safety, as it will let you store anything at all in it and try to call any method/property/field you want, even if the object doesn't have it. And you won't know until runtime that it didn't work.

Dynamic has some very powerful uses. However, it's usually best to be avoided.
Was This Post Helpful? 1
  • +
  • -

#15 Thinus du Pisanie   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 07-October 11

Re: VB to C# conversion

Posted 24 August 2012 - 01:06 AM

Ey Curtis Rutland I replaced the Dynamic with Var works better but thanks for the thumps up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1