|
Now that you've completely changed your requirement, it's time you started reading the documentation on these methods and started experimenting on your own on how they work.
You're trying to learn by constantly asking questions that are easy to find the answers to and copying and pasting code you find on the internet. You're not making an effort to understand the code you're using. You have to start creating small programs that test the methods you're trying to use so you can see how they work. You also have to get to know how to use the debugger. It's there to debug YOU and better you're understanding of how your code works.
|
|
|
|
|
I think yours is an excellent example of "agile programming"; i.e. no planning, just "coding".
It is obvious the leading and trailing "[]" can be eliminated.
We then wind up with a "coma-delimited" file record; which can be split using string split (6 tokens).
Then it's just a question of testing the first token for an "?".
But, hey, as long as we come up with the "right answer", then it's all good. Right?
|
|
|
|
|
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
this.trenutneKontrole[0].Image = button.Image;
this.pokusaj[0] = (int)button.Tag;
}
break;
case 2:
{
this.trenutneKontrole[1].Image = button.Image;
this.pokusaj[1] = (int)button.Tag;
}
break;
case 3:
{
this.trenutneKontrole[2].Image = button.Image;
this.pokusaj[2] = (int)button.Tag;
}
break;
case 4:
{
this.trenutneKontrole[3].Image = button.Image;
this.pokusaj[3] = (int)button.Tag;
}
break;
This is full code:
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;
using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;
private PictureBox pictureBox6;
private PictureBox pictureBox5;
private PictureBox pictureBox4;
private PictureBox pictureBox3;
private PictureBox pictureBox2;
private PictureBox pictureBox1;
private GroupBox groupBox8;
private PictureBox pictureBox55;
private PictureBox pictureBox56;
private PictureBox pictureBox57;
private PictureBox pictureBox58;
private GroupBox groupBox9;
private Label label1;
private MenuStrip menuStrip1;
private ToolStripMenuItem igraToolStripMenuItem;
private GroupBox groupBox2;
private PictureBox pictureBox47;
private PictureBox pictureBox48;
private PictureBox pictureBox49;
private PictureBox pictureBox50;
private PictureBox pictureBox51;
private PictureBox pictureBox52;
private PictureBox pictureBox53;
private PictureBox pictureBox54;
private PictureBox pictureBox39;
private PictureBox pictureBox40;
private PictureBox pictureBox41;
private PictureBox pictureBox42;
private PictureBox pictureBox43;
private PictureBox pictureBox44;
private PictureBox pictureBox45;
private PictureBox pictureBox46;
private PictureBox pictureBox31;
private PictureBox pictureBox32;
private PictureBox pictureBox33;
private PictureBox pictureBox34;
private PictureBox pictureBox35;
private PictureBox pictureBox36;
private PictureBox pictureBox37;
private PictureBox pictureBox38;
private PictureBox pictureBox23;
private PictureBox pictureBox24;
private PictureBox pictureBox25;
private PictureBox pictureBox26;
private PictureBox pictureBox27;
private PictureBox pictureBox28;
private PictureBox pictureBox29;
private PictureBox pictureBox30;
private PictureBox pictureBox15;
private PictureBox pictureBox16;
private PictureBox pictureBox17;
private PictureBox pictureBox18;
private PictureBox pictureBox19;
private PictureBox pictureBox20;
private PictureBox pictureBox21;
private PictureBox pictureBox22;
private PictureBox pictureBox11;
private PictureBox pictureBox12;
private PictureBox pictureBox13;
private PictureBox pictureBox14;
private PictureBox pictureBox10;
private PictureBox pictureBox9;
private PictureBox pictureBox8;
private PictureBox pictureBox7;
private ToolStripMenuItem prikaziTabeluToolStripMenuItem;
private ToolStripMenuItem infoToolStripMenuItem;
private ToolStripMenuItem vratiPotezToolStripMenuItem;
private int brojKlikova = 0;
private int brojPoena = 0;
private bool pocetakIgre = true;
private bool igraZavrsena = false;
private int[] pokusaj;
private SkockoClass skocko;
public PictureBox[] trenutneKontrole;
public PictureBox[] rezultatPokusajaKontrole;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
public int rbrPokusaja;
public Skocko()
{
InitializeComponent();
int[] array = new int[4];
this.pokusaj = array;
this.skocko= null;
this.trenutneKontrole = null;
this.rezultatPokusajaKontrole = null;
this.rbrPokusaja = 1;
this.InitializeComponent();
this.pictureBox1.Tag = 0;
this.pictureBox2.Tag = 1;
this.pictureBox3.Tag = 2;
this.pictureBox4.Tag = 3;
this.pictureBox5.Tag = 4;
this.pictureBox6.Tag = 5;
this.button1.Tag = 0;
this.button2.Tag = 1;
this.button3.Tag = 2;
this.button4.Tag = 3;
this.button5.Tag = 4;
this.button6.Tag = 5;
}
private void SkloniSlike()
{
for (int i = 7; i < 59; i++)
{
Control[] array = base.Controls.Find("pictureBox" + i.ToString(), true);
PictureBox pictureBox = array[0] as PictureBox;
pictureBox.Image = null;
Application.DoEvents();
Thread.Sleep(3);
}
}
private void PrikaziBodove()
{
this.label1.Text = this.brojPoena.ToString();
}
private void PrikaziRezultatPokusaja(PictureBox[] resPicBoxs, Bitmap[] resPictures)
{
for (int i = 0; i < resPicBoxs.Length; i++)
{
resPicBoxs[i].Image = resPictures[i];
}
}
private void igraToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!this.pocetakIgre && !this.igraZavrsena)
{
if (MessageBox.Show("Da li ste sigurni da zelite da pocnete novu partuju?\nBodovi ce biti smanjeni za 10 poena", "Info", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
this.brojPoena -= 10;
this.label1.Text = this.brojPoena.ToString();
}
this.skocko= new SkockoClass();
this.brojKlikova = 0;
this.rbrPokusaja = 1;
if (!this.pocetakIgre)
{
this.SkloniSlike();
}
else
{
this.pocetakIgre = false;
}
this.igraZavrsena = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void prikaziTabeluToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void infoToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Z)
{
if (this.brojKlikova > 0 && this.brojKlikova < 4)
{
this.brojKlikova--;
this.trenutneKontrole[this.brojKlikova].Image = null;
}
}
}
private void vratiPotezToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.brojKlikova > 0 && this.brojKlikova < 4)
{
this.brojKlikova--;
this.trenutneKontrole[this.brojKlikova].Image = null;
}
}
private void Command_Click(object sender, EventArgs e)
{
Button button = sender as Button;
PictureBox pictureBox = sender as PictureBox;
this.brojKlikova++;
switch (this.rbrPokusaja)
{
case 1:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox7,
this.pictureBox8,
this.pictureBox9,
this.pictureBox10
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox11,
this.pictureBox12,
this.pictureBox13,
this.pictureBox14
};
break;
case 2:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox15,
this.pictureBox16,
this.pictureBox17,
this.pictureBox18
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox19,
this.pictureBox20,
this.pictureBox21,
this.pictureBox22
};
break;
case 3:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox23,
this.pictureBox24,
this.pictureBox25,
this.pictureBox26
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox27,
this.pictureBox28,
this.pictureBox29,
this.pictureBox30
};
break;
case 4:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox31,
this.pictureBox32,
this.pictureBox33,
this.pictureBox34
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox35,
this.pictureBox36,
this.pictureBox37,
this.pictureBox38
};
break;
case 5:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox39,
this.pictureBox40,
this.pictureBox41,
this.pictureBox42
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox43,
this.pictureBox44,
this.pictureBox45,
this.pictureBox46
};
break;
case 6:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox47,
this.pictureBox48,
this.pictureBox49,
this.pictureBox50
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox51,
this.pictureBox52,
this.pictureBox53,
this.pictureBox54
};
break;
}
switch (this.brojKlikova)
{
case 1:
{
this.trenutneKontrole[0].Image = button.Image;
this.pokusaj[0] = (int)button.Tag;
}
break;
case 2:
{
this.trenutneKontrole[1].Image = button.Image;
this.pokusaj[1] = (int)button.Tag;
}
break;
case 3:
{
this.trenutneKontrole[2].Image = button.Image;
this.pokusaj[2] = (int)button.Tag;
}
break;
case 4:
{
this.trenutneKontrole[3].Image = button.Image;
this.pokusaj[3] = (int)button.Tag;
}
break;
}
if (this.brojKlikova == 4)
{
this.brojKlikova = 0;
this.PrikaziRezultatPokusaja(this.rezultatPokusajaKontrole, this.skocko.ProveriPokusaj(this.pokusaj));
if (this.skocko.reseno)
{
switch (this.rbrPokusaja)
{
case 1:
this.brojPoena += 100;
break;
case 2:
this.brojPoena += 90;
break;
case 3:
this.brojPoena += 80;
break;
case 4:
this.brojPoena += 70;
break;
case 5:
this.brojPoena += 60;
break;
case 6:
this.brojPoena += 50;
break;
}
this.rbrPokusaja = 1;
this.igraZavrsena = true;
this.PrikaziBodove();
this.PrikaziResenje();
}
else if (this.rbrPokusaja == 6)
{
this.igraZavrsena = true;
this.rbrPokusaja = 1;
this.brojPoena -= 10;
this.PrikaziBodove();
this.PrikaziResenje();
}
else
{
this.rbrPokusaja++;
}
}
}
private void PrikaziResenje()
{
this.PrikaziRezultatPokusaja(new PictureBox[]
{
this.pictureBox55,
this.pictureBox56,
this.pictureBox57,
this.pictureBox58
}, this.skocko.DajKombinacijuKaoNizSlika());
}
private void pictureBox47_Click(object sender, EventArgs e)
{
if (this.brojKlikova > 0 && this.brojKlikova < 4)
{
this.brojKlikova--;
this.trenutneKontrole[this.brojKlikova].Image = null;
}
}
}
}
Other class with code:
using Skocko5.Properties;
using System;
using System.Drawing;
namespace Skocko5
{
public class Igra
{
private int[] kombinacija;
public bool reseno;
public Igra()
{
int[] array = new int[4];
this.kombinacija = array;
this.reseno = false;
Random random = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 4; i++)
{
this.kombinacija[i] = random.Next(0, 6);
}
}
public int[] DajKombinacijuKaoNizInt()
{
return this.kombinacija;
}
public Bitmap[] DajKombinacijuKaoNizSlika()
{
Bitmap[] array = new Bitmap[4];
for (int i = 0; i < this.kombinacija.Length; i++)
{
switch (this.kombinacija[i])
{
case 0:
array[i] = new Bitmap(Resources.herc);
break;
case 1:
array[i] = new Bitmap(Resources.pik);
break;
case 2:
array[i] = new Bitmap(Resources.karo);
break;
case 3:
array[i] = new Bitmap(Resources.tref);
break;
case 4:
array[i] = new Bitmap(Resources.skocko);
break;
case 5:
array[i] = new Bitmap(Resources.zvezda);
break;
}
}
return array;
}
public Bitmap[] ProveriPokusaj(int[] pokusaj)
{
int num = 0;
int num2 = 0;
int[] array = (int[])this.kombinacija.Clone();
for (int i = 0; i < pokusaj.Length; i++)
{
for (int j = 0; j < array.Length; j++)
{
if (pokusaj[i] == array[j])
{
num++;
array[j] = -1;
break;
}
}
}
for (int i = 0; i < this.kombinacija.Length; i++)
{
if (pokusaj[i] == this.kombinacija[i])
{
num2++;
}
}
int num3 = num - num2;
Bitmap[] array2 = new Bitmap[4];
for (int i = 0; i < num2; i++)
{
array2[i] = new Bitmap(Resources.pogodakPravoMesto);
}
for (int i = num2; i < num2 + num3; i++)
{
array2[i] = new Bitmap(Resources.pogodakPogresnoMesto);
}
for (int i = num2 + num3; i < this.kombinacija.Length; i++)
{
array2[i] = new Bitmap(Resources.promasaj);
}
if (num2 == 4)
{
this.reseno = true;
}
return array2;
}
private void PostaviPrvoPojavljivanjeBrojaNaMinusJedan(int broj, ref int[] niz)
{
for (int i = 0; i < niz.Length; i++)
{
if (niz[i] == broj)
{
niz[i] = -1;
break;
}
}
}
}
}
|
|
|
|
|
|
That didn't help me!!!!!!
|
|
|
|
|
You cannot cast an Object to an int ; it makes no sense. You really need to understand the basics of C#, before trying a project such as this.
modified 23-Dec-16 4:55am.
|
|
|
|
|
You can if it's a boxed int though, and some ints were assigned to tags so it looks like that was the intention
|
|
|
|
|
The Tag property returns an Object , so it can (as you imply) be cast to an Intxx type, but not to a simple int .
modified 23-Dec-16 4:56am.
|
|
|
|
|
int is the same type as Int32.
This works[^]:
object obj = 1;
int x = (int)obj;
|
|
|
|
|
My bad; I should always run my own tests rather than accepting what OPs post.
|
|
|
|
|
I'm beginning to suspect that nothing can actually help you...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Please, start looking at your code, using the debugger, and thinking: your questions are showing increasing evidence that you are not doing any of these things.
If the error is in this line:
this.pokusaj[0] = (int)button.Tag;
Then it obvious that whatever is stored in the Tag property of the button control is not an integer.
What is it? We can't tell.
Why isn't it an integer? Again, we can't tell - because we can't run your code and see what is going on.
So for the unpteenth time: USE THE DEBUGGER and look at what is happening. It's the only way you are going to find out.
Personally? I'd guess the Tag is null - which can't be cast to any value type.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
You cannot convert an object to int. Do some more research before asking for help
|
|
|
|
|
You can though, so right back at you.
|
|
|
|
|
Depends on what the object contains. If you have a method:
private void MyMethod(object o)
{
int i = (int) o;
...
} And you use it thus:
int x = 666;
MyMethod(x); It will work fine.
If you do this:
MyMethod(null); Or
MyMethod(new Button()); Then it will fail.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
There are a couple of things I notice about your code. The first thing I see is that you use a switch statement unnecessarily. As each array item you drop into is 1 away from the value in brojKlikova, you can use a simple subtraction to allocate the correct value.
The second issue you have is that you have no idea what's in Tag. It may, or may not, be an int. If I were you, I would use something that is suited to test and convert a value into an int. Specifically, I would use int.TryParse to see if I could actually convert from whatever is in Tag to an int.
int tagValue;
this.trenutneKontrole[brojKlikova-1] = button.Image;
if (!int.TryParse(button.Tag, out tagValue))
{
pokusaj[brojKlikova-1] = tagValue;
}
else
{
System.Diagnostics.Debug.WriteLine("The value in the Tag for {0} is not an integer", brojKlikova);
} Run the application and watch the Output window to see which one of your Tag values triggers this. It's that simple.
This space for rent
|
|
|
|
|
It's like you're running your "code" through an obfuscator before posting it here.
|
|
|
|
|
Take a look below at some of OP's other posts.
|
|
|
|
|
I'm following a tutorial for Web API Jwt Tokens but am confused on what exactly this below code is doing. When I read tutorials, I like to take the time to understand the content rather than just sorta copy/paste blow through them. If anyone is familiar with this type of code, please give me a little walkthrough. I'll go ahead and narrate how I feel this is working below to start it off:
public string Protect(AuthenticationTicket data)
public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket>
{
private readonly string _issuer = string.Empty;
public CustomJwtFormat(string issuer)
{
_issuer = issuer;
}
public string Protect(AuthenticationTicket data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];
string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);
var signingKey = new HmacSigningCredentials(keyByteArray);
var issued = data.Properties.IssuedUtc;
var expires = data.Properties.ExpiresUtc;
var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
var handler = new JwtSecurityTokenHandler();
var jwt = handler.WriteToken(token);
return jwt;
}
The code appears to:
1. Accept user claims as an argument called "data" (?)
2. After ensuring the data isn't null, it pulls an "AudienceId" and "AudienceSecret" from AppSettings in web.config and assigns to two variables.
3. It decodes the AudienceSecret from Base64Url into a byte array? <----This is where I'm confused. The secret is just a URL???
4. It now takes the decoded URL and then passes it into a hash function, creating a keyed-hash message authentication code "signing credentials" (also confused a bit here)
5. Assigns issued and expiry date to the claims/data.
6. It then creates a token with the above data
7.News up a "token handler" and then creates yet another jwt token variable and finally apparently receives the token from handler.WriteToken(token)
This is my best guess but there are so many types thrown around here, this code is actually quite difficult to understand, especially coming back to OOP from C; it seems very convoluted with big words and not a ton of meaning.
Any additional info or tips would be appreciated, especially about the "audience." I don't get the relationship between the URL/Base64encoded/string/key thing Thank you!
modified 21-Dec-16 22:10pm.
|
|
|
|
|
The trouble is the developer was too lazy to use proper variable typing, so all those var keywords just make it more difficult to understand. One of the worst decisions in C# was that keyword IMHO.
|
|
|
|
|
I agree with you. It's pretty clear to me that, just like in many other areas/trades, there are many "features" included in modern programming languages which are to be blunt, for lazy people. Or at least abused by them. The thing that always baffles my mind is people do everything they can to try and type less, yet I've never met one programmer whose problem is that they spend too much time typing. In fact, I wished I spent more time typing and less time dealing with bs!
|
|
|
|
|
Go over to QA, and look at the amount of effort some students will put into avoiding learning how to write code: they will invest hours in trying to get you to write a five line console app...
These are the people that want var to be usable outside Linq queries.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
But doesn't everyone want to be a "project manager" ... and skip all that "techie" stuff?
|
|
|
|
|
TheOnlyRealTodd wrote: 3. It decodes the AudienceSecret from Base64Url into a byte array? <----This is where I'm confused. The secret is just a URL???
The secret is a Base64[^]-encoded byte array.
The TextEncodings.Base64Url.Decode method uses Convert.FromBase64String[^]. It replaces some characters that can't be used in a URL, and pads the string to the correct length.
It's not clear why you'd need to do that, since you're not passing the string in a URL.
public class Base64UrlTextEncoder : ITextEncoder
{
public string Encode(byte[] data)
{
if (data == null) throw new ArgumentNullException("data");
return Convert.ToBase64String(data).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
public byte[] Decode(string text)
{
if (text == null) throw new ArgumentNullException("text");
return Convert.FromBase64String(Pad(text.Replace('-', '+').Replace('_', '/')));
}
private static string Pad(string text)
{
int count = 3 - (text.Length + 3) % 4;
if (count == 0) return text;
return text + new string('=', count);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm trying to do an if record exists update statement else insert statement and can't get past the line:
OracleDataReader reader = check_RID.ExecuteReader();
Keep getting ora-00096 missing expression error:
OracleConnection con = new OracleConnection(strConnection);
con.Open();
OracleCommand check_RID = new OracleCommand("SELECT COUNT(*) FROM CONTRACT_INFO WHERE (rid = @rid)", con);
check_RID.Parameters.Add("@rid", labelRID.Text);
OracleDataReader reader = check_RID.ExecuteReader();
if (reader.HasRows)
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = strConnection;
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "UPDATE...
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("primecontractor.aspx?Id=" + labelRID.Text);
}
else
{
OracleConnection conn = new OracleConnection(); // C#
conn.ConnectionString = strConnection;
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT....
......
cmd.ExecuteNonQuery();
conn.Close();
|
|
|
|
|