Accessing methods in class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1305 Views - Last Post: 30 August 2018 - 05:55 PM Rate Topic: *---- 2 Votes

#1 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Accessing methods in class

Posted 27 August 2018 - 12:03 PM

Hi,

I have created this class
using System;

namespace ConsoleApp3
{
internal class Dice
{
public Dice()
{

int.TryParse(Console.ReadLine(), out int dice);
}
}
}


I am trying to access this in the static method by following:
static int RullaTärning(Random slumpObjekt)
{
Dice d1 = new Dice();

int nr = slumpObjekt.Next(1,d1 );
Console.WriteLine(nr);
return nr;

}

:code:
I am trying to create a random object depending of what i want

I am trying d1.dice to access TryParse() but does not work like that. What am i doing wrong here?

This post has been edited by modi123_1: 27 August 2018 - 12:04 PM
Reason for edit:: In the future, please highlight the text that is code and click the [code] button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Accessing methods in class

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14500
  • View blog
  • Posts: 58,122
  • Joined: 12-June 08

Re: Accessing methods in class

Posted 27 August 2018 - 12:07 PM

What do you think you are doing here?
07  public Dice()
08  {
09   
10  int.TryParse(Console.ReadLine(), out int dice);
11  }


Was This Post Helpful? 0
  • +
  • -

#3 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 27 August 2018 - 12:16 PM

That line reads what is written and types it out as a whole number through 'dice'?

What's wrong?

This post has been edited by Skydiver: 27 August 2018 - 01:25 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14500
  • View blog
  • Posts: 58,122
  • Joined: 12-June 08

Re: Accessing methods in class

Posted 27 August 2018 - 12:28 PM

First there's the general issue of having the constructor getting input instead of prioritizing setting up initial states and values.
Then there's the second issue of scope. What's the scope of the variable 'int dice'?

Traveling down from there you use the object d1 - of type 'Dice' - in the next.

static int RullaTärning(Random slumpObjekt)
3   Dice d1 = new Dice();
4 
5   int nr = slumpObjekt.Next(1,d1 );



Does the random class's next take in an integer and a 'dice' object? No.. no it doesn't.
https://docs.microso...2_System_Int32_

I would figure you need a way to return the 'int dice' value from your class. Properties are great at that.
Was This Post Helpful? 0
  • +
  • -

#5 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 27 August 2018 - 01:01 PM

In the country i am in it is night so i am tired.

Sofar i did this by your suggestion:

private static int mIndex;

        static int RullaTärning(Random slumpObjekt)
        {
            int[] malePetNames = { TryParse(Console.ReadLine())};
            int nr = slumpObjekt.Next(1, malePetNames[mIndex]);
            Console.WriteLine(nr);
            return nr;
            
        }

        private static int TryParse(string v)
        {
            throw new NotImplementedException();
        }
:code:/>

Will try better tomorrow, but any tips flips?

This post has been edited by Skydiver: 27 August 2018 - 01:26 PM
Reason for edit:: In the future, please highlight the text that is code and click the [code] button in the editor. Also no need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14500
  • View blog
  • Posts: 58,122
  • Joined: 12-June 08

Re: Accessing methods in class

Posted 27 August 2018 - 01:08 PM

Come on.. how many times do I need to badger someone about adding the 'code tags'?

Well - what happens when you run it?
Was This Post Helpful? 0
  • +
  • -

#7 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 27 August 2018 - 01:11 PM

Does not even work. I was much closer even before starting this thread

This post has been edited by Skydiver: 27 August 2018 - 01:26 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14500
  • View blog
  • Posts: 58,122
  • Joined: 12-June 08

Re: Accessing methods in class

Posted 27 August 2018 - 01:15 PM

Sure.. I have no idea where that came from (above).

Your class constructor should _NOT_ be getting values from the console. Typically you want that to set up variable values, initialize things, etc. You certainly can have it have an input parameter to set some variable with.

You never got around to responding to where the _SCOPE_ was for the 'int dice' variable.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,259
  • Joined: 05-May 12

Re: Accessing methods in class

Posted 27 August 2018 - 01:30 PM

daygamer: This is a fourth reminder: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

I highly recommend taking a more structured approach to learning C# rather than your shotgun approach. As modi123_1 is trying to lead you to discover, you need to learn the concepts of scope and type correctness. Additionally, he trying to tell you about good class design. All of these would be covered in a good C# book or tutorial that is geared towards somebody learning how to program.
Was This Post Helpful? 2
  • +
  • -

#10 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 28 August 2018 - 02:03 AM

Hi,

I am tryng to make the random.Next(); size output by wish, this means, I can add console.ReadLine and make the output how big i want. I know this and I know how to make this BUT there is another problem I am facing here.

I am trying to do exactly the above mentioned thing but in a METHOD function.

Now, how do I make a method that I can access inside random.Next(P2 + INSIDEHERE); object?

This post has been edited by daygamer: 28 August 2018 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#11 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 28 August 2018 - 02:27 AM

if i try finding my codes in books I have to read a book every time. Dont have time for that, must learn asap
Was This Post Helpful? 0
  • +
  • -

#12 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 108
  • View blog
  • Posts: 404
  • Joined: 09-September 16

Re: Accessing methods in class

Posted 28 August 2018 - 02:35 AM

You should not look for code but rather learn to write your own code.
Why don't you have time to study it properly ?
Learning programming takes a long time and if you don't have the time then better forget it.
Was This Post Helpful? 1
  • +
  • -

#13 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 28 August 2018 - 02:49 AM

I am learning by knowing what i did wrong, and that does not the books show me. I have asked a question about method no one answered in my other post

This post has been edited by Skydiver: 28 August 2018 - 05:24 AM
Reason for edit:: Remove unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6627
  • View blog
  • Posts: 27,088
  • Joined: 12-December 12

Re: Accessing methods in class

Posted 28 August 2018 - 04:26 AM

Assuming you have built the method that returns an integer then, where you have INSIDEHERE, just call the method.

I do not understand what the issue is, if you know how to write a method then you should know how to call it...?

I have merged this question with your existing topic as it is on the same subject.

Please do not open a new thread on the same topic or discussion.
Was This Post Helpful? 0
  • +
  • -

#15 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: Accessing methods in class

Posted 28 August 2018 - 04:47 AM

Hi there nice to receive your message. Inside a partial class i set this:
I have a question for you, in a get set function I get "number = Int.TryParse(console.ReadLine(),out in write)", inside the function i set write as the value, is that correct way of going?

because inside the mainfunction i wanna reach this with the "write" statement look "int nr = random.Next(1, W.number );"

You see where I am going with this right? Not really hard to understand, really simple code. I wanna tupe what console reads right in the write.

I got this error message when running the code "Process is terminated due to StackOverflowException"

and jesus christ yes i quote you so that at least you can see that i wrote to you. Dont get why this is a dealbreaker in a forum, jesus.

here is the code

internal class Size
    {
        private bool nummer;
        private int write;

        public int number
        {
            get

            {
                return number;
            }
            set
            {
              write = value ;
            }
        }
        public Size()
        {
            
           nummer = int.TryParse(Console.ReadLine(), out int write);
        }
    }


I want to access the "write" in the main function, this is a class in a partial map

this is how I am trying to reach the code

      Size W = new Size();
            int nr = random.Next(1, W.number );

This post has been edited by Skydiver: 28 August 2018 - 05:25 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3