VB.NET solution doesn't port to C#.NET

  • Thread starter Thread starter al-s
  • Start date Start date
A

al-s

I have a working VB.NET 2005 (.NET2.0) solution that works. When I
translate the code modules, form modules to C# I get all sorts of
build errors, much of which are related to scoping of methods.

I have the following given in a form module:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SPORTLinkCSC
{
public partial class SPortLink : Form
{
public SPortLink()
{
InitializeComponent();
}

static SPortImplement si = new SPortImplement();

private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}

void FormLoad(object sender, EventArgs e)
{
this.txtRcvMsg.Text = "Waiting to receive message";
this.txtSndMsg.Text = "Waiting to send message";
InitBoard();
}
}
}

I also have the following in a code module which contains methods that
I want the form to call:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace SPORTLinkCSC
{
public class SPortImplement
{

private void InitBoard()
{
Int32 status = 0;
//ulong address = 0;

SPortLink.txtStatusMsg.Text = "Opening BlackTip for
Reading/Writing messages";

}
}
}

When trying to build, I get the errors:
1) An object reference is required for the nonstatic field, method,
or property
2) The name 'InitBoard' does not exist in the current context

The code module and the form module are in seperate files both in the
VB.NET project and in the C# project. Why after converting the code
from language to another does the whole thing fall apart? The two
languages are relatively close and I believe I converted them
properly. I can provide the VB.NET code of these two modules if it
would help.

Any insight or help greatly appreciated.

Al
 
Hi Al,

Well,

SPortLink.txtStatusMsg.Text = "Opening BlackTip for Reading/Writing messages";

.... won't work because SPortLink does not have a static reference calledtxtStatusMsg. You may have wanted to do

SPortLink link = new SPortLink();
link.txtStatusMsg.Text = "Opening BlackTip for Reading/Writing messages";

.... which won't work either unless txtStatusMsg is public


InitBoard() won't work from SPortLink since it does not have this method.. That method is defined in SPortImplement
 
I would translate the module to a C# class with all the methods being static.
Private void InitBoard()
then becomes
public static void InitBoard()
and the call becomes
SPortImplement.InitBoard();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top