Use of unassigned local variable "strb" StringBuilder [closed] - c#

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
So I am trying to learn how to use the StringBuilder Class. I read up about it and it seems amazing compared to string !
I am trying to create the StringBuilder in the other button but it keeps throwing me the error:
; expected" and Use of unassigned local variable "strb"
on line 42 & 43.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string nrmlString = "C#";
nrmlString += " This";
nrmlString += " is";
nrmlString += " a";
nrmlString += " Test";
nrmlString += " Thisss";
MessageBox.Show(nrmlString);
}
private void button2_Click(object sender, EventArgs e)
{
StringBuilder strb new StringBuilder("something");
strb.Append("Something else");
MessageBox.Show(strb.ToString());
}
}
}

You missed one = in your button2_Click:
StringBuilder strb = new StringBuilder("something");
strb.Append("Something else");

#VargaDev,
Your error was a simple missing of the = in your code.
StringBuilder strb = new StringBuilder("something");
And it has already been answered. I just chimed in for a suggestion. When trying this type of codes, using VS and creating a form is cumbersome. Have a check at the wonderful (and free) utility LinqPad. You can use that as a code scratch pad. ie: For testing your code above, you would simply do this:
-Choose C# statements (or C# program) from the combo
-Type
StringBuilder strb = new StringBuilder("something");
strb.Append("Something else");
strb.ToString().Dump();
and hit F5. That is it! Dump() is on streoids. You can dump almost anything, it shows the result in a suitable way (a datagrid for example if it is a list like thing).
PS: I don't have any affiliation with the author of the utility (Joseph Albahari), just a lover of it. It is (and he is) worth to be appraised.

Related

Unregistering hotkeys at runtime with MouseKeyHook [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 2 years ago.
Improve this question
I want to allow users to re-assign hotkeys at runtime and am using the Gma.System.MouseKeyHook NuGet package.
Creating new hotkeys at runtime works just fine and dandy but clearing the Action of an already assigned Combination/Sequence (by setting its Action = null) does not remove the original assignment.
Is there some way to do this on a per Combination/Sequence basis? More specifically I want to avoid calling Dispose() on my Hook.GlobalEvents() reference and having to re-initialize the whole systems assignments.
Any help would be greatly appreciated :)
If you want to unsubscribe from keypress event:
_globalHook = Hook.GlobalEvents();
_globalHook.KeyPress += GlobalHookKeyPress; //Subscribe
_globalHook.KeyPress -= GlobalHookKeyPress; //Unsubscribe
Edit:
I understand now that you called OnCombination.
After going over the code of this method, you cannot change the combinations list after you created it. Other calls to OnCombination will just add more registrations.
Edit:
Another option is to use reactive extensions:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reactive.Linq;
using Gma.System.MouseKeyHook;
using MouseKeyHook.Rx;
namespace HotkeyPlay
{
public partial class Form1 : Form
{
private IDisposable _keysObservable;
public Form1()
{
InitializeComponent();
var triggers = new Trigger[]
{
Trigger.On(Keys.H).Alt().Shift()
};
_keysObservable =
Hook
.GlobalEvents()
.KeyDownObservable()
.Matching(triggers)
.Subscribe((trigger) =>
{
Debug.WriteLine(trigger.ToString());
});
}
private void button1_Click(object sender, EventArgs e)
{
_keysObservable.Dispose();
var triggers = new Trigger[]
{
Trigger.On(Keys.B).Alt().Shift()
};
_keysObservable =
Hook
.GlobalEvents()
.KeyDownObservable()
.Matching(triggers)
.Subscribe((trigger) =>
{
Debug.WriteLine(trigger.ToString());
});
}
}
}
Nuget packages:
Install-Package System.Reactive.Linq
Install-Package MouseKeyHook.Rx
MouseKeyHook.Rx is still in pre-release version.

Adding text box to form [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 2 years ago.
Improve this question
I am trying to add a dynamic text box to my windows form with the following code. It's all fine up until the last line:
Form1.Controls.Add(dynamicTextBox);
I'm getting the error:
"An object reference is required for the non-static field, method, or
property Control.Controls."
I'm not sure what I'm doing wrong here and would appreciate any help possible!
//create a text box
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "Text";
dynamicTextBox.Name = "textBox1";
dynamicTextBox.Enabled = false;
dynamicTextBox.Multiline = true;
dynamicTextBox.Height = 80;
dynamicTextBox.Width = 300;
dynamicTextBox.Location = new System.Drawing.Point(234, 279);
Form1.Controls.add(dynamicTextBox);
The following code adds an text box to the form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
TextBox txtBox;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
txtBox = new TextBox();
txtBox.Location = new Point(10, 50);
txtBox.Visible = true;
Controls.Add(txtBox);
}
}
}

Random number will not show up in text box [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 8 years ago.
Improve this question
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RollTheDice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDice_Click(object sender, EventArgs e)
{
int Roll;
Random rand = new Random();
Roll = rand.Next(0,10);
diceDisplay.Text = btnDice.ToString();
}
}
}
Not able to output number to the text field, there is no error it just will not work.
When the button is clicked system. error does show on it but does not show on the build
You are not assigning random number to the TextBox but assigning the Button.ToString:
int Roll;
Random rand = new Random();
Roll = rand.Next(0,10);
diceDisplay.Text = Roll.ToString(); //modified
Also precise your code and replace with just one line:
diceDisplay.Text = new Random().Next(0,10).ToString();
You should use roll.ToString() instead of btnDice.ToString();

C# The name does not exist in the current context [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 9 years ago.
Improve this question
I am trying to learn programming and I am starting with a book called Software Development Fundamentals. However I am having loads of difficulty understanding certain subjects. Especially because my native language is not English. I am stuck at the subject (events) and (delegates). I feel like this is to difficult for me, I can not even get this code to work!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson02
{
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Changed += new EventHandler(r_Changed);
r.Length = 10;
}
static void r_changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine(
"Value Changed: Length = {0}",
r.Length);
}
}
class Rectangle
{
public EventHandler Changed;
private double length;
public double Length
{
get
{
return length;
}
set
{
length = value;
Changed(this, EventArgs.Empty);
}
}
}
}
I get this error:
Error 1 The name 'r_Changed' does not exist in the current context 14 59 Lesson02
C# is case-sensitive language. You have defined function as r_changed and using it as r_Changed
Use
r.Changed += new EventHandler(r_changed);
instead of
r.Changed += new EventHandler(r_Changed);
I'm pretty sure you'd know by now that C# is a case sensitive programming language.
This should work
static void r_Changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine("Value Changed: Length = {0}", r.Length);
}
Notice how r_Changed is capitals (r_changed is what you originally defined)
I would suggest using this because it is easier to read.
There is a little typo mistake in your code. It should be r_Changed instead of r_changed in the your Event Handler.
i.e write
static void r_Changed(object sender, EventArgs e)
in place of
static void r_changed(object sender, EventArgs e)
(Remember C# is Case-sensitive)

if statements in buttons c#

I'm a brand new n00bie in visual c# and I ran into a weird obstacle that is driving me CRAZY!! Here is the code in question (yes, an Hello World program):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals("Goodbye Cruel World"))
{
textBox1.Text = ("Hello World!");
}
else { textBox1.Text = ("Goodye Cruel World"); }
}
}
}
I also tried to use textBox1.Text=="Goodbye Cruel World"; as the evaluation parameter for the if statement getting no errors in the compiler (by the way I am using Visual Studio 2012 Ultimate)
The program runs fine. I initalised the textbox text property as "Hello World!" using the Design GUI of VS. The problem I am facing is that the code works only the first time the user clicks the button. Any time after the button does NOTHING.
I debugged the code and I made sure that the textbox text property is appropriately changed the first time the user clicks the button. When the user clicks the button a second time (or any time after that for that matter) once the code gets to the if statement it skips it, as if the evaluation of the expression within is FALSE. In fact, keeping up with the debug tool, the button keeps executing only the code within the else block, even though I know for a fact that the TextBox.Text property that I am working with has been appropriately changed before.
What am I missing here??? Why doesn't the button just switches the textbox text value between the two strings I hardcoded within?
You are using three strings, not two. "Goodye Cruel World" is not equal to "Goodbye Cruel World". Hence, you cannot expect any kind of "string swapping" behaviour whatsoever from this source code.
Lesson to learn: Do not use the same string at different points of your code. Instead, create a constant string variable which has that value, and then use it every time you need it. For example code see Habib's answer.
That is a case of defining string constant in your code:
public partial class Form1 : Form
{
private const string GOODBYE = "Goodbye Cruel World";
private const string HELLO = "Hello World!";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals(GOODBYE ))
{
textBox1.Text = HELLO;
}
else { textBox1.Text = (GOODBYE ); }
}
}
If you are using same string in multiple places then its better if you define it as a const and use that everywhere in your code, this will help you in reducing errors like the one you have now (Goodye is Goodbye) and it is also easier to change/maintain.
Check the spelling of Goodye in the else clause. The condition will always be false.

Categories