Reputation: 396
I'm making a blackjack project..
I've firstly created the Hit
class for the Hit
Button.
So on the form, when I click it, it will retrieve a value from the switch case scenario that has randomized a number/value within the hit class.
I've tried making a variable just under the Class hit to = stRefID within the GenerateID
method but this failed as well; everything is public, so I'm baffled.
Thank you for your time & assisting me with this, as it's my first time actually posting here, but I use this place all the time.
Hit.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Blackjack
{
public class Hit
{
public static string GenerateID(int MinSize, int MaxSize)
{
string stRefID = "";
Random random = new Random();
int iChosenMaxSize = random.Next(0, 11);
int two = 2;
int three = 3;
int four = 4;
int five = 5;
int six = 6;
int seven = 7;
int eight = 8;
int nine = 9;
int ten = 10;
int jack = 10;
int queen = 10;
int king = 10;
int ace = 11;
for (int x = 1; x <= iChosenMaxSize; x++)
{
int iCharType = random.Next(0, 12);
switch (iCharType)
{
case 0:
stRefID += two;
break;
case 1:
stRefID += three;
break;
case 2:
stRefID += four;
break;
case 3:
stRefID += five;
break;
case 4:
stRefID += six;
break;
case 5:
stRefID += seven;
break;
case 6:
stRefID += eight;
break;
case 7:
stRefID += nine;
break;
case 8:
stRefID += ten;
break;
case 9:
stRefID += ace;
break;
case 10:
stRefID += jack;
break;
case 11:
stRefID += queen;
break;
case 12:
stRefID += king;
break;
}
} return stRefID;
}
}
}
And where I would like to retrieve and set the text box to the value retrieved:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Blackjack
{
public partial class Form1 : Form
{
private void hit_Click(object sender, EventArgs e)
{
this.test.Text = Hit.stRefID;
}
I've attempted making the button public as well but this failed...
test text box for output:
private void test_TextChanged(object sender, EventArgs e)
{
}
Kind regards, Mike.
Revised Code:
namespace Blackjack
{
public class Hit
{
public static string GenerateID(string stRefID)
{
Random random = new Random();
int iChosenMaxSize = random.Next(0, 11);
int two = 2;
int three = 3;
int four = 4;
int five = 5;
int six = 6;
int seven = 7;
int eight = 8;
int nine = 9;
int ten = 10;
int jack = 10;
int queen = 10;
int king = 10;
int ace = 11;
for (int x = 1; x <= iChosenMaxSize; x++)
{
int iCharType = random.Next(0, 12);
switch (iCharType)
{
case 0:
stRefID += two;
break;
case 1:
stRefID += three;
break;
case 2:
stRefID += four;
break;
case 3:
stRefID += five;
break;
case 4:
stRefID += six;
break;
case 5:
stRefID += seven;
break;
case 6:
stRefID += eight;
break;
case 7:
stRefID += nine;
break;
case 8:
stRefID += ten;
break;
case 9:
stRefID += ace;
break;
case 10:
stRefID += jack;
break;
case 11:
stRefID += queen;
break;
case 12:
stRefID += king;
break;
}
} return stRefID;
}
}
}
I made the generateID method with stRefID because the return should go back to it - whereby I can call the value from the button? the public static string declaration works but only if I have a defaul value...
Upvotes: 0
Views: 5578
Reputation: 2597
Not sure if I understood completely.
But I could see that stRefID
is a variable local
to GenerateID
function.
If you need to access it as Hit.stRefID
then stRefID
should be a static class member
Try adding stRefID
as static
global Class
variable.
public class Hit
{
public static string stRefID = "";
public static string GenerateID(int MinSize, int MaxSize)
{
//Your logic
}
Upvotes: 4
Reputation: 939
what u have done is declared the stRefID
variable inside the scope of the method GenerateID
this is not gonna work so what u can do is put that variable within the scope of the class and then u can use it like this.
namespace Blackjack
{
public class Hit
{
string stRefID = "";
public static string GenerateID(int MinSize, int MaxSize)
{
Random random = new Random();
int iChosenMaxSize = random.Next(0, 11);
int two = 2;
int three = 3;
int four = 4;
int five = 5;
int six = 6;
int seven = 7;
int eight = 8;
int nine = 9;
int ten = 10;
int jack = 10;
int queen = 10;
int king = 10;
int ace = 11;
for (int x = 1; x <= iChosenMaxSize; x++)
{
int iCharType = random.Next(0, 12);
switch (iCharType)
{
case 0:
stRefID += two;
break;
case 1:
stRefID += three;
break;
case 2:
stRefID += four;
break;
case 3:
stRefID += five;
break;
case 4:
stRefID += six;
break;
case 5:
stRefID += seven;
break;
case 6:
stRefID += eight;
break;
case 7:
stRefID += nine;
break;
case 8:
stRefID += ten;
break;
case 9:
stRefID += ace;
break;
case 10:
stRefID += jack;
break;
case 11:
stRefID += queen;
break;
case 12:
stRefID += king;
break;
}
} return stRefID;
}
this should work fine i guess
Upvotes: -1
Reputation: 4923
You cannot access a variable defined inside a method. stRefID's scope is strictly inside GenerateID. Based on your class, if you want to access the value of stRefID, you need to call Hit.GenerateID
, this will return the value of stRefID. Scope of variables is very important in C# (as with all programming languages). I suggest you take a look at C# Variable Scope
Upvotes: 2