How can I convert this code from vb. net to C#? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to convert vb .net code to c# but I am having problem with the following code:
Dim MI_Display_Channel As New MethodInvoker(AddressOf display_channel)
Private Sub display_channel()
TextBox1.Text = fv_channel
End Sub
How can I convert this piece of code into c#?

I see two answers machine translated where, apparently, a field initializer of an instance field, refers an instance member of the class. That is not allowed.
To be explicit:
class Xxx
{
MethodInvoker MI_Display_Channel = display_channel; // compile-time error!
void display_channel()
{
TextBox1.Text = fv_channel;
}
}
will not compile. When the field initialization is not allowed in a field initializer, use a constructor:
class Xxx
{
public Xxx() // other instance constructors may want to chain : this()
{
MI_Display_Channel = display_channel; // fine
}
MethodInvoker MI_Display_Channel; // no initializer here
void display_channel()
{
TextBox1.Text = fv_channel;
}
}

From here, you get this:
MethodInvoker MI_Display_Channel = new MethodInvoker(display_channel);
private void display_channel()
{
TextBox1.Text = fv_channel;
}
I'm not sure why it was difficult.

Related

C# Global Object [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am building a program for my course in which i need global objects as i intend to have the object accessible from many forms and edited also. Before saying just use global variables i cant the specs state for use of OOP.
my latest attempt to fix this is using a public class but this gave a protection error problem
Code:
Form1.cs (forgot to rename and not re doing all the code and design)
public class ObjectsGlobal
{
Bays bay1 = new Bays();
Bays bay10 = new Bays();
}
frmInput.cs
private void btnAdd_Click(object sender, EventArgs e)
{
if ( 1 == Convert.ToInt32(nudBayNum))
{
ObjectsGlobal.bay1.CarMake = txtMake.Text;
}
}
any ideas are welcome at this point
Change it to:
public static class ObjectsGlobal
{
public static Bays bay1 = new Bays();
public static bay10 = new Bays();
}
Also, as recommended in a comment I have now read, take a look at the Singleton Pattern.

How to create thread according to integer value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to create thread according to integer value. For example if the variable is '5', program should create 5 threads or variable is '2', program should create 2 threads, etc. But I can't understand which path I must follow.
It's just a matter of creating the Thread and start it. But I wouldn't suggest you to handle the thread explicitly, but to use Tasks or ThreadPool in order to execute multithreading work.
using System;
using System.Threading;
public class Program
{
public static void Main()
{
int numberOfRequestedThreads = 3;
for (int i = 0; i < numberOfRequestedThreads; i++)
{
var tempThread = new Thread(new ThreadStart(DoWork));
tempThread.Name = i.ToString();
tempThread.Start();
}
}
public static void DoWork()
{
Console.WriteLine("Thread#{0} is now working!", Thread.CurrentThread.Name);
}
}

Why is my C# constructor not working with the method I'm trying to use? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Maybe I've misunderstood how constructors work, but in any case, I'm trying to create an array and populate it in the constructor.
I have the following code --
class ClsDeck
{
private string[] deck = new string[52];
private string[] hand = new string[12];
BuildDeck()
{
//lots of code assigning images to each individual element of the "deck" array.
}
//many other methods that need to be called by a form.
}
Visual Studio 2012 insists that the method have a return type. I have simply added "void" to the BuildDeck method, and the error disappeared, but every example I've seen of a constructor has to have the same name as the class, and it has been the only method in the class.
That won't even compile. BuildDeck() has no return type. Constructor names need to match the class name (including case). Replace BuildDeck with ClsDeck().
By definition, a constructor is a method that 1.) has the same name as the class, and 2.) has no return value.
In your example above, "BuildDeck" is not a constructor ... it is a method, and therefore must specify a return type (or "void" if it doesn't return anything).
If you want a constructor, rename "BuildDeck" to "ClsDeck".
The constructor of your class is actually missing.
Make the following changes, and your code will compile:
class ClsDeck
{
private string[] deck = new string[52];
private string[] hand = new string[12];
public ClsDeck()
{
// Place your array initializations here.
}
private void BuildDeck()
{
//lots of code assigning images to each individual element of the "deck" array. }
//many other methods that need to be called by a form.
}
}
That will not work or compile. In order to achieve what you want, you could have a constructor for ClsDeck and call BuildDeck
class ClsDeck {
private string[] deck = new string[52];
private string[] hand = new string[12];
ClsDeck() { //lots of code assigning images to each individual element of the "deck" array. }
//many other methods that need to be called by a form.
BuildDeck();
}
private void BuildDeck() {
//Build your deck
}
}

How to use the thread to show something on the label on the form? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have a method
public static string MyWrite2()
{
string p = "hi";
for (int i = 0; i < 20; i++)
{
p += "kk";
}
return p;
}
and i want to write a thread which when i click on btn1 it call MyWrite2 and put the result on label1, how can i do it?
You can use Invoke() method on a label1 and pass a method which does the writing as an argument to the Invoke() method.
Using anonymous method it would be sth like:
label1.Invoke(new MethodInvoker(delegate
{
label1.Text = MyWrite2();
}
));
In the other thread.

Using lock( ) C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why is this lock not working?
CheckActivity is generated from Mouse/Keyboard Windows hook. Any hint how to make this lock to work?
private void CheckActivity(KeyboardMouseKey k)
{
lock(this)
{
if (_map)
{
_map = false;
if (openFileDialogSelectAudio.ShowDialog() == DialogResult.OK)
MapSound(k, openFileDialogSelectAudio.FileName);
}
else
{
///play
foreach (var m in _mappings.Where(m => m.Key.Equals(k)))
m.Value.Play();
}
UpdateGui();
}
}
You are using lock(this).
If Check Activity is called on different objects, lock(this) will not prevent for making sure that only one thread is executing CheckActivity (which appears to be the purpose of the lock)
Use lock on static object to make it work.
Example:
private static readonly object lockObj = new object();
...
lock(lockObj)
{
....
}

Categories