C# IF Statement from VB

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2066 Views - Last Post: 02 April 2016 - 06:17 AM Rate Topic: -----

#1 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

C# IF Statement from VB

Posted 29 March 2016 - 06:56 AM

Here is the deal, I have a program that I have to move to C# mainly because it is old and the company wants to move to C#.
Below you will find the code from the VB program which I am attempting to move into a C# program. Both are in classes of their own GenerateExcelReport.vb and GenerateExcelReport.cs. The problem I am facing is that I cannot get the variables from my C# GenerateExcelReport.cs into the IF statement. A visual might be more helpful:
--------------------------------------------------------------------------------------------------------------------------

Public Shared Sub ExcelReport(ByVal table As DataTable, ByVal WorksheetsNum As Object, ByVal xlsChartTitle As String)

        xlsSheet = xlsWB.Worksheets(WorksheetsNum)
        For i As Integer = 2 To 14
            xlsCell = xlsSheet.Cells(i, 3)
            xlsCell.Value = ""
        Next
        If MainForm.ShiftCmbox.SelectedIndex = "0" Then <---- Here is the IF statement for VB -->
            xlsCell = xlsSheet.Cells(2, 2)
            xlsCell.Value = "7:00"
            xlsCell = xlsSheet.Cells(3, 2)
            xlsCell.Value = "8:00"
            xlsCell = xlsSheet.Cells(4, 2)
            xlsCell.Value = "9:00"
            xlsCell = xlsSheet.Cells(5, 2)
            xlsCell.Value = "10:00"
            xlsCell = xlsSheet.Cells(6, 2)
            xlsCell.Value = "11:00"
            xlsCell = xlsSheet.Cells(7, 2)
            xlsCell.Value = "12:00"
            xlsCell = xlsSheet.Cells(8, 2)
            xlsCell.Value = "13:00"
            xlsCell = xlsSheet.Cells(9, 2)
            xlsCell.Value = "14:00"
            xlsCell = xlsSheet.Cells(10, 2)
            xlsCell.Value = "15:00"
            xlsCell = xlsSheet.Cells(11, 2)
            xlsCell.Value = "16:00"
            xlsCell = xlsSheet.Cells(12, 2)
            xlsCell.Value = "16:30"
            xlsCell = xlsSheet.Cells(13, 2)
            xlsCell.Value = "17:00"
            xlsCell = xlsSheet.Cells(14, 2)
            xlsCell.Value = "17:30"

            xlsApp.Workbooks(1).Worksheets(WorksheetsNum).select()
            xlsSheet.Select()
            xlsApp.ActiveChart.CharTitle.Text = MainForm.StartDate.Text & " Day Shift " & xlsChartTitle & "Hourly Yield "

        End If




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Added using statements below. DO NOT REMOVE THESE!
using Excel = Microsoft.Office.Interop.Excel;
//
namespace EMS_Hourly_Reporting
{

    public class GenerateExcelReport
    {
        public static Excel.Application xlsApp;
        public static Excel.Workbook xlsWB;
        public static Excel.Worksheet xlsSheet;
        public static Excel.Range xlsCell;

        public static void openFile(string Filename)
        {
            xlsApp = new Excel.Application();
            xlsWB = xlsApp.Workbooks.Open(Filename);
                     
        }

        public static void ExcelReport(Excel.DataTable table, object WorksheetsNum, string xlsChartTitle)
        {

            xlsSheet = xlsWB.Worksheets(WorksheetsNum); <!-- Here xlsWB.Worksheets Error Non-Invocable Member -->
            for (int i = 2; i <= 14; i++)
            {
                xlsCell = xlsSheet.Cells(i, 3);
                xlsCell.Value = "";
            }
            if ()<----- Here I cannot place cmbShift a combo box which is from another form into the ()-->

        public static void CloseFile(string newFilename)
        {
            xlsWB.SaveAs(newFilename);
            xlsWB.Close();
            xlsApp.Quit();

        }

    } // public class GenerateExcelReport closing
            
} //namespace closing




I'm sorry if this is all confusing.

Is This A Good Question/Topic? 0
  • +

Replies To: C# IF Statement from VB

#2 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: C# IF Statement from VB

Posted 29 March 2016 - 07:11 AM

So 'MainForm' is one of those global variable carry overs from vb6. It's part of the Microsoft.VisualBasic.ApplicationServices namespace, and is not used in C#:
https://msdn.microso...(v=vs.110).aspx

If you want global access to the "MainForm", you'll need to set it up your own global variable for it, set it when you create the form considered the 'main form', and access that. That or completely refactor the project.
Was This Post Helpful? 0
  • +
  • -

#3 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

Re: C# IF Statement from VB

Posted 29 March 2016 - 07:16 AM

Sorry, I may misspoke. The MainForm in the VB program isn't what I am trying to access. I am trying to access the cmbShift combo box that is on the C# mainForm : Form. I am basically taking what is in the VB code and transferring it to C# obviously not just a copy and paste.
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,209
  • Joined: 12-January 10

Re: C# IF Statement from VB

Posted 29 March 2016 - 07:22 AM

found this little snippet on excel dropdowns that may help...
(assuming: using Excel = Microsoft.Office.Interop.Excel)
Excel.DropDowns allDropDowns = YourWorkSheet.DropDowns(Type.Missing);
Excel.DropDown oneDropdown = allDropDowns.Item(YourIndex);


from

http://www.dreaminco...0&#entry2243112
Was This Post Helpful? 0
  • +
  • -

#5 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

Re: C# IF Statement from VB

Posted 29 March 2016 - 07:32 AM

View PostDarenR, on 29 March 2016 - 07:22 AM, said:

found this little snippet on excel dropdowns that may help...
(assuming: using Excel = Microsoft.Office.Interop.Excel)
Excel.DropDowns allDropDowns = YourWorkSheet.DropDowns(Type.Missing);
Excel.DropDown oneDropdown = allDropDowns.Item(YourIndex);


from

http://www.dreaminco...0&#entry2243112


Thanks, however, the dropdown I am using isn't in an Excel sheet or workbook, it is on a Windows Forms Application.
Was This Post Helpful? 0
  • +
  • -

#6 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: C# IF Statement from VB

Posted 29 March 2016 - 07:33 AM

To get a hold of 'cmbShift', you'll need a reference to the form that 'cmbShift' is a member of. There could be any number of forms in existence, though usually only one.

In VB.Net that form is stored at startup in a global variable called 'MainForm', and that's how your program is referencing it.

In C# it does not exist. You need to create the global variable, or refactor so that you can get a reference to the form (and subsequently the control in the form) in some manner.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# IF Statement from VB

Posted 29 March 2016 - 03:57 PM

A couple SERIOUS problems here.

One: You're using WinForms. That is already end-of-life technology. Its really silly to take a dead program and invest all this effort and money into re-writting for another dead system.

Two: You don't know how much you don't know, about the environment you're trying to develop for. The idea that you're trying to get ahold of the combobox that's on the form makes me cringe. Your data (your logic) should never know anything about the GUI that is being used to present it. You shouldn't be trying to get ahold of the combobox. You should only be updating a collection of values in your data; and that data happens to be binded to your GUI. But your data doesn't know that. Your data needs to be blissfully ignorant of whether it is in WinForms, a console app, WPF, or being sent out through a REST call to a web browser.

I would urge you to hire/contract a developer from THIS decade to make the new program for you. Taking someone that only understands 20 year old paradigms and asking them to suddenly be up-to-date to make a modern program is silly - unless your employer is willing to send you to school to bring your skills forward about 2 decades.
Was This Post Helpful? 0
  • +
  • -

#8 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

Re: C# IF Statement from VB

Posted 29 March 2016 - 04:05 PM

Maybe you need to seriously take some manor classes from this decade. If I wanted to be trashed talked for trying to learn I would've added these questions to Facebook. The fact that you think you are a know-it-all is appalling to me at the least. I am trying to learn on my own because my employer will not pay for this. I have recently read that using WPF is more up-to-date or even using MVC. There are parts of your reply that I do appreciate however, that is the fact you pointed out we shouldn't be trying to obtain information from the GUI etc. so thank you for that.

This post has been edited by tlhIn`toq: 30 March 2016 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# IF Statement from VB

Posted 29 March 2016 - 04:19 PM

You seemed to have confused honest peer-to-peer comments with insults.
At no time did I insult you, call you names etc. I merely pointed out major flaws in the project. I'm sorry if it bothers you that your skillset might be one of those weaknesses. But that's not a reflection on who you are as a person. Just that you haven't kept your skills current. There's no shame in that. Nobody can know everything. I am vastly behind on certainly web technologies and only started doing mobile app development 4 months ago. There are 20 year olds that can code circles around me on both fronts.

I'm not ashamed nor embarrassed to admit to the gaps in my knowledge. You can't be in this profession because if you don't admit them and accept them, then you don't admit you need to work at learning in those areas.

So don't read in to anything I said. Its not fair to me as the writer if you as the reader choose to magnify what I wrote, add in connotation that wasn't there, or imagine a tone of voice that doesn't exist in the pureness of words on a screen.

I did not insult you. If anything I wrote threatened you then I suggest you recognize that as an area to put on your "I need to study" list.

This post has been edited by tlhIn`toq: 29 March 2016 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

Re: C# IF Statement from VB

Posted 29 March 2016 - 04:29 PM

I apologize to you. I guess I am so dang frustrated with trying to learn C# because the only language in programmed in was Q-Basic ( hahah yeah that long ago). I've found some decent courses on Udemy from this guy named Mosh. They seem to be helping a lot. Thank you for your input and again I am sorry I magnified the honesty of your statements. If you have other suggestions on courses etc I am all eyes and ears. But also it is very troubling to know that a company is using such out dated stuff and expect someone to do a job and not be willing to pay for or at least assist in them obtaining education needed.

This post has been edited by tlhIn`toq: 30 March 2016 - 03:58 PM
Reason for edit:: Needless quote of entire previous post removed

Was This Post Helpful? 1
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# IF Statement from VB

Posted 30 March 2016 - 04:07 PM

You may have noticed at the head of the C# thread is a pinned post with lots of resources.

Things you need to be aware of:

Nobody hires for WinForms anymore. Its end of life. You want to be targeting your education and design plans to WPF/MVVM.

There are many good books along the lines of "Learn C# in 30 days". WORK the crap out of those from cover to cover. Don't move to chapter 2 until you KNOW chapter 1. This is not history class where you can get an 'F' the first semester on French 17th century, and still get an 'A' the second semester on WWII. In C# as with any language the second chapter builds on the lessons of the first... the 19th chapter builds on 1-18. If you have a shaky foundation to start with then everything after is pointless.

Many of those books will still target WinForms. They will teach you C#, but don't take the program design principals to heart. Just worry about the language. But if you can find C# books targeting WPF you want them.

One of the biggest foundation concept differences between your previous BASIC language and C# is that C# is an Object Oriented language. You make objects in code (such as a Truck). The object has properties such as .Color and .VINnumber and .NoOfDoors - Being able to grasp OOP design is crucial. If you open up my signature block you'll see links for tutorials on Classes, Properties and Events. These are three most important concepts to start with. Then go from there.
Was This Post Helpful? 0
  • +
  • -

#12 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

Re: C# IF Statement from VB

Posted 30 March 2016 - 04:24 PM

Thank you very much for that response. Your response was very informative and I will take note of the book. I guess it is just very hard being under pressure to do all this in a few weeks. Mainly because the old programmer left and will not respond back to our emails to fix it.

This post has been edited by andrewsw: 30 March 2016 - 04:25 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: C# IF Statement from VB

Posted 30 March 2016 - 04:27 PM

Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.

I do like the phrase "..I magnified the honesty of your statements" though ;)

View Posttadams2179, on 30 March 2016 - 11:24 PM, said:

..because the old programmer left and will not respond back to our emails to fix it.

I am curious, why is the previous programmer under an obligation to "fix" it?
Was This Post Helpful? 0
  • +
  • -

#14 tadams2179   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 16

Re: C# IF Statement from VB

Posted 30 March 2016 - 04:29 PM

Because she signed an agreement stating that she would and that she would be compensated for her time as well.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: C# IF Statement from VB

Posted 30 March 2016 - 05:05 PM

It is the word "fix" that I wanted to emphasize. You say they are moving to C#, so in what sense was the code "broken"? I'm labouring the point but it just seems a little odd that she would be under an obligation.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2