-5

How can I convert int (StaffID) to string in the below method. It wont accept Guid.NewGuid(ToString)

public void submitStaffID(StaffEnity staff)
{
    staff.StaffID = Convert.ToInt32(Guid.NewGuid(ToString));
    StaffEntity.Add(staff);
}

No overload for method NewGuid takes 1 argument.

namespace StaffEntity
{
    [DataContract]
    public class StaffEnity
    {
        [DataMember]
        public int StaffID;
        [DataMember]
        public string Forename;
        [DataMember]
        public string Surname;
        [DataMember]
        public DateTime TimeAdded;
    }
}
pb2q
  • 56,563
  • 18
  • 143
  • 144
G Gr
  • 5,880
  • 20
  • 87
  • 177
  • http://www.youtube.com/watch?v=WAq_Z-dT67o im following this tutorial but instead of string I declared my StaffID as an Int. (5 minutes into the video) – G Gr Mar 16 '12 at 00:39
  • 1
    Read [my answer](http://stackoverflow.com/a/9730264/601179), you can't convert `Guid` to int, I'm pretty sure you will get an exception in the conversion. – gdoron is supporting Monica Mar 16 '12 at 00:40
  • 8
    I rolled back to your previous question. **You can't edit a question to a new one because you're prevented asking new questions.** it's invalid edit which cause all of answers and comments looks stupid and unrelated. – gdoron is supporting Monica Mar 16 '12 at 01:37
  • ah ok first time I have ever had this 6 questions per day thing :S – G Gr Mar 16 '12 at 01:38

6 Answers6

3

You're looking for this:

staff.StaffID = Convert.ToInt32(Guid.NewGuid().ToString());

But converting it afterward to int32 won't work!

From MSDN:

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

An int structure, on the other hand:

Represents a 32-bit signed integer.

So, you can't convert from Guid to int without loosing most of the information.

https://stackoverflow.com/questions/4518684/convert-guid-to-int

You probably meant this

staff.StaffID = new Random().Next(int.MinValue, int.MaxValue);

Note for proper use of the Random class:
Random number generator only generating one random number

Community
  • 1
  • 1
gdoron is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
2

Well, the error message is telling you the problem... Guid.NewGuid does not take any arguments. You placed ToString inside the method call when you meant to place it after.

staff.StaffID = Convert.ToInt32(Guid.NewGuid().ToString());

Which of course won't work correctly as a Guid represents a 128-bit value, much too large for an int.

Why not make StaffId a Guid to begin with (not a Guid converted to something else, but Guid StaffId { get; set; }) or not a random value?

Ed S.
  • 119,398
  • 20
  • 176
  • 254
1

I think Guid.NewGuid().ToString() will work.

GrantVS
  • 1,871
  • 1
  • 23
  • 26
1

Perhaps you mean to do this:

staff.StaffID = Convert.ToInt32(Guid.NewGuid().ToString());
Ritch Melton
  • 11,318
  • 4
  • 39
  • 53
0

What exactly are you trying to do? What does the "ToString" variable represent?

Also, the Guid.NewGuid method doesn't have any arguments. The proper use would just be Guid.NewGuid() instead of Guid.NewGuid(ToString).

This will generate a random GUID for you. If you are worried about collision, don't mind it. The chances of generating the same GUID twice are really small.

Mathias Lykkegaard Lorenzen
  • 14,077
  • 22
  • 91
  • 177
0

ToString() is a method you apply on.

Guid.NewGuid().ToString() should give you a new guid

Shyju
  • 206,216
  • 101
  • 402
  • 492