CircuitBoard vBoard = this;
// Find the desired circuit shape
CircuitShape vShape = vBoard.GetComponent(vId);
In the above statement the vBoard is throwing null in certain time. Any idea?
Please help.
Thank you in advance....
more code.. this is a public function
class CircuitBoard :Canvas
{
public void Move(string iBoardId, string iCircuitShapeId, double iXCordinate, double iYCordinate)
{
CircuitBoard vBoard = this;
// secutity check..
if (null != vBoard)
{
string vId = PCBFactory.GetUniqueTag(iCircuitShapeId, vBoard);
// Find the desired circuit shape
CircuitShape vShape = vBoard.GetComponent(vId);
if (vShape != null)
{
// do something...
}
}
}
}
Why are you assigning this to something in the first place? Why not just try:
class CircuitBoard :Canvas
{
public void Move(string iBoardId, string iCircuitShapeId, double iXCordinate, double iYCordinate)
{
string vId = PCBFactory.GetUniqueTag(iCircuitShapeId, vBoard);
CircuitShape vShape = this.GetComponent(vId);
if (vShape != null)
{
// do something...
}
}
}
}
There's no need to define vBoard at all.
Related
I'm new to C# and I can't figure out how to reference the value of a variable from one class to another.
It's set to when the button is pressed, it'll take the text in the text box and sets that as "alphaask". Then it instances "alphaanswer" which would tell the label to change its text.
"alphaanswer" will take the value "alphaQuest" and see if its equal to "bob" which then would change the label.
ALL I want to know how to set the value of "alphaQuest" from the value of "alphaask" so the string can check it with "alphaanswer"
public partial class QuestionTab : Form
{
public string alphaask = "null";
public void button1_Click(object sender, EventArgs e)
{
// alphabutton
// Checks if something is in textbox then says bool is true
bool asked = false;
if(textBoxAlpha.Text != "")
{
alphaask = textBoxAlpha.Text;
asked = true;
}
if(asked==true)
{
// If bool is true than instance auxy
var instance = new Alpha();
instance.alphaanswer();
}
}
}
public class Alpha
{
string alphaQuest = // <-- I want to make alphaQuest equal to alphaask
alphaanswer();
public void alphaanswer()
{
if (alphaanswer == bob)
{
//change text in label1
}
}
}
Do these changes
public partial class QuestionTab : Form
{
public string alphaask = "null";
public void button1_Click(object sender, EventArgs e)
{
bool asked = false;
if(textBoxAlpha.Text != "")
{
alphaask = textBoxAlpha.Text;
asked = true;
}
if(asked==true)
{
// If bool is true than instance auxy
var instance = new Alpha();
instance.alphaAnswer(alphaask);
//Here you are sending the current value of alphaAsk to the alphaanswer method.
}
}
}
public class Alpha
{
public void alphaAnswer(string alphaAnswer) //This string receives the value you sent
{
if (alphaAnswer == "bob")
{
//change text in label1
}
}
}
make a contractor in class Alpha with String parameter
public Alpha(String value)
{
}
then when you call it
var instance = new Alpha(alphaask);
instance.show();
I have problem with my code , I want to replace specific string to a new one but it doesn't work
public void InsertYahoo(TextBox sender)
{
if (IsGmail(sender))
{
ReplaceGmail(sender);
}
else if(IsYahoo(sender))
{
return;
}
else
{
sender.Text +="#yahoo.com";
}
}
public bool IsYahoo(TextBox sender)
{
if (sender.Text.Contains("#yahoo.com")
{
return true;
}
else
{
return false;
}
}
public bool IsGmail(TextBox sender)
{
if (sender.Text.Contains("#gmail.com")
{
return true;
}
else
{
return false;
}
}
public void ReplaceGmail(TextBox sender)
{
sender.Text.Replace("#gmail.com, "#yahoo.com");
}
This code what i tried , so any suggestions?
Also I tried to get the index of #gmail.com and remove it but it did not work neither
Strings are immutable, so every method in the String class does not modify the current instance but returns a new one. You have to assign this to the original variable:
sender.Text = sender.Text.Replace("#gmail.com,"#yahoo.com");
If you are interested in why strings are immutable: Why .NET String is immutable?
Something like this:
//DONE: we should check for null
//DONE: it's Yahoo if it ends on #yahoo.com (not contains)
public static bool IsYahoo(TextBox sender) =>
sender != null &&
sender.Text.TrimEnd().EndsWith("#yahoo.com", StringComparison.OrdinalIgnoreCase);
public static bool IsGmail(TextBox sender) =>
sender != null &&
sender.Text.TrimEnd().EndsWith("#gmail.com", StringComparison.OrdinalIgnoreCase);
public static void InsertYahoo(TextBox sender) {
if (null == sender)
throw new ArgumentNullException(nameof(sender));
if (IsYahoo(sender))
return;
// Uncomment, In case you want to change gmail only
//if (!IsGmail(sender))
// return;
// If we have an eMail like bla-bla-bla#somewhere
int p = sender.Text.LastIndexOf('#');
// ... we change somewhere to yahoo.com
if (p > 0)
sender.Text = sender.Text.Substring(0, p) + "#yahoo.com";
}
Well, my issue here is basically what it says in the title. I'm trying to call my bool value of my Player2 class for my Tic Tac Toe-project we have in school. I think it's worth mentioning that I use "Player Player1, Player2;" in the beginning of Form1.cs to create two instances of my class, Player. I've read multiple posts on the internet about this but all of them are people trying to call in more parameters than that they are providing. I don't see how a bool value of true or false is more than one.
Thanks in advance.
One of my buttons where this problem appears.
public void Btn1_Click(object sender, EventArgs e) >{
{
if (click1 == 0)
{
if (Player2.GetActive(true))//(turn == 0)
{
Btn1.Text = "X";
}
else
{
>Btn1.Text = "O";
}
//turn++;
click1++;
}
else
{
Btn1.Text = Btn1.Text;
}
display();
checkit();
}
}
This is my player class.
` public class Player
{
//Characteristics
string name;
int points;
bool Active;
//Constructor
public Player() { points = 0; Active = true; }
//Methods
public void SetName(string n) { name = n; }
public string GetName() { return name; }
public void SetPoints(int p) { points = p; }
public int GetPoints() { return points; }
public void SetActive(bool a) { Active = a; }
public bool GetActive() { return Active; }`
You have the code:
Player2.GetActive(true)
But you define get active as
public bool GetActive() { return Active; }`
So it is correct you have not defined a GetActive with a parameter.
Here,
if(Player2.GetActive(true))
you are passing an extra argument (true) to the method GetActive. As we can see from the method declaration of GetActive, it takes no parameters:
public bool GetActive() { return Active; }
// ↑
// empty parentheses
I think what you mean here is "if Player2.GetActive() is true..." right? You don't need to specify the value you want if it is true, just doing this is fine:
if (Player2.GetActive())
If you want to check if it is false, add ! before the call to negate the result:
if (!Player2.GetActive())
Like BugFinder said, you are using the method to get the Active-Value instead of using the method to set the value.
So change
Player2.GetActive(true)
to
Player2.SetActive(true)
Just as an addition:
Since we are working with C# here and many of your methods are called set and get, I suggest you change those methods to be properties:
public string Name
{
get { return name; }
set { name = value; }
}
public int Points
{
get { return points; }
set { points = value; }
}
public bool Active
{
get { return active; }
set { active = value; }
}
Now when you want to set the value, you can type
Player2.IsActive = true;
and to check the value simple type code like
If(Player2.IsActive)
//Do Stuff
I've made a document class that downloads and reads the text in it. The smart thing is that it only downloads and reads the text in the document when or if it's needed. By using the Text property it'll try to read the document, if it hasn't been downloaded it'll download it then read it.
It's very nice. However I've noticed my use of Exceptions leads to some funky code. See below.
Document class
public delegate byte[] DownloadBinaryDelegate(IDocument document);
public delegate string TextReaderDelegate(IDocument document);
public class Document
{
public DownloadBinaryDelegate DownloadBinaryDelegate { private get; set; }
public TextReaderDelegate TextReaderDelegate { private get; set; }
private bool _binaryIsSet;
private byte[] _binary;
public byte[] Binary
{
get
{
if (_binaryIsSet)
return _binary;
if (DownloadBinaryDelegate == null)
throw new NullReferenceException("No delegate attached to DownloadBinaryDelegate.");
Binary = DownloadBinaryDelegate(this);
DownloadBinaryDelegate = null; // unhock delegate as it's no longer needed.
return _binary;
}
set
{
if (_binaryIsSet)
return;
_binary = value;
_binaryIsSet = true;
}
}
private bool _textIsSet;
private string _text;
public string Text
{
get
{
if (_textIsSet)
return _text;
if (TextReaderDelegate == null)
throw new NullReferenceException("No delegate attached to TextReaderDelegate.");
Text = TextReaderDelegate(this); // this delegate will call Binary and return the translated text.
TextReaderDelegate = null; // unhock delegate as it's no longer needed.
return _text;
}
set
{
if (_textIsSet)
return;
_text = value;
_textIsSet = true;
}
}
The Problem
What I wrote in the first place.
if (document.Text == null) // text is not set
{
if (document.Binary == null) // binary has not been downloaded
document.DownloadBinaryDelegate = Util.DownloadDocument;
document.TextReaderDelegate = Util.ReadDocument;
}
Totally forgetting that the Text property throws an exception.
So I have to write something like this, which is a bit funky code.
// check if text has already been read and set
try
{
var isTextSet = document.Text == null;
}
catch (NullReferenceException)
{
document.DownloadBinaryDelegate = Util.DownloadDocument;
document.TextReaderDelegate = Util.ReadDocument;
}
I hope you can see what I mean.
So my question, is this a bad design? How would you have done it? Keep in mind I would still like the current functionality.
Lazy initialization is already baked into the .NET framework. I would suggest re-implementing your class using Lazy<T>.
To answer your specific question, it sounds like your class will always require the Binary and Text delegates, so I would make them required parameters to the constructor.
I was unable to use Lazy as I'm using delegate(this) which is not allowed.
So I ended up using the fine answer from here: What exception type to use when a property cannot be null?
public class Document
{
private DownloadBinaryDelegate _downloadBinaryDelegate;
public void SetDownloadBinaryDelegate(DownloadBinaryDelegate downloadBinary)
{
if (downloadBinary == null)
throw new ArgumentNullException("downloadBinary");
_downloadBinaryDelegate = downloadBinary;
}
private TextReaderDelegate _textReaderDelegate;
public void SetTextReaderDelegate(TextReaderDelegate readerDelegate)
{
if (readerDelegate == null)
throw new ArgumentNullException("readerDelegate");
_textReaderDelegate = readerDelegate;
}
private bool _binaryIsSet;
private byte[] _bytes;
public void SetBinary(byte[] bytes, bool forceOverwrite = false)
{
if (_binaryIsSet && !forceOverwrite)
return;
_bytes = bytes;
_binaryIsSet = true;
}
public byte[] GetBinary()
{
if (_binaryIsSet)
return _bytes;
if (_downloadBinaryDelegate == null)
throw new InvalidOperationException("No delegate attached to DownloadBinaryDelegate. Use SetDownloadBinaryDelegate.");
SetBinary(_downloadBinaryDelegate(this));
_downloadBinaryDelegate = null; // unhock delegate as it's no longer needed.
return _bytes;
}
public bool TryGetBinary(out byte[] bytes)
{
if (_binaryIsSet)
{
bytes = _bytes;
return true;
}
if (_downloadBinaryDelegate != null)
{
bytes = GetBinary(); // is this legit?
return true;
}
bytes = null;
return false;
}
private bool _textIsSet;
private string _text;
public void SetText(string text, bool forceOverwrite = false)
{
if (_textIsSet && !forceOverwrite)
return;
_text = text;
_textIsSet = true;
}
public string GetText()
{
if (_textIsSet)
return _text;
if (_textReaderDelegate == null)
throw new InvalidOperationException("No delegate attached to TextReaderDelegate. Use SetTextReaderDelegate.");
SetText(_textReaderDelegate(this)); // this delegate will call Binary and return the read text.
_textReaderDelegate = null; // unhock delegate as it's no longer needed.
return _text;
}
public bool TryGetText(out string text)
{
byte[] bytes;
if (!TryGetBinary(out bytes))
{
text = null;
return false;
}
if (_textIsSet)
{
text = _text;
return true;
}
if (_textReaderDelegate != null)
{
text = GetText(); // is this legit?
return true;
}
text = null;
return false;
}
}
Can anybody help me with this?:
I'm trying call a method from a my class "numbers" to show that if the entered number is over 50, on button click a message box shows displaying "high" but if it's below 50 it displays "low".
I can't figure out what i'm doing wrong here.
This is the code from my class:
private int number;
private string getNumber(int num)
{
number = num;
return number.ToString();
}
public int numProperty
{
get { return number; }
set { number = value; }
}
public void isHighorlow()
{
if (number >=50)
{
}
else
{
return;
}
}
Note: the int "number" is property that gets it's value from a text box too.
& here is the code from my form:
numbers info = new numbers();
private void Btn_Click(object sender, EventArgs e)
{
info.numProperty = Convert.ToInt32(numberBOX.Text);
info.isHighorlow = Messagebox.Show = ("High");
}
I know that I've not added the "low" bit yet because i'm still trying to see how this works. Sorry if it seems confusing as i'm still learning c#.
I get the error message: cannot assign isHighorlow because it's part of a method group.
And I also realise it's much easier if I just do an if statement on the textbox, but I'm practising Classes and Methods so I'm trying to do it this way.
thanks.
I'm guessing you want something like this:
public string isHighorlow(int number)
{
if (number >=50)
{
return "High";
}
else
{
return "Low";
}
}
numbers info = new numbers();
private void Btn_Click(object sender, EventArgs e)
{
Messagebox.Show(info.isHighorlow(Convert.ToInt32(numberBOX.Text)))
}
IsHighOrLow should be as follows
public bool isHighorlow()
{
if (number >=50)
{
return true;
}
else
{
return false;
}
}
And in button click
if (info.isHighorlow){
//say high
} else
{
// say low
}
isHighorLow is a method, not a property.
MessageBox.Show is a method.
Not sure what you are trying to do but it should be :
if(info.isHigh(Convert.ToInt32(numberBox.Text)))
Messagebox.Show("High");
else
Messagebox.Show("Low");
Meaning you have a method isHigh like so:
public bool isHigh()
{
return number>=50
}
(disclaimer: double check boolean and associated constants per C#)
isHighOrLow doesn't do anything at all. Perhaps this would be better:
public boolean isHigh()
{
if (number >=50)
{
return true;
}
else
{
return false;
}
}
Or, more concisely:
public boolean isHigh()
{
return number >=50;
}
When you call it, this might be closer to what you need:
numbers info = new numbers();
private void Btn_Click(object sender, EventArgs e)
{
info.numProperty = Convert.ToInt32(numberBOX.Text);
if (info.isHigh())
{
Messagebox.Show("High");
}
else
{
Messagebox.Show("Low");
}
}
In your class you have defined void isHighorlow().
This means that you have a method that returns nothing.
Of course such method cannot be used on the left part of an expression like you have done.
Probably you want to write in your class
public bool isHighorlow()
{
if (number >=50)
{
return true;
}
else
{
return false;
}
}
in this way you declare a method that return True if the internal value is >= 50 or false otherwise.
Now, in your form you can use the method in this way
Messagebox.Show(info.isHighorlow() ? "High" : "Low");
However, if the requirement is simply to return a flag for true or false, it is better to use a read only property changing the class code in this way
public bool isHighorlow()
{
get
{
return (number >=50 ? true : false);
}
// No set, read only
}
Try to change your code this way:
private int _number;
private string GetNumber(int number)
{
_number = number;
return number .ToString();
}
public int Number
{
get { return _number; }
set { _number = value; }
}
public string IsHigh()
{
get { if (number >= 50) return true; }
}
numbers info = new numbers();
private void Btn_Click(object sender, EventArgs e)
{
info.Number = Convert.ToInt32(numberBOX.Text);
MessageBox.Show(info.IsHigh ? "High" : "Low");
}