Question Please Help me convert this VB code to C#

Yacine

New member
Joined
Mar 20, 2023
Messages
3
Programming Experience
1-3
For Each row As DataGridviewRow In DataGridview1.Rows Dim expdate_v As Date - row.Cells ("exp_date") .Value Dim calcdays_v As Integer - DateDiff(DateInterval.Day, Date.Now, expdate_v) Next row.cells("rdays"). Value - calcdays_v
 
First things first, how about you post the actual code? That's not valid VB code as it is and I think that there is a line outside the loop that should be inside.

Secondly, how about you format the code properly? Expecting us to work out where the line breaks should be when you presumably already know is making the job harder for no reason.

Finally, you say "help" but it seems like you just want us to do it for you. What have you done and where are you stuck? What exactly do you not understand about the code you have? Presumably you want to use it for a specific purpose so you should have a pretty good idea of what it actually does. How would you do that in C# normally?

We can help but you need to make some effort too and not only have you tried to make it easier for us to help but you've actually made it harder.
 
Also, please post in the most specific forum that applies to the issue, rather than just the first one you come to. This is not about VS at all, so it doesn't belong in VS.NET General. Maybe VB.NET General but the DataGridView is a Windows Forms control so that is even more appropriate. Thread moved.
 
Of course I could help. I've already let you know what you need to do to get that help. I'll repeat it:
  • Post the actual code you want to translate
  • Format the code properly
  • Explain what your actual problem is
We are happy to make an effort for you if you're prepared to help yourself and us.
 
Thanks in advance for your Help me Sir!

I've attached the screenshot of the VB code that I'm having a problem to translate to C# code
I'm just having a problem converting the last part of the code
(


For Each row As DataGridviewRow In DataGridview1.Rows Dim expdate_v As Date - row.Cells ("exp_date") .Value Dim calcdays_v As Integer - DateDiff(DateInterval.Day, Date.Now, expdate_v) Next row.cells("rdays"). Value - calcdays_v

)

Capture 2023-03-20 121733.jpg
 
Form5, table1, column4, DataGridView1

You're allowed to name things other than "type followed by number" (and I know you know how, because I see `dt`, `da` etc - what's bonkers is your local variables are relatively well named even though they're never seen, and your public members have awful names).

Just imagine if microsoft did that with all the classes they wrote; your code would look like:

C#:
  conn.Method1()
  ...
  da.SqlCommand4 = cmd
  ...
  dt.Method37()
  da.Method5(dt)
  DataGridView1.DataTable2 = dt
  conn.Method7()
  DataGridView1.Boolean16 = True
  ...
  column4.String12 = "rdays"
  column4.String82 = "Remaining Days"
  column4.Integer17 = 200
  DataGridView.DataGridViewColumnCollection3.Method19(3, column4)

It's complete nonsense.

This should be an interesting read:

Reverse engineering obfuscated assemblies [updated 2019] | Infosec Resources

By writing code filled with meaningless names, and then asking other people for help, you're essentially performing some mild obfuscation of it, and expecting people to work it out. It gets considerably harder the more you do this, and the longer it goes on

I'll llet others chime in on the etiqette of posting code as a screenshot, but suffice to say when you want help for free, make yourself easy to help

I recommend to striaghten the code out, and post it as formatted text, using [code] tags, so that humans will at least be interested and you've had a go ..
..or use a machine (which won't care about this rpesentation) to help: Convert VB.NET to/from C# online
 
Last edited:
This is funny. I was about to chastise you for posting a bad picture of the code when I specifically asked that you post the code formatted properly and then I realised that that picture might be all you have. That explains why you have actually transcribed the code badly by putting a line outside the loop that should be inside it and using subtraction operators (-) when they should be assignment operators (=). Why do you only have a bad picture instead of actual code? Why is it that you can translate some code but not some other that is no more complicated? How is it that you can't recognise a variable assignment if you know anything about programming? Why is it that, despite being asked, you still haven't explained what the actual problem is? You just keep saying "here's some code, convert it for me because I can't". That's not an explanation of a problem. When someone goes to this much trouble to not explain a problem, it smells fishy. You can clear that up quite easily.
 
Back
Top Bottom