CardsNumber = myObject.HandleString(Cards);
if i have a method like this all i want is
CardsNumber to change not Cards
myObject.HandleString(Cards);
change too i need it to be the same? why does it change?
if i specify
CardsNumber = myObject.HandleString(Cards);
should'nt that only change?

i hope you understand what i mean thank you for answers!

If myObject.HandleString(Cards); has a return value, CardsNumber = myObject.HandleString(Cards); will set CardsNumber to be that value.

Cards would be modified if it is a form level variable and the method makes changes to the Cards variable within it.

Can we see the method code and that might help find the change that you dont want.

public string[] HandleString(string[] str)
        {
            for (int a = 0; a <= 51; a++)
            {
                str[a] = str[a].Replace("   Hjärter ", "");
                str[a] = str[a].Replace("   Ruter ", "");
                str[a] = str[a].Replace("   Spader ", "");
                str[a] = str[a].Replace("   Klöver ", "");
            }
            return str;
        }

this is the code and i use two strings[52]
but when i run the code both strings change when i only want one to change

using System.Text;
using System.Windows.Forms;
using AdkNetWrapper;

namespace Visma_projekt
{
    public partial class Form1 : Form
    {
        BlackJack myObject = new BlackJack();
        public string[] Cards = new string[52];
        public Form1()
        {
            InitializeComponent();

        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            int[] intCards = new int[52];
            string[] CardsNumber = new string[52];                  
            //PlayerWindow.Text += Cards[0] + Cards[1];
            Cards = myObject.CreateCards(Cards);                    
            Cards = myObject.ShuffleCards(Cards); 



            CardsNumber = Cards;
            CardsNumber = myObject.HandleString(CardsNumber);  




            intCards = myObject.ConvertStringToInt(CardsNumber);    

            int intPlayerScore = intCards[0] + intCards[1];
            string stringPlayerScore = intPlayerScore.ToString();
            PlayerWindow.Text = " ";
            PlayerWindow.Text += Cards[6] + Cards[13];              
            PlayerWindow.Text += CardsNumber[6] + CardsNumber[13];

            Playerscore.Text = "You got " + stringPlayerScore + "!  hit it or stop?";
            DealerWindow.Text = " ";
            DealerWindow.Text += Cards[2];
            //for (int a = 0; a <= 51; a++)
            //{
            //    PlayerWindow.Text += CardsNumber[a];
            //}
        }
        private void btnStop_Click(object sender, EventArgs e)
        {

        }
        private void btnHitIt_Click(object sender, EventArgs e)
        {

        }
        private void PlayerWindow_TextChanged(object sender, EventArgs e)
        {

        }
        private void DealerWindow_TextChanged(object sender, EventArgs e)
        {

        }
        private void Playerscore_TextChanged(object sender, EventArgs e)
        {

        }
        private void DealerScore_TextChanged(object sender, EventArgs e)
        {

        }
        private void Playerbox_TextChanged(object sender, EventArgs e)
        {

        }
        private void Dealerbox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

From1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Visma_projekt
{
    public class BlackJack
    {
        public string[] CreateCards( string[] str)
        {
            for (int a = 0; a <= 51; a++)
            {
                if (a <= 12)
                {
                    str[a] = "   Hjärter " + (a+1).ToString();
                }
                if (a > 12 && a <= 25)
                {
                    str[a] = "   Ruter " + (a-12).ToString();
                }
                if (a > 25 && a <= 38)
                {
                    str[a] = "   Spader " + (a-25).ToString();
                }
                if (a > 38)
                {
                    str[a] = "   Klöver " + (a-38).ToString();
                }
            }
            return str;
        }
        public string[] ShuffleCards(string[] str)
        {
            Random random = new Random();
            for (int a = 0; a <= 51; a++)
            {
                string temp = str[a];
                int randomIndex = random.Next(a + 1);
                str[a] = str[randomIndex];
                str[randomIndex] = temp;
            }
            return str;
        }
        public string[] HandleString(string[] str)
        {
            for (int a = 0; a <= 51; a++)
            {
                str[a] = str[a].Replace("   Hjärter ", "");
                str[a] = str[a].Replace("   Ruter ", "");
                str[a] = str[a].Replace("   Spader ", "");
                str[a] = str[a].Replace("   Klöver ", "");
            }
            return str;
        }

        public int[] ConvertStringToInt(string[] str)
        {
            int[] intCards = new int[52];
            for (int a = 0; a <= 51; a++)
            {
                int.TryParse(str[a], out intCards[a]);
            }
            return intCards;
        }
    }
}

This is the whole code so far if that helps i made some changes at the first code with spaces between and it still changes both of them i dont understand at all right now....

You may need to tweak my variable declaration to get the length right, but you were overwriting the originals passed in as well as returning them to the new array as output.

public string[] HandleString(string[] str)
        {
            string[] TheResults = new string[str.Length];
            for (int a = 0; a <= 51; a++)
            {
                TheResults[a] = str[a];
                TheResults[a] = TheResults[a].Replace("   Hjärter ", "");
                TheResults[a] = TheResults[a].Replace("   Ruter ", "");
                TheResults[a] = TheResults[a].Replace("   Spader ", "");
                TheResults[a] = TheResults[a].Replace("   Klöver ", "");
            }
            return TheResults;
        }

Edited 4 Years Ago by Mike Askew

ahh thank you it works fine now :) but one question? if i copy the original
CardsNumber = Cards;
CardsNumber = myObject.HandleString(CardsNumber);
and then only use the copy in the method i had earlier should'nt that give the same effect? just want to know :)

hmm didnt work as i taught well well alteast i know how to do it now :)

ahh thank you it works fine now :) but one question? if i copy the original
CardsNumber = Cards;
CardsNumber = myObject.HandleString(CardsNumber);
and then only use the copy in the method i had earlier should'nt that give the same effect? just want to know :)

It would provide the same effect yes.

This question has already been answered. Start a new discussion instead.