Shorthand way to populate a udt.

Windows specific questions.
Post Reply
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Shorthand way to populate a udt.

Post by deltarho[1859] »

In the following I use 'Type <COORD>(100, 60))' which is a fantastic way to populate the COORD structure.

Code: Select all

#include once "windows.bi"

Sub SetWindow( WinLeft As Short, WinTop As Short, WinRight As Short, WinBottom As Short ) 
  Dim As HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE)
  SetConsoleScreenBufferSize(hOut, Type <COORD>(100, 60)) ' Default buffer size
  SetConsoleWindowInfo(hOut, True, Type <SMALL_RECT>(WinLeft, WinTop, WinRight, WinBottom))
End Sub

Sleep
I found the method in one of UEZ's contributions. In fact, I didn't find anyone else using it.

SetConsoleWindowInfo is typically inconsistent as it doesn't use a structure name but a pointer to a structure, so I ended up with a type mismatch when I used Type <SMALL_RECT>'.

Needless to say, I have tried umpteen ways to develop a shorthand way to avoid that.

Does a shorthand method exist when using a structure pointer, or am I forced to use the longhand way?
paul doe
Posts: 1859
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: Shorthand way to populate a udt.

Post by paul doe »

Code: Select all

Sub SetWindow( WinLeft As Short, WinTop As Short, WinRight As Short, WinBottom As Short ) 
  Dim As HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE)
  SetConsoleScreenBufferSize(hOut, Type <COORD>(100, 60)) ' Default buffer size
  SetConsoleWindowInfo(hOut, True, @Type<SMALL_RECT>(WinLeft, WinTop, WinRight, WinBottom))
End Sub
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

I despair sometimes. Why? Well, I am sure I tried @Type<SMALL_RECT> and it got 'thrown out', but I cannot remember why.

Obviously it was not one of the umpteen ways tried.

Thanks, Paul and fxm.

Thanks also to UEZ who got me thinking about this in the first place.

My next problem is giving me a window to expect. SetWindow isn't taking a blind bit of notice of the parameters I give it. According to Microsoft, both APIs expect characters for width and rows for height.
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

Here is a generalized way.

Code: Select all

Type Simon
  As Long x,y,z
End Type

Dim John As Simon
John = Type <Simon>(5,6,7)
Print John.y
Sleep
giving 6.

I may be wrong, but I'm sure '<whatever>', other than UEZ, hasn't been used before.

Is it worth putting it in the Wiki somewhere?
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shorthand way to populate a udt.

Post by fxm »

deltarho[1859] wrote: Mar 24, 2025 7:34 I am sure I tried @Type<SMALL_RECT> and it got 'thrown out', but I cannot remember why.

'@Type<UDT>([parameter, ...])' or '@UDT([parametrer, ...])':
Perhaps the UDT had a matching constructor, but your fbc version was earlier than 1.00.0.

(ps: I deleted my previous post when I saw that Paul had beaten me to it by a bit)
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shorthand way to populate a udt.

Post by fxm »

deltarho[1859] wrote: Mar 24, 2025 8:16 Is it worth putting it in the Wiki somewhere?
There is already a full page on Type (Temporary) in the documentation.
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

Type <UDT>([parameter, ...]) is NOT in the Help file!
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

Apologies fxm. I hadn't seen 'result = Type<typename>( initializers, ... )' in Type (Temporary) in the Wiki or Temporary Types in the Help file.

I am surprised that only UEZ has used it - it is very handy.

I wonder how many others are saying: "Wow, I hadn't read that either." :)
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shorthand way to populate a udt.

Post by fxm »

I seem to remember that there are many others who use it!
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

I am not so sure. If not, then it may because 'Temporary Types' is not something folk would read with a view to a 'Shorthand way to populate a udt'. :)

Added: This is a golden opportunity to thank you for the work you do on the Wiki and Help file. They are both a 'job and a half' and no mistake. :wink:
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

In the Help file in Type we have:

Code: Select all

Type clr
    red As UByte
    green As UByte
    blue As UByte
End Type
 
Dim c As clr
c.red = 255
c.green = 128
c.blue = 64
Additionally, we could have

Alternatively (See Temporary Types) we could use
Dim c As clr
c = Type <clr> (255,128,64)

That is a 'sneaky' way to introduce Temporary Types to someone who may otherwise not read it; like me. :)
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shorthand way to populate a udt.

Post by fxm »

Perhaps another little-known keyword:
- Type (Temporary) allows you to assign a list of variables to the fields of a UDT instance.
But conversely:
- LET() (Assignment) allows you to assign the fields of a UDT instance to a list of variables.

Example:

Code: Select all

Type Vector3D
    x As Double
    y As Double
    z As Double
End Type

Dim As Double x1 = 5, y1 = 7, z1 = 9

Dim v3 As Vector3D
v3 = Type(x1, y1, z1)
Print "v3.x = "; v3.x, "v3.y = "; v3.y, "v3.z = "; v3.z

Dim As Double x2, y2, z2
Let(x2, y2, z2) = v3
Print "x2 = "; x2, "y2 = "; y2, "z2 = "; z2

Sleep
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Shorthand way to populate a udt.

Post by fxm »

deltarho[1859] wrote: Mar 24, 2025 15:41 In the Help file in Type we have:

Code: Select all

Type clr
    red As UByte
    green As UByte
    blue As UByte
End Type
 
Dim c As clr
c.red = 255
c.green = 128
c.blue = 64
Additionally, we could have

Alternatively (See Temporary Types) we could use
Dim c As clr
c = Type <clr> (255,128,64)

That is a 'sneaky' way to introduce Temporary Types to someone who may otherwise not read it; like me. :)
Done:
KeyPgType → fxm [added alternative in the first example]
deltarho[1859]
Posts: 4670
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Shorthand way to populate a udt.

Post by deltarho[1859] »

Thanks fxm.
Post Reply